2012-07-06 109 views
1

我的应用看起来应该是这样的: This is how it should look, though the way I had it before it wasn't scaling well. So that's why I am doing this, but it is giving me lots of problems. 这就是它应该看起来的样子,尽管它在扩展性不好之前的样子。所以这就是为什么我这样做,但它给了我很多问题。Android layout_weight不一致

但是,甚至看似相同的设置,第二行占据了所有的房间,使它看起来像这样: Any explanation why it is doing this? It beats me.

任何解释它为什么这样做呢?它击败了我。 这是我的XML,任何解决方案?

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/LinearLayout02" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:weightSum="7.0" 
> 

<LinearLayout 
    android:id="@+id/LinearLayout03" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1.0" > 

<ImageView 
    android:id="@+id/terranlogo1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
    android:src="@drawable/terranlogo" /> 

<ImageView 
    android:id="@+id/protosslogo1" 
    android:layout_width="wrap_content" 
    android:layout_weight="1.0" 
    android:layout_height="wrap_content" 
    android:src="@drawable/protosslogo" /> 

<ImageView 
    android:id="@+id/zerglogo1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
    android:src="@drawable/zerglogo" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/LinearLayout04" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1.0" > 

<CheckBox 
    android:id="@+id/ck_t1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0"/> 

<CheckBox 
    android:id="@+id/ck_p1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="25dp" 
    android:layout_weight="1.0" /> 

<CheckBox 
    android:id="@+id/ck_z1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="25dp" 
    android:layout_weight="1.0" /> 
</LinearLayout> 

    <LinearLayout 
    android:id="@+id/LinearLayout05" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" > 

<ImageView 
    android:id="@+id/terranlogo2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
    android:src="@drawable/terranlogo" /> 

<ImageView 
    android:id="@+id/protosslogo2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
    android:src="@drawable/protosslogo" /> 

<ImageView 
    android:id="@+id/zerglogo2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
    android:src="@drawable/zerglogo" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/LinearLayout06" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:orientation="horizontal" 
    android:layout_weight="1.0" > 

<CheckBox 
    android:id="@+id/ck_t2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" /> 

<CheckBox 
    android:id="@+id/ck_p2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
    android:layout_marginLeft="25dp" /> 

<CheckBox 
    android:id="@+id/ck_z2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
    android:layout_marginLeft="25dp" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/LinearLayout07" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:orientation="horizontal" 
    android:layout_weight="1.0" > 

<TextView 
    android:id="@+id/textView4" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Choose your opponets race(s) and your race(s)" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/LinearLayout08" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:orientation="horizontal" 
    android:layout_weight="1.0" > 

<TextView 
    android:id="@+id/textView3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Game Length" 
    android:layout_weight="1.0" 
    android:textSize="15dp" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:text="Mistakes Allowed" 
    android:layout_weight="1.0" 
    android:textSize="15dp" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Answer Time" 
    android:layout_weight="1.0" 
    android:textSize="15dp" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/LinearLayout09" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:orientation="horizontal" 
    android:layout_weight="1.0" > 

<Spinner 
    android:id="@+id/s_answertime" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
/> 

<Spinner 
    android:id="@+id/s_mistakenumber" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
/> 


<Spinner 
    android:id="@+id/s_gametime" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1.0" 
/> 

</LinearLayout> 
</LinearLayout> 

所有帮助表示赞赏在搞清楚了这一点 - !Lijap

+0

只是一个建议,不要给容器布局高度0dp,因为它有孩子在里面。有它“wrap_content” – Maxim 2012-07-06 21:55:16

+0

为什么权重都相等?你想让复选框与图像一样高?我不明白 - 这不是第一张图片所显示的。 – matt5784 2012-07-06 21:58:00

+0

Maxim,在子LinearLayout元素上应该将layout_height设置为0dp,因为它避免了第二遍计算布局高度。这是通过Lint工具推荐的。 – tad 2012-07-06 21:58:11

回答

3

在中间的LinearLayout高度设置为wrap_content,这是不是你想要的。将根LinearLayout的所有直接子元素设置为layout_height =“0dp”,您将获得均匀分布在高度上的所有项目。

此外,如果您打算让所有元素占据容器高度的100%,则weightSum = 7是不需要的。

查看http://ugiagonzalez.com/2012/01/19/android-linearlayout-distribution-explained-weight-and-sizes/了解更多信息。

+1

谢谢!不能相信我没看到那个wrap_content。 – Lijap 2012-07-06 22:14:18