在TableLayout中设置列的宽度

时间:2014-02-18 21:17:23

标签: android android-layout

我在设置TableLayout中的列宽时遇到问题。此问题已在前面讨论过:
How to set Table width and number of Columns in TableLayout in Android
How to get a TableLayout with 2 columns of equal width
Set equal width of columns in table layout in Android
但我没有找到解决方案。下面的代码确实将列设置为相等的宽度,但它似乎关闭了第1列中的按钮。自从按钮的文本 第二列更宽,它以不受欢迎的方式显示。

TableLayout

期望的展示:

enter image description here     

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/menu1Button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:layout_weight="1"
            android:background="@color/darkgrey2"
            android:contentDescription="@string/menu1"
            android:onClick="click1"
            android:padding="60dp"
            android:text="hello"
            android:textColor="@color/white"
            android:textSize="32sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/menu2Button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:layout_weight="1"
            android:background="@color/darkgrey2"
            android:contentDescription="@string/menu1"
            android:onClick="click1"
            android:padding="60dp"
            android:text="hellothere"
            android:textColor="@color/white"
            android:textSize="32sp"
            android:textStyle="bold" />
    </LinearLayout>
</TableRow>

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/menu3Button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:layout_weight="1"
            android:background="@color/darkgrey2"
            android:contentDescription="@string/menu1"
            android:onClick="click1"
            android:padding="60dp"
            android:text="hello"
            android:textColor="@color/white"
            android:textSize="32sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/menu4Button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:layout_weight="1"
            android:background="@color/darkgrey2"
            android:contentDescription="@string/menu1"
            android:onClick="click1"
            android:padding="60dp"
            android:text="hello"
            android:textColor="@color/white"
            android:textSize="32sp"
            android:textStyle="bold" />
    </LinearLayout>
</TableRow>

编辑: 删除边距和填充会导致: enter image description here

EDIT2: 仅删除保证金: enter image description here

4 个答案:

答案 0 :(得分:1)

将TableRow的宽度设置为match_parent,它将使用它的子元素(即按钮)进行扩展

<TableRow
   android:id="@+id/tableRow2"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" >

不确定是否想要LinearLayout,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="20dp"
        android:text="Hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="20dp"
        android:text="Hello There" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="20dp"
        android:text="Button" />

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="20dp"
        android:text="Button" />
</LinearLayout>

答案 1 :(得分:1)

我认为您无法以这种方式实现,因为您的宽度基于weight而不是width属性。由于你将所有按钮的宽度都放在具有最大宽度的按钮的宽度上,为了获得这种效果,我将使用不同的方法,基于选择最大值。所有按钮的宽度,并将其设置为其余按钮。如果您有一个静态数量的按钮,或者您可以跟踪布局中的每个按钮,这将有效。所以,恢复:

1)从布局中删除layout_weight属性。

2)如果您不知道在创建查看时分配给他们的文本,您可以将layout_width属性保留为0,否则将其设置为wrap_content。 / p>

3)在你的onCreate()或某种方法中,做一些像这样的事情(这是伪代码):

int maxwidth = -1;

// Firstly, get the maximum width
for (Button myButton : my_buttons)
  if (myButton.getWidth() > maxwidth)
    maxwidth = myButton.getWidth();

// Assign it to all your Buttons
for (Button myButton : my_buttons)
  myButton.setWidth(maxwidth);

答案 2 :(得分:1)

试试这段代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/menu1Button"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:contentDescription="Button"
                android:onClick="click1"
                android:padding="60dp"
                android:text="hello"
                android:textSize="32sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/menu2Button"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:contentDescription="Button2"
                android:onClick="click1"
                android:padding="60dp"
                android:text="hellothere"
                android:textSize="32sp"
                android:textStyle="bold" />
        </LinearLayout>
    </TableRow>
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/menu3Button"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:onClick="click1"
                android:padding="60dp"
                android:text="hello"
                android:textSize="32sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/menu4Button"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:onClick="click1"
                android:padding="60dp"
                android:text="hello"
                android:textSize="32sp"
                android:textStyle="bold" />
        </LinearLayout>
    </TableRow>
</LinearLayout>

答案 3 :(得分:0)

您必须在按钮声明中添加以下内容:

android:singleLine="true"

希望这有助于:)