【问题标题】:Buttons to fill width when using TableLayout使用 TableLayout 时填充宽度的按钮
【发布时间】:2011-01-15 06:31:53
【问题描述】:

我有一个表格,每行有 2 行,每行有 3 个按钮。 如何使按钮均匀地填充空间。在 HTML 中,我会给它们 33% 的宽度。

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

【问题讨论】:

    标签: xml android views tablelayout


    【解决方案1】:

    所以在帖子结束时,正确的做法是设置NumberOfTheColumns

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

    您可以设置需要多少水平“片”。如果您有 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>
    

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

    【讨论】:

      【解决方案2】:

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

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

      例子:

      <?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>
      

      【讨论】:

      • 这个解决方案似乎没有响应。 3 列会在现代移动设备上看起来,但可能会在较小的屏幕上挤压太多东西,并且在平板电脑上会变宽......
      • 比我发现的其他任何东西都要好。
      【解决方案3】:

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

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

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

      【讨论】:

      • 这也不起作用。所有 layout_weight=1 我仍然有大小不等的按钮。
      【解决方案4】:

      尝试将android:stretchColumns="*" 添加到您的&lt;TableLayout&gt; 标记中。

      【讨论】:

      • 这不起作用。至少使用最新版本的 android。
      • @Timmmm 你这么确定?它仍在文档中:developer.android.com/reference/android/widget/…
      • 是的。您需要设置权重,并将宽度设置为 0。而您不需要需要设置stretchColumns。 Android 文档不能保证任何事情!请参阅下面的工作示例。
      • @Timmmm 这是一种方法。但是,stretchColumns 对我来说效果很好,包括在我的 Galaxy Nexus 上运行最新版本的 Android。因此,如果这对您不起作用,您的布局还有其他问题。此外,我从未发现 Android API 文档有任何问题。
      • @Timmmm 它在 froyo 、 ics 和姜饼上完美运行。经过测试。
      猜你喜欢
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      • 2018-11-10
      • 2015-01-09
      • 1970-01-01
      • 2015-09-21
      • 1970-01-01
      相关资源
      最近更新 更多