如何将LinearLayoutalignment到父级的中心?

我在这里看过很多类似的问题,但没有任何帮助。 我有不同的嵌套布局的层次结构,都有android:layout_width="fill_parent" ,最内部的布局有android:layout_width="wrap_content – 我需要在中心alignment(水平)。 ?

更新::我发现问题的原因 – 如果我把内置的LinearLayout放入RelativeLayout与android:layout_width="fill_parent" ,它仍然包装它的内容。 TableRow,但是,真的填满了屏幕。

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <TableRow > <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="1" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="2" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout> </TableRow> </TableLayout> </FrameLayout> </LinearLayout> 

这两个属性通常是混淆的:

  • android:gravity设置View的使用内容的严重性。
  • android:layout_gravity设置视图或布局相对于其父的重力。

因此,要么在LinearLayout本身上的父母或android:layout_gravity="center"

我已经把自己多次混淆起来,想知道为什么事情不能正确对中…

请尝试在您的线性布局

 android:layout_centerHorizontal="true" android:layout_centerVertical="true" 
 <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:orientation="horizontal" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="1" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="2" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout> </RelativeLayout> 

考虑在LinearLayout上包装relativeLayout。 android:layout_centerHorizontal="true"将使视图中心水平。

layout_gravity="center""center_horizontal"到父级布局。

在附注中,您的TableRowLinearLayout似乎不是必需的,因为TableRow已经是水平的LinearLayout

尝试这个:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <TableRow > <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_gravity="center_horizontal" > <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="1" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="2" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout> </TableRow> </TableLayout> </FrameLayout> </LinearLayout> 

这对我工作..添加空视图..

 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:orientation="horizontal" > <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" ads:adSize="BANNER" ads:adUnitId="@string/banner_ad_unit_id" > </com.google.android.gms.ads.AdView> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> 

尝试<TableRow android:gravity="center_horizontal">这将在<TableRow android:gravity="center_horizontal">内的LinearLayout的中心。

@jksschneider解释几乎是正确的。 确保您没有设置任何gravity到父级布局,然后将layout_gravity="center"为您的视图或布局。

在devise自定义通知时,我也面临同样的情况。 我已经尝试用true设置下列属性。

  android:layout_centerInParent. 

对于ImageView或其他视图布局重力或重力不存在使用:在孩子(我有一个相对的布局与一个ImageView作为一个孩子)的android:layout_centerInParent="true"

问题是, (的存储其他元素的主要布局)重力没有设置。 如果你改变它的中心其他元素的重力必须工作得很好。