Android 如何使文本按钮自动调整大小以适应按钮
想问一下如何使按钮上的文字自动调整大小,以适应按钮。 我试过布景Android 如何使文本按钮自动调整大小以适应按钮,android,android-studio,android-layout,kotlin,android-button,Android,Android Studio,Android Layout,Kotlin,Android Button,想问一下如何使按钮上的文字自动调整大小,以适应按钮。 我试过布景 android:autoSizeTextType="uniform" 但它不起作用,文本仍然变成了两行,当我把 android:maxLines="1" 它的一部分消失了,有没有办法自动调整文本大小以适应按钮,使其最多只有一行,并以编程方式或xml方式显示整个文本 我是一个新手,所以任何建议都将非常感谢。 提前谢谢 编辑: 这是我的xml,我想要适合的按钮文本是btnReq和btnAcc <?xml versio
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>
您可以使用Relayvelayout和文本视图而不是按钮。为你的相对布局创建一个可绘制的阴影设计,然后它看起来像一个按钮。您可以为相对布局指定宽度,并为文本指定马赫数。您可以使用RelayiveLayout和textview代替按钮。为你的相对布局创建一个可绘制的阴影设计,然后它看起来像一个按钮。您还可以为相对布局指定宽度,并为文本指定马赫数父级。基于,
autoSizeTextType
属性仅适用于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"/>
让它像按钮一样工作
另外,请记住,autoSizeTextType
属性从
Android 8.0(API级别26)基于,autoSizeTextType属性仅在文本视图上工作
将按钮更改为文本视图:
<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"/>
让它像按钮一样工作
另外,请记住,autoSizeTextType
属性从
安卓8.0(API级别26)请显示此按钮的完整xml代码…@deHaar刚刚添加了xml,这有点棘手,因为使用了布局权重(使用
Android:layout\u width=“0dp”
和Android:layou weight=“1”
),按钮的宽度没有(仅)不再依赖于文本宽度…请显示此按钮的完整xml代码…@deHaar刚刚添加了xml,这有点棘手,因为由于使用了布局权重(使用android:layout\u width=“0dp”
和android:layout\u weight=“1”
),按钮的宽度没有(仅)不再依赖于文本宽度…这是不正确的。MaterialButton
和AppCompatButton
内部有一个TextView
。您也可以将autoSizeTextType
与它们一起使用。此外,在较低的API级别上可以使用AppCompatTextView。这是不正确的。MaterialButton
和AppCompatButton
内部有一个TextView
。您还可以将autoSizeTextType
与它们一起使用。此外,它还可以使用AppCompatTextView在较低的API级别上使用。