android-下拉通知栏日期显示
2013-12-16 20:23
429 查看
4.3
(代码修改时注意包的导入)
1:只显示月日星期几的DateView:
DateView.java
触发日期更新:
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_TIME_TICK.equals(action)
|| Intent.ACTION_TIME_CHANGED.equals(action)
|| Intent.ACTION_TIMEZONE_CHANGED.equals(action)
|| Intent.ACTION_LOCALE_CHANGED.equals(action)) {
updateClock();
}
}
};
//日期更新
protected void updateClock() {
//dateFormat-时间格式
/*system_ui_date_pattern定义在donottranslate。xml文件中,对应是
只有月日星期
<!-- @hide DO NOT TRANSLATE. ICU pattern for "Mon, 14 January" -->
<string name="icu_abbrev_wday_month_day_no_year">eeeMMMMd</string>
<!-- @hide DO NOT TRANSLATE. date formatting pattern for system ui.-->
<string name="system_ui_date_pattern">@string/icu_abbrev_wday_month_day_no_year</string>
*/
/*
eeeMMMMd:
"d"代表年月日星期几中的“日”;
“eee”代表年月日星期几中的“星期几”;
“MMMM”代表年月日星期几中的“月”;
*/
final String dateFormat = getContext().getString(R.string.system_ui_date_pattern);
final Locale l = Locale.getDefault(); //获得当前语言环境
String fmt = ICU.getBestDateTimePattern(dateFormat, l.toString());
SimpleDateFormat sdf = new SimpleDateFormat(fmt, l);
setText(sdf.format(new Date()));
另外:
/*
Locale.getDefault()该方法检测当前语言环境并随之动态变化;
*/
/*Date.java
public Date() {
this(System.currentTimeMillis());
}
*/
}
2:显示年月日星期几
修改方法:
1>:修改xml文件<string name="icu_abbrev_wday_month_day_no_year">YYYYeeeMMMMd</string>
2>:修改代码:
protected void updateClock() {
。。。。
final String dateFormat = "YYYY"+getContext().getString(R.string.system_ui_date_pattern);
。。。。
}
3:双行显示年月日星期几
注意view的android:layout_centerVertical="true"标识该view布局在父布局的垂直居中;
注意此情况下日期view不可设置为android:singleLine="true"。
private final void updateClock() {
final Context context = getContext();
Date now = new Date();
CharSequence dow = DateFormat.format("EEEE", now);
String format=Settings.System.getString(context.getContentResolver(), Settings.System.DATE_FORMAT);
String date ="";
if(format==null || "".equals(format.trim())){
date = DateFormat.getDateFormat(context).format(now);
}else{
date = new SimpleDateFormat(format).format(now);
}
//end
setText(context.getString(R.string.status_bar_date_formatter, dow, date));
}
4:双行显示存在 android:layout_alignBaseline属性时:
相对布局中android:layout_alignBaseline="@id/clock"标识当前view基线是clock这个view,如下date的文字显示第一行以clock底部对其的,如下图:
<RelativeLayout
android:id="@+id/datetime"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingStart="2dp"
android:paddingEnd="2dp"
android:background="@drawable/ic_notify_button_bg"
android:enabled="false"
>
<com.android.systemui.statusbar.policy.Clock
android:id="@+id/clock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:singleLine="true"
android:textAppearance="@style/TextAppearance.StatusBar.Expanded.Clock"
android:layout_centerVertical="true"
/>
<com.android.systemui.statusbar.policy.DateView android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.StatusBar.Expanded.Date"
android:layout_toEndOf="@id/clock"
android:layout_centerVertical="true"
android:layout_alignBaseline="@id/clock"
/>
</RelativeLayout>
5:其他随笔
TextView的android:textAllCaps ="true"----标识字母全大写
(代码修改时注意包的导入)
1:只显示月日星期几的DateView:
DateView.java
触发日期更新:
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_TIME_TICK.equals(action)
|| Intent.ACTION_TIME_CHANGED.equals(action)
|| Intent.ACTION_TIMEZONE_CHANGED.equals(action)
|| Intent.ACTION_LOCALE_CHANGED.equals(action)) {
updateClock();
}
}
};
//日期更新
protected void updateClock() {
//dateFormat-时间格式
/*system_ui_date_pattern定义在donottranslate。xml文件中,对应是
只有月日星期
<!-- @hide DO NOT TRANSLATE. ICU pattern for "Mon, 14 January" -->
<string name="icu_abbrev_wday_month_day_no_year">eeeMMMMd</string>
<!-- @hide DO NOT TRANSLATE. date formatting pattern for system ui.-->
<string name="system_ui_date_pattern">@string/icu_abbrev_wday_month_day_no_year</string>
*/
/*
eeeMMMMd:
"d"代表年月日星期几中的“日”;
“eee”代表年月日星期几中的“星期几”;
“MMMM”代表年月日星期几中的“月”;
*/
final String dateFormat = getContext().getString(R.string.system_ui_date_pattern);
final Locale l = Locale.getDefault(); //获得当前语言环境
String fmt = ICU.getBestDateTimePattern(dateFormat, l.toString());
SimpleDateFormat sdf = new SimpleDateFormat(fmt, l);
setText(sdf.format(new Date()));
另外:
/*
Locale.getDefault()该方法检测当前语言环境并随之动态变化;
*/
/*Date.java
public Date() {
this(System.currentTimeMillis());
}
*/
}
2:显示年月日星期几
修改方法:
1>:修改xml文件<string name="icu_abbrev_wday_month_day_no_year">YYYYeeeMMMMd</string>
2>:修改代码:
protected void updateClock() {
。。。。
final String dateFormat = "YYYY"+getContext().getString(R.string.system_ui_date_pattern);
。。。。
}
3:双行显示年月日星期几
注意view的android:layout_centerVertical="true"标识该view布局在父布局的垂直居中;
注意此情况下日期view不可设置为android:singleLine="true"。
private final void updateClock() {
final Context context = getContext();
Date now = new Date();
CharSequence dow = DateFormat.format("EEEE", now);
String format=Settings.System.getString(context.getContentResolver(), Settings.System.DATE_FORMAT);
String date ="";
if(format==null || "".equals(format.trim())){
date = DateFormat.getDateFormat(context).format(now);
}else{
date = new SimpleDateFormat(format).format(now);
}
//end
setText(context.getString(R.string.status_bar_date_formatter, dow, date));
}
4:双行显示存在 android:layout_alignBaseline属性时:
相对布局中android:layout_alignBaseline="@id/clock"标识当前view基线是clock这个view,如下date的文字显示第一行以clock底部对其的,如下图:
<RelativeLayout
android:id="@+id/datetime"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingStart="2dp"
android:paddingEnd="2dp"
android:background="@drawable/ic_notify_button_bg"
android:enabled="false"
>
<com.android.systemui.statusbar.policy.Clock
android:id="@+id/clock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:singleLine="true"
android:textAppearance="@style/TextAppearance.StatusBar.Expanded.Clock"
android:layout_centerVertical="true"
/>
<com.android.systemui.statusbar.policy.DateView android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.StatusBar.Expanded.Date"
android:layout_toEndOf="@id/clock"
android:layout_centerVertical="true"
android:layout_alignBaseline="@id/clock"
/>
</RelativeLayout>
5:其他随笔
TextView的android:textAllCaps ="true"----标识字母全大写
相关文章推荐
- android下拉通知栏日期显示固定不变 问题修改
- Android组件:radio、checkbox、下拉列表(两种方式)、日期;界面不显示appname
- select 设置下拉列表的选中项的值 显示当前日期 年份 月份 下拉列表获取当前日期
- android ListView向上滑动隐藏标题,下拉显示标题栏
- Android显示时间和日期的控件的使用总结
- android-Ultra-Pull-To-Refresh-下拉自定义显示动画1
- Android中的日期时间显示
- android自定义Spinner下拉菜单和AutoCompleteTextView自动显示的(下拉列表框)样式
- Android Calender显示日期与时间
- Android下拉通知栏会影响Activity的生命周期方法吗?
- Android 使用DatePicker以及TimePicker显示当前日期和时间
- android—DatePicker 和TimePicker显示日期以及使用TimePickerDialog,DatePickerDialog来专门实现时间选择对话框
- Android开发之显示通知
- Android日期空间(DatePickerDialog)中的年月日不全部显示
- android 网络运营商的名字显示规则(锁定屏幕,下拉列表)
- Android日期显示和日期选择库
- Android中Spinner下拉列表(使用自定义Adapter实现,显示数据取自联系人数据库)
- VBA代码控制DTPicker控件的下拉日期表显示
- javascript 下拉框显示当前日期