【问题标题】:XML Table layout? Two EQUAL-width rows filled with equally width buttons?XML 表布局?两个等宽的行填充了等宽的按钮?
【发布时间】:2011-02-21 08:54:03
【问题描述】:

这是我的 LAND 格式 XML 的一部分:

<TableLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center"
    android:stretchColumns="*">
<TableRow>    
    <Button
        android:id="@+id/countbutton"
        android:text="@string/plus1"/>      
    <Button
        android:id="@+id/resetbutton"
        android:text="@string/reset" 
        />  
</TableRow>
</TableLayout>

现在我没有得到 - 一行和按钮的宽度取决于按钮内的 TEXT。如果两个文本都一样长,可以说: TEXT 没问题 - 表的一半在屏幕中间。但如果它们的大小不同 - 让我们说“A”和“这是长按钮”,表格的中心不再位于屏幕中间,因此按钮的宽度不相等......

【问题讨论】:

    标签: android xml width android-tablelayout


    【解决方案1】:

    要在按钮大小相同的行中放置按钮,您需要这样做。

        <LinearLayout android:orientation="horizontal" 
             android:layout_width="fill_parent"
             android:layout_height="fill_parent">
             <Button android:layout_weight="1" 
                 android:layout_height="wrap_content" 
                 android:layout_width="0dip"/>
             <Button android:layout_weight="1" 
                 android:layout_height="wrap_content" 
                 android:layout_width="0dip"/>
        </LinearLayout>
    

    并为您的按钮填写其他 xml 属性。

    神奇之处在于 layout_weight 和 width 属性。您不需要表格布局。这些属性告诉布局你的视图应该在父布局中占据相等的空间。

    【讨论】:

    • 如何在代码中为动态创建的按钮设置这些值?
    • parant.addView(newView, new LinearLayout.LayoutParams(0 /*width*/, WRAP_CONTENT, 1 /*weight*/));
    • 如果您通过搜索找到此内容,请注意您需要在 LinearLayout 上执行 android:layout_width="fill_parent" 或其他值。我在运行时遇到了关于如何指定 layout_width 的错误,我花了一段时间才发现它抱怨的是 LinearLayout,而不是包含零值布局宽度的项目。另请参阅stackoverflow.com/questions/1177020/…
    • 我们怎样才能使两行高度相等?请看我的问题stackoverflow.com/q/10779802/720176
    【解决方案2】:

    除了接受的答案:

    我有一个类似的问题,我需要在一个网格中具有相同列宽的多个图像,所以我使用了表格布局。它可以工作,但是由于图像是异步加载的,相应的列会占据整个宽度,直到所有列中至少有一张图像。

    我使用 Robby Pond 的解决方案解决了这个问题,但它不适用于最后一行,它不一定有与其他行一样多的图像,当我想要它们时拉伸这些图像以占用所有可用空间适合与上述相同的列。为了解决这个问题,我用常规的 View 对象填充了该行的剩余空列,

    使用与所有其他图像相同的布局参数:

    width = 0, weight = 1. 这样就解决了!

    【讨论】:

    • 这是表格布局 +1 的完美答案
    【解决方案3】:

    很好的例子(最初来自http://androidadvice.blogspot.com/2010/10/tablelayout-columns-equal-width.html

    测试和工作:

    <TableRow>
      <!-- Column 1 -->
      <TextView
         android:id="@+id/tbl_txt1"
         android:layout_width="0dip"
         android:layout_height="wrap_content"
         android:background="@color/red"
         android:textColor="@color/white"
         android:padding="10dip"
         android:layout_margin="4dip"
         android:layout_weight="1"
         android:text="Column 1" />
    
      <!-- Column 2 -->
      <TextView
         android:id="@+id/tbl_txt2"
         android:layout_width="0dip"
         android:layout_height="wrap_content"
         android:background="@color/red"
         android:textColor="@color/white"
         android:padding="10dip"
         android:layout_margin="4dip"
         android:layout_weight="1"
         android:text="Column 2" />
    
      <!-- Column 3 -->
      <TextView
         android:id="@+id/tbl_txt3"
         android:layout_width="0dip"
         android:layout_height="wrap_content"
         android:background="@color/red"
         android:textColor="@color/white"
         android:padding="10dip"
         android:layout_margin="4dip"
         android:layout_weight="1"
         android:text="Column 3" />
    </TableRow>
    

    【讨论】:

      【解决方案4】:

      试试这个:测试和工作:

      1)对于 tablelayout 设置 android:stretchColumns="1"

      2)同时设置每一项(列)layout_width="0dip" and layout_weight="1"

      3)并且不要设置tablerow

      的layout_width
              <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
                  android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
                  android:paddingRight="@dimen/activity_horizontal_margin"
                  android:paddingTop="@dimen/activity_vertical_margin"
                  android:paddingBottom="@dimen/activity_vertical_margin"
                  tools:context="inext.smartshop.CustomerProfile.CustomerProfileView">
      
      
              <RelativeLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content">
      
      
                  <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/tablelayout"
                  android:stretchColumns="1"
                  android:layout_above="@+id/userProfilebtnsignout"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentStart="true"
                  android:layout_alignParentRight="true"
                  android:layout_alignParentEnd="true"
                  android:layout_below="@+id/relativeLayout">
      
                  <TableRow
      
                      android:layout_height="fill_parent"
                      android:padding="5dp"
                      android:id="@+id/detailsTableRow"
                      android:gravity="center_horizontal">
      
                      <TextView
      
                      android:layout_height="wrap_content"
                      android:textAppearance="?android:attr/textAppearanceSmall"
                      android:text="Full Name :  "
                      android:layout_width="0dip"
                      android:layout_weight="1"
                      android:id="@+id/textView8"
                      android:gravity="right" />
      
                      <TextView
                      android:layout_width="0dip"
                      android:layout_weight="1"
                      android:layout_height="wrap_content"
                      android:textAppearance="?android:attr/textAppearanceSmall"
                      android:text="Ram Chhabra"
      
                      android:id="@+id/userProfilename"
      
                      android:gravity="left" />
      
                  </TableRow>
      
                  <TableRow android:padding="5dp"
      
                      android:layout_height="wrap_content">
      
                      <TextView
                      android:layout_width="0dip"
                      android:layout_weight="1"
                      android:layout_height="wrap_content"
                      android:textAppearance="?android:attr/textAppearanceSmall"
                      android:text="Email Address :  "
      
                      android:id="@+id/textView10"
                      android:gravity="right" />
      
                      <TextView
                      android:layout_width="0dip"
                      android:layout_weight="1"
                      android:layout_height="wrap_content"
                      android:textAppearance="?android:attr/textAppearanceSmall"
                      android:text="RAMCHHABRA0012@GMAIL.COM"
                      android:id="@+id/userProfileemail"
                      android:gravity="left"
                       />
                  </TableRow>
      
                  <TableRow android:padding="5dp">
      
                      <TextView
                      android:layout_width="0dip"
                      android:layout_weight="1"
                      android:layout_height="wrap_content"
                      android:textAppearance="?android:attr/textAppearanceSmall"
                      android:text="Contact No 1 :  "
                      android:id="@+id/textView12"
      
                      android:gravity="right" />
      
                      <TextView
                      android:layout_width="0dip"
                      android:layout_weight="1"
                      android:layout_height="wrap_content"
                      android:textAppearance="?android:attr/textAppearanceSmall"
                      android:text="8130032232"
                      android:id="@+id/userProfilecontactone"
                       />
                  </TableRow>
      
      
      
                  </TableLayout>
      
              </RelativeLayout>
      
              </RelativeLayout>
      

      【讨论】:

        【解决方案5】:

        首先您需要将android:stretchColumns="*" 添加到TableLayout。

        每个 TableRow 都需要有 android:layout_weight="1" 才能平等拉伸。

        TableRow 中的每个按钮都需要有android:layout_width="0dp"

        这里有一些例子

         <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:stretchColumns="*"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
        
            <TableRow
                android:layout_height="wrap_content"
                android:layout_weight="1">
        
                <Button
                    android:id="@+id/button1"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:onClick="onButtonClick"
                    android:text="@string/button1" />
        
                <Button
                    android:id="@+id/button2"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:onClick="onButtonClick"
                    android:text="@string/button2" />
            </TableRow>
        
            <TableRow
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1">
        
                <Button
                    android:id="@+id/button3"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:onClick="onButtonClick"
                    android:text="@string/button3" />
        
                <Button
                    android:id="@+id/button4"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:onClick="onButtonClick"
                    android:text="@string/button4" />
            </TableRow>
        </TableLayout>
        

        【讨论】:

          【解决方案6】:

          布局sn-p

          <TableLayout
              android:id="@+id/tablelayout"
              android:layout_width="match_parent"
              android:layout_height="match_parent" />
          

          以编程方式设置表格中按钮布局属性的代码:

          public void addButtons(View view) {
              TableLayout tableLayout = (TableLayout) findViewById(R.id.tablelayout);
              Context context = getApplicationContext();
              tableLayout.removeAllViews();
          
              for (int rowIndex = 0; rowIndex < ROWS; rowIndex++) {
                  TableRow row = new TableRow(context);
                  for (int columnIndex = 0; columnIndex < COLUMNS; columnIndex++) {
                      Button btn = new Button(context);
                      LayoutParams buttonParams = new LayoutParams(0,
                              LayoutParams.WRAP_CONTENT, 1f);
                      btn.setLayoutParams(buttonParams);
                      row.addView(btn);
                  }
                  tableLayout.addView(row);
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2015-03-24
            • 1970-01-01
            • 2022-01-07
            • 2021-09-26
            • 2012-12-15
            • 2015-06-19
            • 1970-01-01
            • 2015-08-06
            相关资源
            最近更新 更多