如何使文本按钮自动调整大小以适合按钮

Jol*_*zal 7 android android-layout android-button kotlin android-studio

如何使按钮文本自动调整其大小以适合按钮?

我尝试过设置:

android:autoSizeTextType="uniform"
Run Code Online (Sandbox Code Playgroud)

但这行不通。
如果我输入以下内容,文本仍然以两行结束:

android:maxLines="1"
Run Code Online (Sandbox Code Playgroud)

文本的某些部分消失了。

是否有任何方法(以编程方式或在 XML 中)自动调整文本大小以适合一行上的按钮,并且还显示整个文本?

编辑 :

这是我的 XML,我想要放入文本的按钮是“btnReq”和“btnAcc”:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".PlayFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        app:layout_constraintBottom_toTopOf="@+id/tableLayout"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.074"
        tools:layout_editor_absoluteX="0dp">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:paddingRight="10dp">

            <EditText
                android:id="@+id/et_email"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:ems="10"
                android:hint="email"
                android:inputType="textPersonName"
                android:autoSizeTextType="uniform"/>

            <Button
                android:id="@+id/btnReq"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="REQUEST"
                android:autoSizeTextType="uniform"
                android:maxLines="1"/>

            <Button
                android:id="@+id/btnAcc"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="ACCEPT"
                android:autoSizeTextType="uniform"
                android:maxLines="1"/>
        </LinearLayout>
    </LinearLayout>

    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerInParent="true"
        android:padding="10dp"
        app:layout_constrainedHeight="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintDimensionRatio="1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_default="percent">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <Button
                android:id="@+id/btn1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@color/colorPrimary" />

            <Button
                android:id="@+id/btn2"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@color/colorPrimary" />

            <Button
                android:id="@+id/btn3"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@color/colorPrimary" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <Button
                android:id="@+id/btn4"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@color/colorPrimary" />

            <Button
                android:id="@+id/btn5"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@color/colorPrimary" />

            <Button
                android:id="@+id/btn6"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@color/colorPrimary" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <Button
                android:id="@+id/btn7"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@color/colorPrimary" />

            <Button
                android:id="@+id/btn8"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@color/colorPrimary" />

            <Button
                android:id="@+id/btn9"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_margin="2dp"
                android:layout_weight="1"
                android:background="@color/colorPrimary" />
        </TableRow>

    </TableLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

Nad*_*dia 1

您可以使用 RelaytiveLayout 和 textview 而不是按钮。并为您的相对布局创建一个可绘制的阴影设计,然后它看起来像一个按钮。您可以为相对布局指定宽度,并为文本指定马赫父级。