2015-04-01 45 views
0

我有2個LinearLayouts重量1,具有1佈局,如果我添加任何東西佔據,而不是半個屏幕全寬,如何避免這種如何給2線之間的高度相等佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:orientation="vertical" 
 
    android:paddingBottom="@dimen/activity_vertical_margin" 
 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
 
    android:paddingRight="@dimen/activity_horizontal_margin" 
 
    android:paddingTop="@dimen/activity_vertical_margin" 
 
    android:weightSum="2" 
 
    tools:context="com.example.testapp1.MainActivity" > 
 

 
    <LinearLayout 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:layout_weight="1" 
 
     android:background="@color/Aqua" 
 
     android:orientation="vertical" 
 
     android:weightSum="2" > 
 

 
     <LinearLayout 
 
      android:id="@+id/subview_1" 
 
      android:layout_width="match_parent" 
 
      android:layout_height="wrap_content" 
 
      android:background="@color/Green" 
 
      android:orientation="vertical" > 
 

 
      <RelativeLayout 
 
       android:id="@+id/insideLayout" 
 
       android:layout_width="match_parent" 
 
       android:layout_height="wrap_content" 
 
       android:background="@color/Yellow" > 
 

 
       <LinearLayout 
 
        android:layout_width="wrap_content" 
 
        android:layout_height="wrap_content" 
 
        android:background="@color/Aqua" 
 
        android:orientation="vertical" > 
 

 
        <View 
 
         android:layout_width="wrap_content" 
 
         android:layout_height="wrap_content" 
 
         android:background="@color/DarkGray" /> 
 
       </LinearLayout> 
 
      </RelativeLayout> 
 
     </LinearLayout> 
 
    </LinearLayout> 
 

 
    <LinearLayout 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:layout_weight="1" 
 
     android:background="@color/BlueViolet" 
 
     android:orientation="vertical" > 
 
    </LinearLayout> 
 

 
</LinearLayout>

當添加視圖標籤,它佔據整個寬度,而不是爲重量1規定的一半,有什麼問題我想提出

+0

我覺得既然你的方向是垂直的,線性佈局的layout_width是匹配的父母它佔據了完整的寬度。如果希望它佔據寬度的一半,則爲layout_width指定一些大小而不是給予匹配父項。 – Keshav1234 2015-04-01 08:14:17

+0

不清楚,你可以請更具體嗎?哪些襯墊布需要50 50? – 2015-04-01 08:45:14

+0

我試圖將2個佈局與50/50高度,我試圖通過使用重量爲1,但如果我添加任何視圖的佈局之一,它佔用全寬,而不是寬度的一半,我正在明確你大衛。 – Rakesh 2015-04-01 09:21:20

回答

3

讓我們來簡化你的問題。以下應該工作。我建議將weight想象成100的百分比,所以如果你想得到一半,那麼就是50,50(如果你想要它的三分之一33,33,33)。不需要重量已被刪除。您錯過了0diplayout_height這就是允許weight定義所採用的空間。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.testapp1.MainActivity" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="50" 
     android:orientation="vertical"> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="50" 
     android:orientation="vertical" > 

    </LinearLayout> 

</LinearLayout> 
+0

謝謝,如何在相對佈局中實現類似的分區? – Rakesh 2015-04-01 10:12:17

+0

它在實現它方面幫了很大忙,謝謝blundell – Rakesh 2015-04-01 12:56:35

相關問題