Android 如何使按钮沿xml布局均匀分布并使文本适合?

Android 如何使按钮沿xml布局均匀分布并使文本适合?,android,xml,android-layout,button,Android,Xml,Android Layout,Button,我在每个按钮上都使用了android:layout\u height=“0dp”。。但是如果字符串太长,大小仍然会改变。 如何使按钮大小恒定,同时使文本适合 (这是一个测验应用程序,我在四个按钮上生成随机字符串,所以我使用了两个水平方向的线性布局) 根据您的描述,我认为您使用的线性布局是错误的。对于水平方向,您希望将宽度设置为0dp。请看下面的代码 <LinearLayout android:layout_width="match_parent" android:layou

我在每个按钮上都使用了
android:layout\u height=“0dp”
。。但是如果字符串太长,大小仍然会改变。 如何使按钮大小恒定,同时使文本适合

(这是一个测验应用程序,我在四个按钮上生成随机字符串,所以我使用了两个水平方向的线性布局)


根据您的描述,我认为您使用的线性布局是错误的。对于水平方向,您希望将宽度设置为0dp。请看下面的代码

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="two"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="two"/>
</LinearLayout>
将上述内容添加到按钮字段。它将省略你的文字,但它将强制执行一行

您确实有一些选择:省略号、较小的字体大小、用于更精细控制的布局别名()


使用layout alias,您可以为不同大小的/dpi/方向/宽度/高度屏幕指定不同的布局,从而指定合适的字体大小。

请发布您的代码。我已经发布了所用四个按钮的xml代码。您应该能够使用
android:maxWidth
限制
按钮的大小属性。否则,现在布局很好,剩下的唯一问题是,当字符串很长且在2行中时,它会影响按钮的高度,因为按钮的宽度现在是固定的。但是现在如果字符串太长,文本会在影响高度的同时出现在两行中。水平布局可能不是最好的选择吗?
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="two"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="two"/>
</LinearLayout>
android:singleLine="true"