想问如何在按钮上制作文本以自动调整其大小以适合按钮。我试过设置
android:autoSizeTextType="uniform"
但是它不起作用,并且文本仍然最终变成2行,并且当我放入
android:maxLines="1"
它的某些部分不见了,有没有办法自动调整文本大小以适合按钮,因此它最多只能有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>
您可以使用RelaytiveLayout和textview代替按钮。并为您的相对布局创建一个可绘制的阴影设计,那么它看起来就像一个按钮。并且您可以给相对的布局一个宽度,并给您的文本加一个马赫父母。
基于documentation,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)