重力和layout_gravity之间的确切区别是什么 [英] What is exact difference between gravity and layout_gravity

查看:123
本文介绍了重力和layout_gravity之间的确切区别是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
Android-重力和layout_gravity

Possible Duplicate:
Android - gravity and layout_gravity

我一直在研究android xml.我已经使用android:layout_gravity="center"将组件对齐到特定位置.

I have been working on android xml's. I have used android:layout_gravity="center" to align components in specific position.

现在,当我在使用活动之类的对话框时,遇到了android:gravity="center".我发现在那里,gravity用于将其child对准特定位置.

Now a days, when I was working on a Dialog like Activity, I came across android:gravity="center". What I found there was, gravity was used to align its child to a specific position.

我将分享一个例子,

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="vertical" >
            <EditText
                android:id="@+id/enterNumberEdit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:hint="Enter No." >

                <requestFocus />
            </EditText>
            </LinearLayout>

它将LinearLayout的子项居中对齐.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:background="@drawable/dialog_background" >
</LinearLayout>

它使LinearLayout本身在中心对齐.

现在我的问题是:

  1. 如果我在这些假设中都正确,那么gravity是否适用于子组件,而layout_gravity是否适用于父组件.
  2. 何时选择gravity而不是layout_gravity.
  1. If I am right in these assumptions, that whether gravity is for child components and layout_gravity is for parent component.
  2. When to choose gravity over layout_gravity.

到目前为止,我已经在xml上找到了很多解释.我想听到更好的解释.让我知道,如果我在某个地方错了

I have found this much explanation working on xml's so far. I would like to hear much better explanation of it. Let me know, if I am wrong somewhere

推荐答案

  1. 是的,您的理解是正确的.任何layout_属性都会向父视图提供指令.
  2. 通常,如果视图占据了其父视图的整个宽度/高度,则无需指定layout_gravity,您可能会发现gravity更有用.如果您的视图没有占据其父视图的整个维度,则可能需要根据所需外观对其进行对齐.
  1. Yes, your understanding is correct. Any layout_ attribute gives instructions to the parent view.
  2. Generally, if a view is taking up the full width/height of its' parent, then you won't need to specify layout_gravity, you'll probably find gravity more useful. If your view does not take up an entire dimension of its' parent, you might want to align it depending on your desired look.

这篇关于重力和layout_gravity之间的确切区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