2013-04-13 38 views
0

我有一個包含兩個內部佈局的主佈局。我將包含的佈局的寬度指定爲match_parent,但它們不佔用父項的整個寬度。Android:包含不填充父寬度的佈局

我可能在每個LinearLayout的內部有更多的組件,例如三個左鍵和三個右鍵。下面是我的代碼

參見下面的例子一個簡單的例子: main_layout.xml

<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:paddingTop="5dp" 
    android:baselineAligned="false" 
    android:orientation="vertical" > 

    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.95" > 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_weight="0.8" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" > 

      <include layout="@layout/child_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_weight="0.2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" > 

      <include layout="@layout/child_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 
     </TableRow> 
    </TableLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_weight="0.05" 
     android:gravity="center" 
     android:orientation="vertical"> 

     <Button 
      android:layout_width="80dp" 
      android:layout_height="40dp" 
      android:textSize="14sp" 
      android:textStyle="bold" 
      android:text="Bottom" /> 
    </LinearLayout> 

</LinearLayout> 

child_layout

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:id="@+id/leftLayout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:orientation="vertical" > 

     <Button 
      android:layout_width="80dp" 
      android:layout_height="40dp" 
      android:textSize="14sp" 
      android:textStyle="bold" 
      android:text="Left" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/rightLayout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:orientation="vertical" > 

     <Button 
      android:layout_width="80dp" 
      android:layout_height="40dp" 
      android:textSize="14sp" 
      android:textStyle="bold" 
      android:text="Right" /> 
    </LinearLayout> 

</RelativeLayout> 

注意,左右按鍵在圖像中重疊。 enter image description here。如何展開包含的佈局以填充父窗口的整個寬度,以便左右兩側顯示左右按鈕。謝謝。

回答

0

在你childlayout,拿出你的LinearLayouts然後分配給左和家長

<Button 
     android:layout_width="80dp" 
     android:layout_height="40dp" 
     android:textSize="14sp" 
     android:textStyle="bold" 
     android:text="Left" 
     android:layout_alignParntLeft="true" /> 


    <Button 
     android:layout_width="80dp" 
     android:layout_height="40dp" 
     android:textSize="14sp" 
     android:textStyle="bold" 
     android:text="Right" 
     android:layout_alignParntRight="true"/> 

的吧?如果我知道你想那麼這應該工作的。這將讓他們在左,右,但取決於如果你想讓他們在同一「行」或高於或低於對方再有其他屬性可以設置

RelativeLayout

LinearLayout可以很容易和有用的需要的時候,但使用RelativeLayout可以時常消除嵌套LinearLayout S和無用ViewGroup小號

+0

我可能在每個LinearLayout中有更多的組件,例如三個左鍵和三個右鍵。我提供了一個簡化的代碼示例。我必須左右分配所有內部組件嗎? – sony

+0

是的,無論你想要他們在哪裏。這似乎更多的工作和更難理解,但恕我直言,'RelativeLayout'很容易工作,一旦你理解它。而且它更加靈活 – codeMagic

+0

我在我的想法上添加了一條筆記給我的回答 – codeMagic

0

您也可以擺脫一些LinearLayouts和RelativeLayout的,做這個:

<?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="wrap_content" 
    android:orientation="horizontal" > 

    <Button 
     android:layout_width="80dp" 
     android:layout_height="40dp" 
     android:textSize="14sp" 
     android:textStyle="bold" 
     android:text="Left" /> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="vertical" /> 

    <Button 
     android:layout_width="80dp" 
     android:layout_height="40dp" 
     android:textSize="14sp" 
     android:textStyle="bold" 
     android:text="Right" /> 
</LinearLayout>