需求效果图
原理
TextClock + format12Hour/format24Hour + 纯布局实现
1.1 时间格式定义 String.xml
<resources>
<string name="clock_12_am_pm_format" translatable="false">a</string>
<string name="clock_24_am_pm_format" translatable="false"></string>
<!-- Time format strings for fall-back clock widget -->
<string name="clock_12_hours_format" translatable="false">hh:mm</string>
<!-- Time format strings for fall-back clock widget -->
<string name="clock_24_hours_format" translatable="false">kk:mm</string>
</resources>
1.2 布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextClock
android:id="@+id/default_clock_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="56dp"
android:format12Hour="@string/clock_12_hours_format"
android:format24Hour="@string/clock_24_hours_format"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextClock
android:id="@+id/am_pm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:format12Hour="@string/clock_12_am_pm_format"
android:format24Hour="@string/clock_24_am_pm_format"
android:textSize="15dp"
app:layout_constraintLeft_toRightOf="@+id/default_clock_view"
app:layout_constraintBaseline_toBaselineOf="@+id/default_clock_view"
android:layout_marginStart="5dp" />
</androidx.constraintlayout.widget.ConstraintLayout>