如何将 LinearLayout 对齐到其父版本的中心?

我在这里看过很多类似的问题,但没有任何帮助。 我有一个不同的嵌套布局层次结构,都有 android:layout_width="fill_parent",和最内层的布局有 android:layout_width="wrap_content-我需要对齐它在中心(水平)。我该怎么做?

更新: : 我已经找到了问题的原因——如果我使用 android:layout_width="fill_parent"将内部 LinearLayout 放入 RelativeLayout,它仍然会包装它的内容。然而,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>
176729 次浏览

These two attributes are commonly confused:

  • android:gravity sets the gravity of the content of the View it's used on.
  • android:layout_gravity sets the gravity of the View or Layout relative to its parent.

So either put android:gravity="center" on the parent or android:layout_gravity="center" on the LinearLayout itself.

I have caught myself a number of times mixing them up and wondering why things weren't centering properly...

<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>

consider wrapping relativeLayout over LinearLayout. android:layout_centerHorizontal="true" will position the view center horizontal.

Try this:

<?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>

add layout_gravity="center" or "center_horizontal" to the parent layout.

On a side note, your LinearLayout inside your TableRow seems un-necessary, as a TableRow is already an horizontal LinearLayout.

@jksschneider explation is almost right. Make sure that you haven't set any gravity to parent layout, and then set layout_gravity="center" to your view or layout.

Try <TableRow android:gravity="center_horizontal"> This will center the inner LinearLayout within the tablerow.

Please try this in your linear layout

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

I have faced the same situation while designing custom notification. I have tried with the following attribute set with true.

 android:layout_centerInParent.

This worked for me.. adding empty view ..

<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>

for ImageView or others views who layout-gravity or gravity does not exist use : android:layout_centerInParent="true" in the child (I had a relative layout with an ImageView as a child).

The problem is that the root's (the main layout you store the other elements) gravity is not set. If you change it to center the other elements' gravity must work just fine.

this worked for me.

<LinearLayout>
.
.
.
android:gravity="center"
.
.>


<TextView
android:layout_gravity = "center"
/>
<Button
android:layout_gravity="center"
/>


</LinearLayout>

so you're designing the Linear Layout to place all its contents(TextView and Button) in its center and then the TextView and Button are placed relative to the center of the Linear Layout

Below is code to put your Linearlayout at bottom and put its content at center. You have to use RelativeLayout to set Layout at bottom.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#97611F"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:orientation="horizontal">


<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="View Saved Searches"
android:textSize="15dp"
android:textColor="#fff"
android:gravity="center_vertical"
android:visibility="visible" />
<TextView
android:layout_width="30dp"
android:layout_height="24dp"
android:text="12"
android:textSize="12dp"
android:textColor="#fff"
android:gravity="center_horizontal"
android:padding="1dp"
android:visibility="visible" />
</LinearLayout>
</RelativeLayout>

I experienced this while working with nested RelativeLayouts. My solution ended up being simple, but it's a little gotcha that's worth being aware of.

My layout was defined with...

android:layout_height="fill_parent"
android:layout_below="@id/someLayout
android:gravity="center"

...however, I didn't think that the layout below this one was placed on top of it. Everything inside this layout was shifted towards the bottom instead of centered. Adding...

android:layout_above="@id/bottomLayout"

...fixed my issue.

Try this:

android:layout_gravity="center_horizontal"

If your parent is a LinearLayout, you can add to it :

android:orientation="vertical"
android:gravity="center"

And to it's child

android:gravity="center"

It should at least center horizontally !