I've looked through numerous similar questions here at SO, but nothing helps. I have a hierarchy of different nested layouts, all have android:layout_width="fill_parent"
, and the inner-most layout has android:layout_width="wrap_content
- I need to align it at the center (horizontally). How do I do that?
我在SO看了很多类似的问题,但没有任何帮助。我有不同嵌套布局的层次结构,都有android:layout_width =“fill_parent”,最里面的布局有android:layout_width =“wrap_content - 我需要在中心(水平)对齐它。我该怎么做?
Update:: I've found the cause of the problem - if I put the inner LinearLayout into RelativeLayout with android:layout_width="fill_parent"
, it still wraps it's content. The TableRow, however, is really filling the screen.
更新::我找到了问题的原因 - 如果我将内部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>
12 个解决方案
#1
188
These two attributes are commonly confused:
这两个属性通常是混淆的:
-
android:gravity
sets the gravity of the content of the View it's used on. - android:gravity设置它所使用的View内容的重力。
-
android:layout_gravity
sets the gravity of the View or Layout relative to its parent. - android:layout_gravity设置View或Layout相对于其父级的重力。
So either put android:gravity="center"
on the parent or android:layout_gravity="center"
on the LinearLayout itself.
所以要么将android:gravity =“center”放在父级上,要么将android:layout_gravity =“center”放在LinearLayout本身上。
I have caught myself a number of times mixing them up and wondering why things weren't centering properly...
我已经把自己混淆了很多次并且想知道为什么事情没有正确地集中......
#2
15
Please try this in your linear layout
请在线性布局中尝试此操作
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
#3
7
<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.
考虑在LinearLayout上包装relativeLayout。 android:layout_centerHorizontal =“true”将视图中心水平放置。
#4
5
add layout_gravity="center"
or "center_horizontal"
to the parent layout.
将layout_gravity =“center”或“center_horizontal”添加到父布局。
On a side note, your LinearLayout
inside your TableRow
seems un-necessary, as a TableRow
is already an horizontal LinearLayout
.
另外,TableRow中的LinearLayout似乎不是必需的,因为TableRow已经是一个水平的LinearLayout。
#5
3
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>
#6
3
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>
#7
2
Try <TableRow android:gravity="center_horizontal">
This will center the inner LinearLayout within the tablerow.
尝试
#8
1
@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.
@jksschneider解释几乎是正确的。确保没有将任何重力设置为父布局,然后将layout_gravity =“center”设置为视图或布局。
#9
1
I have faced the same situation while designing custom notification. I have tried with the following attribute set with true.
我在设计自定义通知时遇到了同样的情况。我尝试使用以下属性设置为true。
android:layout_centerInParent.
#10
1
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
所以你设计线性布局将其所有内容(TextView和Button)放在其中心,然后TextView和Button相对于线性布局的中心放置
#11
0
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).
对于ImageView或其他视图,布局重力或重力不存在使用:android:layout_centerInParent =“true”在孩子中(我有一个相对布局与作为孩子的ImageView)。
#12
0
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.
问题是未设置根(存储其他元素的主要布局)重力。如果你将它改为中心,其他元素的引力必须正常。
#1
188
These two attributes are commonly confused:
这两个属性通常是混淆的:
-
android:gravity
sets the gravity of the content of the View it's used on. - android:gravity设置它所使用的View内容的重力。
-
android:layout_gravity
sets the gravity of the View or Layout relative to its parent. - android:layout_gravity设置View或Layout相对于其父级的重力。
So either put android:gravity="center"
on the parent or android:layout_gravity="center"
on the LinearLayout itself.
所以要么将android:gravity =“center”放在父级上,要么将android:layout_gravity =“center”放在LinearLayout本身上。
I have caught myself a number of times mixing them up and wondering why things weren't centering properly...
我已经把自己混淆了很多次并且想知道为什么事情没有正确地集中......
#2
15
Please try this in your linear layout
请在线性布局中尝试此操作
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
#3
7
<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.
考虑在LinearLayout上包装relativeLayout。 android:layout_centerHorizontal =“true”将视图中心水平放置。
#4
5
add layout_gravity="center"
or "center_horizontal"
to the parent layout.
将layout_gravity =“center”或“center_horizontal”添加到父布局。
On a side note, your LinearLayout
inside your TableRow
seems un-necessary, as a TableRow
is already an horizontal LinearLayout
.
另外,TableRow中的LinearLayout似乎不是必需的,因为TableRow已经是一个水平的LinearLayout。
#5
3
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>
#6
3
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>
#7
2
Try <TableRow android:gravity="center_horizontal">
This will center the inner LinearLayout within the tablerow.
尝试
#8
1
@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.
@jksschneider解释几乎是正确的。确保没有将任何重力设置为父布局,然后将layout_gravity =“center”设置为视图或布局。
#9
1
I have faced the same situation while designing custom notification. I have tried with the following attribute set with true.
我在设计自定义通知时遇到了同样的情况。我尝试使用以下属性设置为true。
android:layout_centerInParent.
#10
1
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
所以你设计线性布局将其所有内容(TextView和Button)放在其中心,然后TextView和Button相对于线性布局的中心放置
#11
0
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).
对于ImageView或其他视图,布局重力或重力不存在使用:android:layout_centerInParent =“true”在孩子中(我有一个相对布局与作为孩子的ImageView)。
#12
0
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.
问题是未设置根(存储其他元素的主要布局)重力。如果你将它改为中心,其他元素的引力必须正常。