Android原生的系统,在statusbar里的系统时间,用12小时制来显示时,是没有上下午的。现在就是跟他添加这个功能,当中文时候,添加在左边,当其他语言时候,添加在右边。
系统原生图片:
修改后的样式:
如果要上下午跟时间的字体大小一样,可以直接在
platform\frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\Clock.java
这个代码里修改输出结果。
不过要是想让上下午显示的比时间稍微小点,用户看起来舒服些,可以修改布局文件,添加2个布局,分别用来显示中文和英文。
platform\frameworks\base\packages\SystemUI\res\layout\status_bar.xml
<com.android.systemui.statusbar.IconMerger android:id="@+id/notificationIcons"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:paddingLeft="6dip"
android:gravity="center_vertical"
android:orientation="horizontal"/>
<LinearLayout android:id="@+id/statusIcons"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:paddingRight="6dip"
android:gravity="center_vertical"
android:orientation="horizontal"/>
<com.android.systemui.statusbar.ChineseTextClock
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="14sp"
android:gravity="bottom"/>
<com.android.systemui.statusbar.Clock
android:textAppearance="@*android:style/TextAppearance.StatusBar.Icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:singleLine="true"
android:paddingRight="6dip"
android:gravity="center_vertical|left"
/>
<com.android.systemui.statusbar.EnglishTextClock
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="14sp"
android:gravity="bottom"/>
</LinearLayout>
然后在代码里去判断是12小时制还是24小时制,并且更加不同语言,不同时区来显示时间,实时刷新。
public class ChineseTextClock extends TextView {
private boolean mAttached;
private String tz;
private Time t;
private Calendar mCalendar;
private String mClockFormatString;
private SimpleDateFormat mClockFormat;
private static final int AM_PM_STYLE_NORMAL = 0;
private static final int AM_PM_STYLE_SMALL = 1;
private static final int AM_PM_STYLE_GONE = 2;
private static final int AM_PM_STYLE = AM_PM_STYLE_GONE;
public ChineseTextClock(Context context) {
this(context, null);
}
public ChineseTextClock(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ChineseTextClock(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (!mAttached) {
mAttached = true;
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
filter.addAction(Intent.ACTION_LOCALE_CHANGED);
//根据时区,时间变化,配置变化,语言变化来改变显示的字符串
getContext().registerReceiver(mIntentReceiver, filter, null, getHandler()); } mCalendar = Calendar.getInstance(TimeZone.getDefault()); updateClock(); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); if (mAttached) { getContext().unregisterReceiver(mIntentReceiver); mAttached = false; } } private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { String tz = intent.getStringExtra("time-zone"); mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz)); if (mClockFormat != null) { mClockFormat.setTimeZone(mCalendar.getTimeZone()); } updateClock(); } else if (action.equals(Intent.ACTION_TIME_CHANGED)) { updateClock(); } else if (action.equals(Intent.ACTION_LOCALE_CHANGED)) { updateClock(); }else if(action.equals(Intent.ACTION_TIME_TICK)){ updateClock(); } } }; final void updateClock() { //获取系统里的时间显示模式,是12小时还是24小时制
Context context = getContext(); boolean b24 = DateFormat.is24HourFormat(context);
mCalendar.setTimeInMillis(System.currentTimeMillis());
Locale locale = Locale.getDefault();
String strLocale = "" + locale;
if(strLocale.equals("zh_CN")){
if(!b24){
if (tz != null) {
t = new Time(tz);
} else {
t = new Time();
}
t.setToNow();
int hourTime = t.hour;
if(hourTime<=11){
setText(R.string.am);
}else{
setText(R.string.pm);
}
}else{
setText("");
}
}else{
setText("");
}
}
}