Android R锁屏/状态AM.PM显示

625

原因:

原生默认在12小时制下是显示h:mm的。状态栏上时间显示是在Clock.java中,在构造函数中会初始化mAmPmStyle,可以看到默认是为AM_PM_STYLE_GONE,即不显示AM/PM的。

    public Clock(Context context, AttributeSet attrs, int defStyle) {
        ......
        try {
            mSystemUIFactoryBase = OpSystemUICustomizationFactoryBase.getOpFactory(context);
            mStatusBarExt = mSystemUIFactoryBase.makeSystemUIStatusBar(context);
            mAmPmStyle = mStatusBarExt.getClockAmPmStyle(a.getInt(R.styleable.Clock_amPmStyle,
                    AM_PM_STYLE_NORMAL));
            mShowDark = a.getBoolean(R.styleable.Clock_showDark, true);
            mNonAdaptedColor = getCurrentTextColor();
        }
        .....
     }

 这里需要考虑到锁屏下的时间制式显示;锁屏上的时间显示是在KeyguardStatusView.java中,在其内部的Patterns类update方法中,会去判断12小时显示,然后替换字串为空。

    private static final class Patterns {
        ......

        static void update(Context context) {
            .....

            clockView12 = DateFormat.getBestDateTimePattern(locale, clockView12Skel);
            // CLDR insists on adding an AM/PM indicator even though it wasn't in the skeleton
            // format.  The following code removes the AM/PM indicator if we didn't want it.
            if (!clockView12Skel.contains("a")) {
                clockView12 = clockView12.replaceAll("a", "").trim();
            }

            clockView24 = DateFormat.getBestDateTimePattern(locale, clockView24Skel);
            ......
       }
       ......
     }

 同时,需要将"keyguard_widget_12_hours_format"的样式进行修改,默认是h:mm

    <!-- Time format strings for fall-back clock widget -->
    <string name="keyguard_widget_12_hours_format" translatable="false">h:mm</string>

解决方案:

将默认值替换成AM_PM_STYLE_NORMAL即可:

             mAmPmStyle = mStatusBarExt.getClockAmPmStyle(a.getInt(R.styleable.Clock_amPmStyle,
-                    AM_PM_STYLE_GONE));
+                    AM_PM_STYLE_NORMAL));
             mShowDark = a.getBoolean(R.styleable.Clock_showDark, true);

 将字串替换的地方注释掉:

             clockView12 = DateFormat.getBestDateTimePattern(locale, clockView12Skel);
             // CLDR insists on adding an AM/PM indicator even though it wasn't in the skeleton
             // format.  The following code removes the AM/PM indicator if we didn't want it.
-            if (!clockView12Skel.contains("a")) {
-                clockView12 = clockView12.replaceAll("a", "").trim();
-            }
+            //if (!clockView12Skel.contains("a")) {
+            //    clockView12 = clockView12.replaceAll("a", "").trim();
+            //}

             clockView24 = DateFormat.getBestDateTimePattern(locale, clockView24Skel);

 将12小时制式更改:

     <!-- Time format strings for fall-back clock widget -->
-    <string name="keyguard_widget_12_hours_format" translatable="false">h:mm</string>
+    <string name="keyguard_widget_12_hours_format" translatable="false">aa</string>