首页 > 解决方案 > 微调器选择的文本在选择时不调整大小

问题描述

我有一个微调器,最大字符长度为 100。文本适合微调器,但是当我选择条目时,它不会在结果中自动调整大小。如果我删除 android:layout_height 则应用程序崩溃。

带有 autoSizeTextType 的微调器 xml

    <Spinner
        android:id="@+id/spinnerCompany"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="43dp"
        android:paddingLeft="16dp"
        android:spinnerMode="dropdown"
        app:autoSizeTextType="uniform"
        app:layout_constraintTop_toBottomOf="@+id/otpLabel"
        tools:layout_editor_absoluteX="27dp" />

微调器打开,文本适合 在此处输入图像描述

已选择微调器项目,文本被截断 在此处输入图像描述

标签: androidspinner

解决方案


为 Text 指定布局高度不是一个好习惯,而是使用 textSize 并让高度和宽度 wrap_content。

<Spinner
    android:id="@+id/spinnerCompany"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="13sp" (this is just random, find the ones fits your usage)
    android:layout_marginTop="43dp"
    android:paddingLeft="16dp"
    android:spinnerMode="dropdown"
    app:autoSizeTextType="uniform"
    app:layout_constraintTop_toBottomOf="@+id/otpLabel"
    tools:layout_editor_absoluteX="27dp" />

推荐阅读