尝试让Android LinearLayout水平和垂直拉伸

尝试让Android LinearLayout水平和垂直拉伸,android,xml,layout,android-linearlayout,stretch,Android,Xml,Layout,Android Linearlayout,Stretch,更新:我很感激使用GridView的建议,我相信这会提供更好的性能。然而,我最初实现简单GridView的努力导致了可耻的失败。我想找出我做错了什么是为了另一个话题。为了修复这个实现,我使用OnWindowFocusChanged来查找ANDROID_内容高度,然后除以行数来设置行高度。关于这个问题的许多讨论中没有提到的主要教训是OnCreate是设置维度的错误位置。OnWindowFocusChanged发生在可以进行精确测量之后。这一信息在关于这个话题的许多讨论中并不经常提及 <

更新:我很感激使用GridView的建议,我相信这会提供更好的性能。然而,我最初实现简单GridView的努力导致了可耻的失败。我想找出我做错了什么是为了另一个话题。为了修复这个实现,我使用OnWindowFocusChanged来查找ANDROID_内容高度,然后除以行数来设置行高度。关于这个问题的许多讨论中没有提到的主要教训是OnCreate是设置维度的错误位置。OnWindowFocusChanged发生在可以进行精确测量之后。这一信息在关于这个话题的许多讨论中并不经常提及

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
<!-- second Row -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />

    <ImageView
        android:id="@+id/iv_clm2"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm3"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
原件: 我正在开发一个Android布局,试图建立一个三列四行的板

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
<!-- second Row -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />

    <ImageView
        android:id="@+id/iv_clm2"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm3"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
我想拉伸电路板以垂直和水平填充可用屏幕。我尝试使用
weight=“1”
选项按行和列嵌套LinearLayout,但只能使其水平拉伸

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
<!-- second Row -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />

    <ImageView
        android:id="@+id/iv_clm2"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm3"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
在以下配置中,行拉伸以填充屏幕宽度,但列设置为“60dp”。我也尝试过GridLayout和TableLayout,但都没有达到预期的效果

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft = "10dp"
        android:paddingRight = "10dp"
        android:background="@drawable/bg" >
        <LinearLayout
            android:id="@+id/player1"
            android:layout_width="match_parent"
            android:layout_height="@dimen/positionHeight"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal"
            android:background="@drawable/square" />
        <LinearLayout
            android:id="@+id/player2"
            android:layout_width="match_parent"
            android:layout_height="@dimen/positionHeight"
            android:layout_alignParentTop="true"
            android:orientation="horizontal"
            android:background="@drawable/square" />    
        <LinearLayout 
          android:id="@+id/row0"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"  
          android:layout_below="@id/player2"
          android:layout_alignParentLeft="true" 
          android:baselineAligned="false"
          android:orientation="horizontal">
          <LinearLayout
            android:id="@+id/position0"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="@dimen/positionHeight"
            android:orientation="horizontal"
            android:background="@drawable/square" />
          <LinearLayout
            android:id="@+id/position1"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="@dimen/positionHeight"
            android:orientation="horizontal"
            android:background="@drawable/square" /> 
          <LinearLayout
            android:id="@+id/position2"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="@dimen/positionHeight"
            android:orientation="horizontal"
            android:background="@drawable/square" />
        </LinearLayout>  
        <LinearLayout 
          android:id="@+id/row1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" 
          android:layout_below="@id/row0"
          android:layout_alignParentLeft="true"
          android:baselineAligned="false"
          android:orientation="horizontal">    
          <LinearLayout
            android:id="@+id/position3"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="@dimen/positionHeight"
            android:orientation="horizontal"
            android:background="@drawable/square" /> 
          <LinearLayout
            android:id="@+id/position4"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="@dimen/positionHeight"
            android:orientation="horizontal"
            android:background="@drawable/square" />
          <LinearLayout
            android:id="@+id/position5"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="@dimen/positionHeight"
            android:orientation="horizontal"
            android:background="@drawable/square" />
        </LinearLayout>      
        <LinearLayout 
          android:id="@+id/row2"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" 
          android:layout_below="@id/row1"
          android:layout_alignParentLeft="true" 
          android:baselineAligned="false"
          android:orientation="horizontal"> 
          <LinearLayout
            android:id="@+id/position6"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="@dimen/positionHeight"
            android:orientation="horizontal"
            android:background="@drawable/square" />
          <LinearLayout
            android:id="@+id/position7"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="@dimen/positionHeight"
            android:orientation="horizontal"
            android:background="@drawable/square" />
          <LinearLayout
            android:id="@+id/position8"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="@dimen/positionHeight"
            android:orientation="horizontal"
            android:background="@drawable/square" />
        </LinearLayout>      
        <LinearLayout 
          android:id="@+id/row3"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" 
          android:layout_below="@id/row2"
          android:layout_alignParentLeft="true" 
          android:baselineAligned="false"
          android:orientation="horizontal">   
          <LinearLayout
            android:id="@+id/position9"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="@dimen/positionHeight"
            android:orientation="horizontal"
            android:background="@drawable/square" />    
          <LinearLayout
            android:id="@+id/position10"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="@dimen/positionHeight"
            android:orientation="horizontal"
            android:background="@drawable/square" />
          <LinearLayout
            android:id="@+id/position11"
           android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="@dimen/positionHeight"
            android:orientation="horizontal"
            android:background="@drawable/square" />
        </LinearLayout>
    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
