我有一個包含兩個內部佈局的主佈局。我將包含的佈局的寬度指定爲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>
注意,左右按鍵在圖像中重疊。 。如何展開包含的佈局以填充父窗口的整個寬度,以便左右兩側顯示左右按鈕。謝謝。
我可能在每個LinearLayout中有更多的組件,例如三個左鍵和三個右鍵。我提供了一個簡化的代碼示例。我必須左右分配所有內部組件嗎? – sony
是的,無論你想要他們在哪裏。這似乎更多的工作和更難理解,但恕我直言,'RelativeLayout'很容易工作,一旦你理解它。而且它更加靈活 – codeMagic
我在我的想法上添加了一條筆記給我的回答 – codeMagic