2012-07-30 91 views
0

我尝试显示两个窗格的UI设计。我尝试在线性布局中添加两个片段并加上一个重量。但是,线性布局似乎忽略了重量。我如何改正它?由于两个片段不能在线性布局中加权?

[链接:] http://dl.dropbox.com/u/78582670/twopanes.png

我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" > 

<fragment 
    class="com.usci.view.fragment.CatalogFragment" 
    android:id="@+id/fragment_catalog" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_weight="0.2"> 
</fragment> 

<fragment 
    class="com.usci.education.TestFragment1" 
    android:id="@+id/fragment_test" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_weight="0.8"> 
</fragment> 

</LinearLayout> 

解决方案:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" > 

<fragment 
    class="com.usci.view.fragment.CatalogFragment" 
    android:id="@+id/fragment_catalog" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="8"> 
</fragment> 

<fragment 
    class="com.usci.education.TestFragment1" 
    android:id="@+id/fragment_test" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="2"> 
</fragment> 

</LinearLayout> 
+0

尝试给重量2,8分别 – rajpara 2012-07-30 07:37:05

+0

尝试。但是,不是我想要的。事实上,我真的想重量第一个片段到8. – 2012-07-30 07:46:38

+0

然后交流它,尝试和错误的家伙。 – rajpara 2012-07-30 07:47:58

回答

4

总是,当您使用的权重,设置宽度/高度(儿童)为0px,并且喜欢用整数表示权重。

所以,解决方案是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 

<fragment 
    class="com.usci.view.fragment.CatalogFragment" 
    android:id="@+id/fragment_catalog" 
    android:layout_width="0px" 
    android:layout_height="match_parent" 
    android:layout_weight="2"> 
</fragment> 

<fragment 
    class="com.usci.education.TestFragment1" 
    android:id="@+id/fragment_test" 
    android:layout_width="0px" 
    android:layout_height="match_parent" 
    android:layout_weight="8"> 
</fragment> 

</LinearLayout> 
+0

Wow!完美地工作,谢谢 – 2012-07-30 10:04:45

+0

并非总是如此 - 我相信你可以完美地分配一个200dp的'layout_width'并设置'layout_weight'来分配剩余空间在200dp之上 – sstn 2013-02-25 12:37:04

+0

@sstn Lint警告这样的事情,不需要额外的计算,但是,也许我不理解你,或者你不明白我的答案 – 2013-02-25 19:54:56

0

与我是通过把20,80工作..也尝试0.8,0.2

<fragment 
class="com.usci.view.fragment.CatalogFragment" 
android:id="@+id/fragment_catalog" 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:layout_weight="0.8"> 
</fragment> 

<fragment 
    class="com.usci.education.TestFragment1" 
    android:id="@+id/fragment_test" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_weight="0.2"> 
</fragment> 
+0

不能工作= [... – 2012-07-30 07:47:05

0

集机器人:layout_width和机器人:layout_height值match_parent,则例如设定机器人:layout_weight(1个片段和2片段)值1

+0

两个片段分成两个相同的大小 – 2012-07-30 07:51:29

+0

是的,您需要更改片段权重,例如第一个片段组权重1和第二个片段组权重8, ) – Vladimir 2012-07-30 07:53:33