<!-- second Row -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />

    <ImageView
        android:id="@+id/iv_clm2"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm3"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>

请给我提个建议。 你必须试试这个。
    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
<!-- second Row -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />

    <ImageView
        android:id="@+id/iv_clm2"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm3"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
<!-- second Row -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />

    <ImageView
        android:id="@+id/iv_clm2"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm3"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>


一般来说,使用这么多布局不是一个好主意。您应该改用
TableLayout
GridLayout

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
<!-- second Row -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />

    <ImageView
        android:id="@+id/iv_clm2"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm3"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
网格布局:

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
<!-- second Row -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />

    <ImageView
        android:id="@+id/iv_clm2"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm3"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>

表格布局:

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
<!-- second Row -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />

    <ImageView
        android:id="@+id/iv_clm2"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm3"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>

正如我在上面的更新中提到的,这里真正的技巧是在OnCreate之外的其他地方设置维度。我将player1、player2和position[]初始化为xml中的LinearLayout元素,然后在onWindowFocusChanged中调整它们的高度以适应。不像这里其他人建议的GridView那么好,但它确实可以让我工作

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
<!-- second Row -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />

    <ImageView
        android:id="@+id/iv_clm2"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm3"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
在以下人员的帮助下:

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
<!-- second Row -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />

    <ImageView
        android:id="@+id/iv_clm2"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm3"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
@覆盖
WindowFocusChanged上的公共无效(布尔hasFocus){
super.onWindowFocusChanged(hasFocus);
View content=getWindow().findViewById(Window.ID\u ANDROID\u content);
player1.getLayoutParams().height=content.getHeight()/6;
player2.getLayoutParams().height=content.getHeight()/6;

对于(int i=0;i我不同意其他人说必须使用GridLayout。请使用LinearLayout尝试这种方法。我对网格的每个单元格使用了通用视图,但任何控件都可以使用

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
<!-- second Row -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />

    <ImageView
        android:id="@+id/iv_clm2"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm3"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/red" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/red" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/red" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/red" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/red" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/red" />

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
<!-- second Row -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />

    <ImageView
        android:id="@+id/iv_clm2"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm3"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>

这种方法也不需要手动操作代码中的布局,所有内容都是使用权重在xml中处理的。

我是否遗漏了一些xml?似乎顶部不在那里。我想我明白你在做什么了。当我像这样嵌套权重时,我会得到一个警告。在你看来,如果只有y 2层?@smirciat这是一个技巧。但是你必须始终使用Griedview来处理这种类型的视图。我尝试了一些TableLayout和GridLayout示例,但无法在水平和垂直方向上填充整个可用区域。我想你应该发布与GridLayout相关的问题,而不是那么多线性布局。什么是问题出在哪里?请查看我在第三个教程中更新的答案,这是一个关于如何使用GridView的好例子。希望它能帮助您看到lint警告“嵌套权重对性能有害”,这可能是真的,但请自己测试一下,我想你不会注意到这么简单的布局会有任何性能问题。这正是我一开始试图做的。我会尝试一下。我同意我的解决方案是“hacky”但是效果很好!为了学习,我需要尝试一下你的解决方案。是的,起先那个皮棉警告确实吓跑了我,但我还是会尝试。我个人一直都在使用嵌套权重,即使在一个布局上有50多个视图,也没有看到任何负面影响。虽然这可能有效,但在我看来,这是相当粗糙的,请看一看在我的回答中,只要在xml布局中适当地使用权重,就可以得到所需的结果。我对你的回答发表了评论,我同意你的看法。我真正想向任何搜索这个问题答案的人强调的一点是,许多像我这样的新手尝试在活动中从onCreate设置屏幕尺寸。事实上,这是一个d的想法,但将在以后的工作是没有提到的帮助论坛经常不够。
    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>
<!-- second Row -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv_clm1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />

    <ImageView
        android:id="@+id/iv_clm2"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/yellow" />

    <ImageView
        android:id="@+id/iv_clm3"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/red" />
</LinearLayout>