问题描述
我一直在研究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
本身在中心对齐.
现在我的问题是:
- 如果我在这些假设中都正确,那么
gravity
是否适用于子组件,而layout_gravity
是否适用于父组件. - 何时选择
gravity
而不是layout_gravity
.
- If I am right in these assumptions, that whether
gravity
is for child components andlayout_gravity
is for parent component. - When to choose
gravity
overlayout_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
推荐答案
- 是的,您的理解是正确的.任何
layout_
属性都会向父视图提供指令. - 通常,如果视图占据了其父视图的整个宽度/高度,则无需指定
layout_gravity
,您可能会发现gravity
更有用.如果您的视图没有占据其父视图的整个维度,则可能需要根据所需外观对其进行对齐.
- Yes, your understanding is correct. Any
layout_
attribute gives instructions to the parent view. - 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 findgravity
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之间的确切区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!