statusbar上根据不同语言,添加PM,AM,或者上下午

本文介绍了如何在Android系统状态栏中,根据语言环境(中文或非中文)显示12小时制时间时,添加PM或AM标识。内容包括修改原生系统时间显示样式,以及通过调整布局文件实现不同语言环境下上下午字体大小的适配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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("");
		}
	
	}
}


评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值