Android 使用 TableLayout 时填充宽度的按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2237001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:06:05  来源:igfitidea点击:

Buttons to fill width when using TableLayout

xmlandroidviewstablelayout

提问by Pentium10

I have a table having 2 rows each row having 3 buttons. How can I make the buttons to fill the space equally. In HTML I would give them 33% width.

我有一个表,每行有 2 行,有 3 个按钮。我怎样才能让按钮均匀地填充空间。在 HTML 中,我会给它们 33% 的宽度。

Also do you know any way I can create a view having 4 image buttons in a row as a grid layout, similar to the launcher.

你也知道我有什么方法可以创建一个具有 4 个连续图像按钮的视图作为网格布局,类似于启动器。

回答by Mark B

Try adding android:stretchColumns="*"to your <TableLayout>tag.

尝试添加android:stretchColumns="*"到您的<TableLayout>标签。

回答by Timmmm

I finally found the true answer here: http://androidadvice.blogspot.com/2010/10/tablelayout-columns-equal-width.html

我终于在这里找到了真正的答案:http: //androidadvice.blogspot.com/2010/10/tablelayout-columns-equal-width.html

There is a more minimal example on stackoverflow also here: https://stackoverflow.com/a/2865558/265521

这里还有一个关于 stackoverflow 的最小示例:https: //stackoverflow.com/a/2865558/265521

Example:

例子:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    <TableRow>
        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:text="Why is doing layouts on Android"
            android:layout_weight="1"
            />
        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:text="so"
            android:layout_weight="1"
            />
    </TableRow>
    <TableRow>
        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:text="damn"
            android:layout_weight="1"
            />
        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:text="frustrating?"
            android:layout_weight="1"
            />
    </TableRow>
</TableLayout>

回答by Cheryl Simon

Set the TableRow layout_width to fill_parent and set a layout_weight of 1 on each button.

将 TableRow layout_width 设置为 fill_parent 并将每个按钮的 layout_weight 设置为 1。

The layout_weight works sort of like a percentage. If all of your items get the same number, they take the same percent of space. If one button has a weight of 2, and another has a weight of 1, then the first will take up twice as much space.

layout_weight 有点像百分比。如果您的所有项目都具有相同的编号,则它们占用的空间百分比相同。如果一个按钮的权重为 2,另一个按钮的权重为 1,则第一个按钮将占用两倍的空间。

If you haven't done so already, ready through the common layoutspage of the dev guide for a good intro to layouts.

如果您还没有这样做,请准备好阅读开发指南的通用布局页面,以了解布局的良好介绍。

回答by Milan

So in conclusion of the posts, the right way to do it is to set the NumberOfTheColumns:

因此,在帖子的总结中,正确的方法是设置NumberOfTheColumns

   <tablelayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:stretchColumns="NumberOfTheColumns"/>

You can set how many horizontal "piece" you need. If you have a 3x3 table:

您可以设置您需要多少个水平“片”。如果您有一张 3x3 的桌子:

 <TableLayout
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tableLayout1"
    android:stretchColumns="3">
    <TableRow
        android:id="@+id/tableRow1">
        <TextView
            android:text="@string/EventTitle"
            android:id="@+id/textView1"
            android:gravity="center_vertical"
            android:layout_column="0" />
        <EditText
            android:inputType="text"
            android:id="@+id/editText3"
            android:layout_weight="2"
            android:layout_span="2"
            android:layout_column="1" />
    </TableRow>
    <TableRow
        android:id="@+id/tableRow2">
        <TextView
            android:text="@string/EventDateStart"
            android:id="@+id/textView1"
            android:gravity="center_vertical"
            android:layout_column="0" />
        <EditText
            android:inputType="date"
            android:id="@+id/editText1"
            android:layout_weight="1"
            android:layout_column="1"
            android:layout_width="wrap_content" />
        <EditText
            android:inputType="time"
            android:id="@+id/editText2"
            android:layout_weight="1"
            android:layout_column="2"
            android:layout_width="match_parent" />
    </TableRow>
    <TableRow
        android:id="@+id/tableRow3">
        <TextView
            android:text="@string/EventDateEnd"
            android:id="@+id/textView1"
            android:gravity="center_vertical"
            android:layout_column="0"
            android:layout_width="match_parent" />
        <EditText
            android:inputType="date"
            android:id="@+id/editText1"
            android:layout_weight="1"
            android:layout_column="1" />
        <EditText
            android:inputType="time"
            android:id="@+id/editText2"
            android:layout_weight="1"
            android:layout_column="2" />
    </TableRow>
    <TableRow>
        <TextView
            android:text="@string/Description"
            android:id="@+id/textView1"
            android:gravity="center_vertical"
            android:layout_weight="3"
            android:layout_span="3"
            android:layout_column="0"
            android:layout_width="match_parent" />
    </TableRow>
    <TableRow>
        <EditText
            android:inputType="textMultiLine"
            android:id="@+id/editText3"
            android:layout_height="50dp"
            android:layout_weight="3"
            android:layout_span="3"
            android:layout_column="0"
            android:layout_width="match_parent" />
    </TableRow>
</TableLayout>

In this sample you can see a 5x3 table. You can set each column width by set how many pieces of the full table width do you need...

在此示例中,您可以看到一个 5x3 的表格。您可以通过设置您需要多少个完整的表格宽度来设置每个列的宽度...