GridLayout中的按钮可以变窄而不是被裁剪吗?

时间:2014-10-02 19:25:18

标签: android android-layout

我有一个包含几个按钮的GridLayout。按钮的翻译文本通常比英文原版长,这使按钮更宽。如果需要,我希望文本包装多行,使按钮不那么宽,因此按钮仍然可以放在行中。默认情况下,GridLayout似乎只是在屏幕外绘制部分按钮或整个按钮。

Here is a screenshot of the layout preview in Android Studio.

这是布局XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/table_detail"
    style="?android:attr/textAppearanceLarge"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <GridLayout
        android:id="@+id/layoutAnswers"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="4"
        android:orientation="horizontal" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button3" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button4" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button5" />
    </GridLayout>

</LinearLayout>

我尝试过使用lines属性,它只是让按钮更高而不包装文本。我尝试过各种重力和布局:重力值。我不想在翻译中手动指定换行符。

1 个答案:

答案 0 :(得分:1)

使用TableLayout代替,使用TableLayout.setShrinkAllColumns()可以缩小按钮以适应可用的宽度。

现在我希望我可以让它们具有相同的宽度,但这不是我最初提出的要求。 更新:当TableRow的子视图的宽度= 0且权重= 1(或所有视图的相同内容)时,TableLayout列的宽度相等。然后TableRow必须具有width = FILL_PARENT,否则视图根本不会出现(至少对我而言)。