statusbar上根据不同语言,添加PM,AM,或者上下午
2011-10-06 02:06
531 查看
Android原生的系统,在statusbar里的系统时间,用12小时制来显示时,是没有上下午的。现在就是跟他添加这个功能,当中文时候,添加在左边,当其他语言时候,添加在右边。
系统原生图片:
修改后的样式:
如果要上下午跟时间的字体大小一样,可以直接在
不过要是想让上下午显示的比时间稍微小点,用户看起来舒服些,可以修改布局文件,添加2个布局,分别用来显示中文和英文。
然后在代码里去判断是12小时制还是24小时制,并且更加不同语言,不同时区来显示时间,实时刷新。
系统原生图片:
修改后的样式:
如果要上下午跟时间的字体大小一样,可以直接在
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(""); } } }
相关文章推荐
- statusbar上根据不同语言,添加PM,AM,或者上下午
- 利用组策略根据不同的部门添加文件共享服务器权限
- SharePoint【学习笔记】-- SharePoint 2010 利用JavaScript根据用户使用的语言应用不同的样式
- 使用 根据语言环境不同 而显示不同的 资源本地化 ASP.NET 网页
- Win8系统添加各个国家不同语言的输入法教程
- 页面根据不同的情况添加不同的样式表
- 编译Android时,添加或者删除system.img中第三方apk、更改Android系统默认语言
- AM、PM是上午和下午的英文缩写、英文缩写(英语星期月份等)
- 根据设备及设备语言去访问不同的网址。(核心:navigator.userAgent、navigator.language)
- Springmvc同一请求下根据不同的逻辑返回页面或者json
- jsp根据浏览器语言实现展示不同语言页面的做法
- yii CGridView 如何根据字段值来显示不同的自定义按钮或者文字?
- ShopEx customSchema 定制可以根据客户的需求对网站进行相应功能的添加修改或者删除
- 编译Android时,添加或者删除system.img中第三方apk、更改Android系统默认语言
- 编译Android时,添加或者删除system.img中第三方apk、更改Android系统默认语言
- jquery实现根据不同class为多个按钮添加点击事件
- Android打电话程序(根据添加按钮监听的3种不同方式分别实现)
- PHP网页根据来访者的浏览器语言不同自动跳转中英文页面
- 带AM或者PM的时间字符串转data类型
- 根据浏览器语言的不同让网站跳转到不同的语言版