statusbar上根据不同语言,添加PM,AM,或者上下午
16lz
2021-01-25
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.EnglishTextClockandroid: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);}@Overrideprotected 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();}@Overrideprotected void onDetachedFromWindow() {super.onDetachedFromWindow();if (mAttached) {getContext().unregisterReceiver(mIntentReceiver);mAttached = false;}}private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {@Overridepublic 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("");}}}
更多相关文章
- Nginx系列教程(六)| 手把手教你搭建 LNMP 架构并部署天空网络电影
- Android系统编译及单模块编译简介
- [置顶] Android系统下的动态Dex加载
- 基于Android系统的IPv6网络接入分析
- 面试题目及其答案
- Android(安卓)armeabi的so库兼容问题
- Android6.0权限系统
- Android实现可播放GIF动画的ImageView
- 安卓源码框架学习-framework层