【发布时间】:2020-09-27 15:25:44
【问题描述】:
例如,我有一个带有 2 个按钮的垂直线性布局,里面有 android:layout_weight="1"。问题是,如果我使用
 在按钮文本中添加新行并增加其大小,则该按钮会变得比另一个大。如果更改了文本大小,如何使按钮不更改其大小?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ACC1"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/button18"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/colorofbuttons"
android:fontFamily="@font/architects_daughter"
android:text="Mom"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp"></Button>
<Button
android:id="@+id/button19"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/colorofbuttons"
android:ellipsize="end"
android:fontFamily="@font/architects_daughter"
android:maxLines="1"
android:text="My Name"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/button20"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/colorofbuttons"
android:fontFamily="@font/architects_daughter"
android:text="Dad"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp" />
<Button
android:id="@+id/button21"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/colorofbuttons"
android:fontFamily="@font/architects_daughter"
android:text="Girl"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
【问题讨论】:
-
已尝试在您的
button中使用android:maxLines="1"和android:ellipsize="end" -
感谢您的回复。它没有按我的意愿工作。如果文本是“我的
name”,现在显示我的... -
能否提供XML代码?
-
@PhilipDaniel 在下面查看我的答案!
-
@PhilipDaniel 您能否将您的预期输出与问题分享为图像或屏幕截图