您的位置:首页 > 移动开发 > Android开发

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"----标识字母全大写
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: