2011-11-17 97 views
8

我正在使用布局并希望在页面的中心和底部显示视图。我正在使用以下代码,但在平板电脑上显示时,它停留在左下角不居中。如何使它成为中心。如何放置页面的底部和中心的android布局?

<LinearLayout android:layout_height="wrap_content" android:background="@color/black" 
     android:id="@+id/ll_bookmarkslistad" android:layout_width="fill_parent" android:gravity="center_horizontal|bottom" 
     android:layout_gravity="bottom|center_horizontal"> 
+0

[Android - gravity and layout_gravity]可能的重复(http://stackoverflow.com/questions/3482742/android-gravity-and-layout-gravity) –

回答

16

你需要做的是让你把一个RelativeLayout的,在这里面的LinearLayout。相对布局最好是您的根部布局,高度为fill_parent,因为它必须达到底部。

然后添加到您的LinearLayout,而不是重力:

android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" 

例子:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
     <LinearLayout 
      android:layout_height="wrap_content" android:background="@color/black" 
      android:id="@+id/ll_bookmarkslistad" android:layout_width="fill_parent" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
     > 
      <!-- stuff inside layout --> 
     </LinearLayout> 
</RelativeLayout> 
1

尝试使用RelativeLayoutandroid:layout_alignParentBottomandroid:layout_centerHorizontal

1

@pink_candy不使用任何重力的LinearLayout,如果你使用它,它会适用于LinearLayout中的所有子视图。 如果你想要任何特定的视图对齐,然后将layout_gravity参数设置为仅用于该视图,则视图可以是按钮,图像,自定义视图或甚至另一个布局。

相关问题