【问题标题】:Divide the width between the table elements evenly均匀划分表格元素之间的宽度
【发布时间】:2011-04-28 16:46:30
【问题描述】:

我正在尝试制作一个表格来显示如下内容:

 ________
|__|__|__|
|__|__|__|

所有的盒子都需要平均分割,最下面一行中间的盒子会有一个非常大的图像,会被强制变成更小的尺寸。这是我当前的 XML 布局,但我最终得到了这样的布局:

 ________
|_|____|_|
|_|____|_|

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

    <TableRow>
        <TextView
            android:layout_column="1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:text="Unused"
            android:padding="3dip" />
        <TextView
            android:layout_column="2"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:text="Top Center"
            android:padding="3dip" />
        <TextView
            android:layout_column="3"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:text="Unused"
            android:padding="3dip" />
    </TableRow>

    <TableRow>
        <TextView
            android:layout_column="1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:text="----&gt;" />
        <ImageView
            android:id="@+id/mainImg"
            android:layout_column="2"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1"
        />
        <TextView
            android:layout_column="3"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:text="&lt;----" />
    </TableRow>
</TableLayout>

我做错了什么?

【问题讨论】:

    标签: android android-tablelayout


    【解决方案1】:

    我不确定你做错了什么。我过去尝试过像您这样的表格布局,但无法正常工作。我发现使用相对布局更容易完成像你这样的布局。

    您将左侧框设置为 left_align,将右侧框设置为 right_align 等。然后为它们设置一个 dip 值以与您当前使用的屏幕尺寸相对应,它们将在其他屏幕尺寸上正确更新。

    不幸的是,一旦您连续超过 3 个(例如 4 或 5 个盒子),此修复将不再有效。

    编辑: 根据 Mal 的评论,似乎另一种选择是在他的 XML 代码中使用相对布局。

    我将发布我的原始修复以供参考。我也忽略了,这实际上是框架布局中的相对布局。

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >  
    
        <Button
                android:id="@+id/start"
                android:layout_width="110dip"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentBottom="true"
            android:text="@string/title_start" />
    
        <Button
            android:id="@+id/options"
            android:layout_width="110dip"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            android:text="@string/title_options" />   
    
        <Button
            android:id="@+id/scores"
            android:layout_width="110dip"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:text="@string/title_scores" />  
    
    </RelativeLayout>
    

    【讨论】:

    • 我从表格布局切换到相对布局并且它工作,但这个答案并不完全正确。
    【解决方案2】:

    你必须使用带权重的线性布局。

    试试这个,你可能需要添加更多的东西,但它应该能让你开始。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
        <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1">
                <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
                <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
                <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
            </LinearLayout>    
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1">
                <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
                <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
                <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    </LinearLayout>
    

    【讨论】:

    【解决方案3】:

    在 RowLayout 的按钮/子元素中尝试 android:layout_weight="1"

         <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    
            <Button
                android:id="@+id/button9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/str1"  
                android:layout_weight="1"/>
    
            <Button
                android:id="@+id/button10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/str2"  
                android:layout_weight="1"/>
    
            <Button
                android:id="@+id/button11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/str3"  
                android:layout_weight="1"/>
    
            <Button
                android:id="@+id/button12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/strMinus"  
                android:layout_weight="1"/>
    
        </TableRow>
    

    【讨论】:

      【解决方案4】:

      更新:Android Percent Support Library 帮助我们使用它的百分比属性平均划分任何 android 小部件。

      Code and Concept

      Github Demo

      <android.support.percent.PercentRelativeLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
          <TextView
              android:id="@+id/fifty_huntv"
              android:layout_width="0dp"
              android:layout_height="0dp"
              android:background="#ff7acfff"
              android:text="20% - 50%"
              android:textColor="@android:color/white"
              app:layout_heightPercent="20%"
              app:layout_widthPercent="50%" />
          <TextView
              android:layout_width="0dp"
              android:layout_height="0dp"
              android:layout_toRightOf="@id/fifty_huntv"
              android:background="#ffff5566"
              android:text="80%-50%"
              app:layout_heightPercent="80%"
              app:layout_widthPercent="50%"
              />
      
      </android.support.percent.PercentRelativeLayout>
      

      真的很棒。

      【讨论】:

        猜你喜欢
        • 2019-07-12
        • 1970-01-01
        • 2011-10-01
        • 2019-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-06
        • 1970-01-01
        相关资源
        最近更新 更多