如何使文本按钮自适应以适应按钮大小

7

如何让按钮文本自动调整大小以适应按钮?

我尝试设置:

android:autoSizeTextType="uniform"

但是那行不通。如果我写成以下形式:"
",文本仍然会分成两行:
android:maxLines="1"

有一部分文本已经消失。

是否有任何编程或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>

1
请展示该按钮的完整XML代码... - deHaar
@deHaar刚刚添加了XML。 - Jolzal
这有点棘手,因为由于使用了布局权重(使用android:layout_width="0dp"android:layout_weight="1"),按钮的宽度不再仅取决于文本宽度... - deHaar
2个回答

2
基于文档autoSizeTextType属性只适用于TextView
将您的按钮更改为TextView:
<TextView
    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"/>

然后在其上定义onClick方法:
android:onClick="onButtonClick"

使其像按钮一样工作。

另外,请注意,autoSizeTextType 属性可从Android 8.0(API级别26)开始使用。


5
不正确。 MaterialButtonAppCompatButton 都有一个内部的 TextView。您也可以在它们上使用 autoSizeTextType - Gabriele Mariotti
此外,使用AppCompatTextView也可以在较低的API级别上使用。 - Dmitrijs
@GabrieleMariotti 实际上,Button 一直都是一个 TextView,因此它内部没有 TextView,它本身就是一个 TextView;-) - Darwind

1
你可以使用RelativeLayout和TextView代替Button,并为你的RelativeLayout创建一个可绘制的阴影设计,使其看起来像一个按钮。你可以给你的RelativeLayout设置宽度,并将文本设置为match parent。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接