2011-09-24 170 views
0

我在一個LinearLayout內有兩個不同的LinearLayouts,我希望第一個佈局位於頂部,第二個佈局位於底部。我嘗試了android:gravity="top",但沒有奏效。這裏是我的XML代碼:在屏幕頂部和底部的LinearLayout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent" android:gravity="center"> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:gravity="top"> 
    <ImageView android:layout_height="wrap_content" android:id="@+id/imageView1" 
     android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView> 
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" android:layout_height="wrap_content"> 
     <TableRow android:weightSum="2"> 
      <Button android:text="Button" android:id="@+id/button2" 
       android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:layout_weight="1"></Button> 
      <Button android:text="Button" android:id="@+id/button1" 
       android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:layout_weight="1"></Button> 
     </TableRow> 
     <TableRow android:weightSum="2"> 
      <Button android:text="Button" android:id="@+id/button3" 
       android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:layout_weight="1"></Button> 
      <Button android:text="Button" android:id="@+id/button4" 
       android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:layout_weight="1"></Button> 
     </TableRow> 
    </TableLayout> 
</LinearLayout> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:gravity="bottom"> 
    <ImageView android:layout_height="wrap_content" android:id="@+id/imageView1" 
     android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView> 
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" android:layout_height="wrap_content"> 
     <TableRow android:weightSum="2"> 
      <Button android:text="Button" android:id="@+id/button2" 
       android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:layout_weight="1"></Button> 
      <Button android:text="Button" android:id="@+id/button1" 
       android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:layout_weight="1"></Button> 
     </TableRow> 
     <TableRow android:weightSum="2"> 
      <Button android:text="Button" android:id="@+id/button3" 
       android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:layout_weight="1"></Button> 
      <Button android:text="Button" android:id="@+id/button4" 
       android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:layout_weight="1"></Button> 
     </TableRow> 
    </TableLayout> 
</LinearLayout> 

+0

訪問此問題http://stackoverflow.com/questions/4675796/layout-problem-how-to-place-something-on-top-and-bottom – kehnar

回答

1

你可以簡單地添加:

<LinearLayout android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:layout_weight="1" /> 
您的頂部和底部之間 LinearLayout

(並從父容器的重力)。

+0

謝謝。現在它可以工作 – Alex

1

改變你的父佈局RelativeLayout代替LinearLayout

父佈局組的頂部子佈局

android:layout_alignParentTop="true" 

與父佈局組的底部繼子佈局

以下
android:layout_alignParentBottom="true" 
+0

這種方法適用於我。 – boosth

相關問題