Android 在表格元素之间均匀划分宽度

Android 在表格元素之间均匀划分宽度,android,android-tablelayout,Android,Android Tablelayout,我正试图制作一个表格来显示如下内容: ________ |__|__|__| |__|__|__| ________ |_|____|_| |_|____|_| 所有框都需要均匀分割,最下面一行的中间框将有一个非常大的图像,该图像将被强制缩小。这是我当前的XML布局,但我最终的布局如下: ________ |__|__|__| |__|__|__| ________ |_|____|_| |_|____|_| 我做错了什么?我不确定你做错了什么。我以前试过像你这样的桌子布局

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

 ________
|__|__|__|
|__|__|__|
 ________
|_|____|_|
|_|____|_|
所有框都需要均匀分割,最下面一行的中间框将有一个非常大的图像,该图像将被强制缩小。这是我当前的XML布局,但我最终的布局如下:

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



我做错了什么?

我不确定你做错了什么。我以前试过像你这样的桌子布局,但没能让它正常工作。我发现使用相对布局更容易完成像您这样的布局

将左框设置为左对齐,将右框设置为右对齐,等等。然后给它们一个dip值,使其与当前使用的屏幕大小相对应,它们将在其他屏幕大小之间正确更新

不幸的是,一旦您连续超过3个(比如4或5个框),此修复就不再有效

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

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



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

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

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

在RowLayout的按钮/子元素中尝试android:layout\u 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>


更新:Android百分比支持库帮助我们使用其百分比属性平均分配任何Android小部件



真是太棒了。

我从表格布局切换到了相对布局,效果很好,但这个答案并不完全正确。这可以奏效,但性能很差。看见
<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>