The reason:
The native default is to display h:mm in 12 hours. The status bar displays the time in clock. Java. In the constructor mAmPmStyle is initialized, and you can see that the default is AM_PM_STYLE_GONE, that is, AM/PM is not displayed.
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(); }... }Copy the code
Here we need to consider the time system display under the lock screen; The time display on the lock screen is in KeyGuardStatusView.java, and the internal Patterns class update method determines the 12-hour display and replaces the string with empty.
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); . }... }Copy the code
At the same time, you need to change the style of keyguard_widgeT_12_hours_format. The default is H :mm
<! -- Time format strings for fall-back clock widget --> <string name="keyguard_widget_12_hours_format" translatable="false">h:mm</string>Copy the code
Solution:
Replace the default value with 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);
Copy the code
Comment out the string substitution:
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);Copy the code
Change the 12-hour format:
<! -- 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>Copy the code