Textview显示不同字体

68 阅读1分钟

如果要封装一个这样子的实践控件

image.png

ui是否还是这样子?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:paddingBottom="@dimen/m15"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tvTime"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#ffffff"
            android:textSize="60sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tvA"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/m5"
            android:text=""
            android:textColor="#ffffff"
            android:textSize="24sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/tvWeekOfDay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="#ffffff"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/tvYMD"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text=""
        android:textColor="#ffffff"
        android:textSize="30sp" />
</LinearLayout>

其实我们可以通过代码设置不同字体大小

public static void setTextViewTimeFormat(TextView textView, String timeFormat) {
    String formattedTime = getTimeFormat(timeFormat);

    // Create a SpannableString for the formatted time
    SpannableString spannableString = new SpannableString(formattedTime);

    // Find the index of "a" in the formatted time string
    int amPmIndex = formattedTime.lastIndexOf(" ");

    // If "a" exists
    if (amPmIndex != -1) {
        // Apply smaller size to the "a" part
        spannableString.setSpan(new RelativeSizeSpan(0.3f), amPmIndex, formattedTime.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    // Set the SpannableString to the TextView
    textView.setText(spannableString);
}

调用的代码:

Utils.setTextViewTimeFormat(tvTime, "h:mm a");

最后可以优化一下布局代码,删除tvA这个textview控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:paddingBottom="@dimen/m15"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvTime"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="#ffffff"
        android:textSize="60sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvWeekOfDay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="#ffffff"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/tvYMD"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text=""
        android:textColor="#ffffff"
        android:textSize="30sp" />
</LinearLayout>