我已经经历了许多类似的问题,看起来在这里左右,但没有什么帮助。
我有不同的嵌套布局的层次结构,都具有机器人:layout_width =FILL_PARENT
,而最里面的布局有机器人:layout_width = WRAP_CONTENT
- 我需要在中心(水平)我如何做到这一点。
更新::我已经找到了问题的原因 - 如果我把内心的LinearLayout到RelativeLayout的使用机器人:layout_width =FILL_PARENT
,它仍然包裹它的内容。本的TableRow,但是,真正填补了屏幕。
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>
<的FrameLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT>
< TableLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT>
<的TableRow>
<的LinearLayout
机器人:方向=横向
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT>
<的TextView
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:文本=1
机器人:textAppearance =:/>中的Android ATTR / textAppearanceLarge?
<的TextView
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =WRAP_CONTENT
机器人:文本=2
机器人:textAppearance =:/>中的Android ATTR / textAppearanceLarge?
< / LinearLayout中>
< /的TableRow>
< / TableLayout>
< /的FrameLayout>
< / LinearLayout中>
解决方案
这两个属性都经常混淆的:
安卓重力
设置查看它的内容的严重性
使用上。
安卓layout_gravity
设置视图的重力或
布局的相对于其父的
所以,要么把安卓重力=中心
在父母或安卓layout_gravity =中心
上的LinearLayout本身。
我捉住了自己数次把它们组合起来,不知道为什么事情没有妥善中心...
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?
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.
<?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>
解决方案
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...