为 TextSwitcher 自动调整 TextView 大小

Auto Size TextView for TextSwitcher

我看到 Android 在 Oreo 中发布了 textView 的新属性:

android:autoSizeTextType

这会根据显示的文本字符串调整 textView 的布局。

我怎样才能将它与 textSwitcher 一起使用?

    altitudeSwitcher = (TextSwitcher) findViewById(R.id.altitude);
    altitudeSwitcher.setFactory(new ViewFactory() {

        @Override
        public View makeView() {


            TextView t = new TextView(getApplicationContext());
            t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
            t.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 50);

            return t;
        }
    });
 <?xml version="1.0" encoding="utf-8"?>
 <TextView
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/textView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/hello" />

 private ViewFactory viewFactory = new ViewFactory() {
    public View makeView()  {
        LayoutInflater inflater = LayoutInflater.from(TextSwitcherTest.this);
        TextView textView = (TextView) inflater.inflate(R.layout.textView, null);
        return textView;
    }
};

我不知道上面的那些,但这是一个编码最少的解决方案。您可以更改主布局高度以查看自动调整大小,或者您可以点击上方的切换器。一个很酷的功能是第二个文本字段的样式在点击时会保留(即文本为白色)。

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    fun switchText(v: View) {
        (v as? TextSwitcher)?.let{
            it.tag = (((it.tag as? Int) ?: 0) + 1) % it.childCount
            it.setText((it.children.toList()[it.tag as Int] as TextView).text)
        }
    }
}

activity_main.xml

<?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="350dp"
    tools:context=".MainActivity">

    <TextSwitcher
        android:id="@+id/a"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/purple_200"
        app:layout_constraintBottom_toTopOf="@id/b"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="spread"
        android:onClick="switchText"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp">
        <TextView
            android:id="@+id/a1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="999 / 999"
            android:gravity="center"
            android:letterSpacing="-0.05"
            app:autoSizeMaxTextSize="936sp"
            app:autoSizeMinTextSize="28sp"
            app:autoSizeTextType="uniform"
            android:lines="1"
            android:includeFontPadding="false" />
        <TextView
            android:id="@+id/a2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="1000000 / 10000000"
            android:textColor="@color/white"
            android:gravity="center"
            android:letterSpacing="-0.05"
            app:autoSizeMaxTextSize="136sp"
            app:autoSizeMinTextSize="28sp"
            app:autoSizeTextType="uniform"
            android:lines="1"
            android:includeFontPadding="false" />
    </TextSwitcher>
    <TextSwitcher
        android:id="@+id/b"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/teal_200"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/a"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintVertical_chainStyle="spread"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp">
        <TextView
            android:id="@+id/b1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="999 / 999"
            android:gravity="center"
            android:letterSpacing="-0.05"
            app:autoSizeMaxTextSize="136sp"
            app:autoSizeMinTextSize="28sp"
            app:autoSizeTextType="uniform"
            android:lines="1"
            android:includeFontPadding="false" />

    </TextSwitcher>

</androidx.constraintlayout.widget.ConstraintLayout>