2011-09-24 171 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

相关问题