Android 中的gravity 和layout_gravity 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3482742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between gravity and layout_gravity in Android?
提问by Paresh Mayani
I know we can set the following values to the android:gravity
and android:layout_gravity
properties:
我知道我们可以为android:gravity
和 android:layout_gravity
属性设置以下值:
center
center_vertical
center_horizontal
, etc.
center
center_vertical
center_horizontal
, 等等。
But I am confused regarding both of these.
但我对这两者感到困惑。
What is the difference between the usage of android:gravity
and android:layout_gravity
?
android:gravity
和的用法有什么区别android:layout_gravity
?
回答by Sephy
Their names should help you:
他们的名字应该可以帮助你:
android:gravity
sets the gravity of the contents (i.e. its subviews) of theView
it's used on.android:layout_gravity
sets the gravity of theView
orLayout
relative to its parent.
android:gravity
设置View
它所使用的内容(即它的子视图)的重力。android:layout_gravity
设置View
或Layout
相对于其父项的重力。
And an example is here.
一个例子是here。
回答by Suragch
gravity
arranges the content insidethe view.layout_gravity
arranges the view's position outsideof itself.
gravity
排列视图内的内容。layout_gravity
将视图的位置排列在其自身之外。
Sometimes it helps to see a picture, too. The green and blue are TextViews
and the other two background colors are LinearLayouts
.
有时看图片也有帮助。绿色和蓝色是TextViews
,其他两种背景颜色是LinearLayouts
。
Notes
笔记
- The
layout_gravity
does not work for views in aRelativeLayout
. Use it for views in aLinearLayout
orFrameLayout
. See my supplemental answerfor more details. - The view's width (or height) has to be greater than its content. Otherwise
gravity
won't have any effect. Thus,wrap_content
andgravity
are meaningless together. - The view's width (or height) has to be less than the parent. Otherwise
layout_gravity
won't have any effect. Thus,match_parent
andlayout_gravity
are meaningless together. - The
layout_gravity=center
looks the same aslayout_gravity=center_horizontal
here because they are in a vertical linear layout. You can't center vertically in this case, solayout_gravity=center
only centers horizontally. - This answer only dealt with setting
gravity
andlayout_gravity
on the views within a layout. To see what happens when you set thegravity
of the of the parent layout itself, check out the supplemental answerthat I referred to above. (Summary:gravity
doesn't work well on aRelativeLayout
but can be useful with aLinearLayout
.)
- 在
layout_gravity
不适合的意见在工作RelativeLayout
。将其用于 aLinearLayout
或 中的视图FrameLayout
。有关更多详细信息,请参阅我的补充答案。 - 视图的宽度(或高度)必须大于其内容。否则
gravity
不会有任何影响。因此,wrap_content
和gravity
在一起是毫无意义的。 - 视图的宽度(或高度)必须小于父视图。否则
layout_gravity
不会有任何影响。因此,match_parent
和layout_gravity
在一起是毫无意义的。 - 在
layout_gravity=center
外观一样layout_gravity=center_horizontal
,因为他们是在一个垂直线性布局在这里。在这种情况下你不能垂直居中,所以layout_gravity=center
只能水平居中。 - 该答案仅涉及布局中的设置
gravity
和layout_gravity
视图。要查看设置gravity
父布局本身的时会发生什么,请查看我上面提到的补充答案。(总结:gravity
在 a 上效果不佳,RelativeLayout
但在LinearLayout
. 上可能有用。)
So remember, layout_gravity arranges a view in its layout. Gravity arranges the content inside the view.
所以请记住,layout_gravity 在它的layout 中安排了一个视图。Gravity 在视图中排列内容。
xml
xml
Here is the xml for the above image for your reference:
这是上图的xml供您参考:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="0dp"
android:layout_weight="1"
android:background="#e3e2ad"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="24sp"
android:text="gravity=" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="left"
android:text="left" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center_horizontal"
android:text="center_horizontal" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#bcf5b1"
android:gravity="right"
android:text="right" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:background="#aacaff"
android:gravity="center"
android:text="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#d6c6cd"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="24sp"
android:text="layout_gravity=" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="left"
android:background="#bcf5b1"
android:text="left" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:background="#aacaff"
android:text="center_horizontal" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="right"
android:background="#bcf5b1"
android:text="right" />
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="#aacaff"
android:text="center" />
</LinearLayout>
</LinearLayout>
Related
有关的
回答by Vishnu Haridas
The difference
区别
android:layout_gravity
is the Outsidegravity of the View. Specifies the direction in which the View should touch its parent's border.
android:layout_gravity
是视图的外部重力。指定视图应该接触其父边框的方向。
android:gravity
is the Insidegravity of that View. Specifies in which direction its contents should align.
android:gravity
是该视图的内部重力。指定其内容应在哪个方向对齐。
HTML/CSS Equivalents
HTML/CSS 等效项
Android | CSS
————————————————————————+————————————
android:layout_gravity | float
android:gravity | text-align
Easy trick to help you remember
帮助你记住的简单技巧
Take layout-gravity
as "Lay-outside-gravity".
取layout-gravity
为“外置重力”。
回答by Ying
Short Answer: use android:gravity
or setGravity()
to control gravity of all child views of a container; use android:layout_gravity
or setLayoutParams()
to control gravity of an individual view in a container.
简答:使用android:gravity
或setGravity()
控制容器所有子视图的重力;使用android:layout_gravity
或setLayoutParams()
控制容器中单个视图的重力。
Long story: to control gravity in a linear layout container such as LinearLayout
or RadioGroup
, there are two approaches:
长话短说:要在线性布局容器(例如LinearLayout
或 )中控制重力RadioGroup
,有两种方法:
1) To control the gravity of ALL child views of a LinearLayout
container (as you did in your book), use android:gravity
(not android:layout_gravity
) in layout XML file or setGravity()
method in code.
1) 要控制LinearLayout
容器的所有子视图的重力(如您在书中所做的那样),请在布局 XML 文件或代码中的方法中使用android:gravity
(not android:layout_gravity
) setGravity()
。
2) To control the gravity of a child view in its container, use android:layout_gravity
XML attribute. In code, one needs to get the LinearLayout.LayoutParams
of the view and set its gravity. Here is a code example that set a button to bottom in a horizontally oriented container:
2) 要控制子视图在其容器中的重力,请使用android:layout_gravity
XML 属性。在代码中,需要获取LinearLayout.LayoutParams
视图的 并设置其重力。下面是一个代码示例,它在水平方向的容器中将按钮设置为底部:
import android.widget.LinearLayout.LayoutParams;
import android.view.Gravity;
...
Button button = (Button) findViewById(R.id.MyButtonId);
// need to cast to LinearLayout.LayoutParams to access the gravity field
LayoutParams params = (LayoutParams)button.getLayoutParams();
params.gravity = Gravity.BOTTOM;
button.setLayoutParams(params);
For horizontal LinearLayout
container, the horizontal gravity of its child view is left-aligned one after another and cannot be changed. Setting android:layout_gravity
to center_horizontal
has no effect. The default vertical gravity is center (or center_vertical) and can be changed to top or bottom. Actually the default layout_gravity
value is -1
but Android put it center vertically.
对于水平LinearLayout
容器,其子视图的水平重力是左对齐的,无法更改。设置android:layout_gravity
为center_horizontal
无效。默认垂直重力为 center(或 center_vertical),可以更改为顶部或底部。实际上默认layout_gravity
值是-1
Android 把它垂直居中。
To change the horizontal positions of child views in a horizontal linear container, one can use layout_weight
, margin and padding of the child view.
要更改水平线性容器中子视图的水平位置,可以使用layout_weight
子视图的 、边距和填充。
Similarly, for vertical View Group container, the vertical gravity of its child view is top-aligned one below another and cannot be changed. The default horizontal gravity is center (or center_horizontal
) and can be changed to left or right.
同样,对于垂直视图组容器,其子视图的垂直重力是上下对齐的,并且无法更改。默认水平重力为中心(或center_horizontal
),可以更改为向左或向右。
Actually, a child view such as a button also has android:gravity
XML attribute and the setGravity()
method to control its child views -- the text in it. The Button.setGravity(int)
is linked to this developer.android.com entry.
实际上,诸如按钮之类的子视图也具有android:gravity
XML 属性和setGravity()
控制其子视图的方法——其中的文本。将Button.setGravity(int)
被链接到这个developer.android.com条目。
回答by matto1990
From what I can gather layout_gravityis the gravity of that view inside its parent, and gravityis the gravity of the children inside that view.
从我可以收集layout_gravity是其母公司内部的视图的重力和重力是视图内孩子们的重力。
I think this is right but the best way to find out is to play around.
我认为这是正确的,但找出答案的最佳方法是四处游玩。
回答by Akshay Paliwal
Look at the image to be clear about gravity
看图看重力
回答by Abhishek Tamta
If a we want to set the gravity of content inside a view then we will use "android:gravity", and if we want to set the gravity of this view (as a whole) within its parent view then we will use "android:layout_gravity".
如果我们想在视图中设置内容的重力,那么我们将使用“android:gravity”,如果我们想在其父视图中设置这个视图(作为一个整体)的重力,那么我们将使用“android:gravity”。布局_重力”。
回答by mszaro
Just thought I'd add my own explanation here - coming from a background on iOS, this is how I've internalized the two in iOS terms: "Layout Gravity" affects your position in the superview. "Gravity" affects the position of your subviews within you. Said another way, Layout Gravity positions you yourself while gravity positions your children.
只是想在这里添加我自己的解释 - 来自 iOS 的背景,这就是我在 iOS 术语中内化这两者的方式:“布局重力”影响您在超级视图中的位置。“重力”会影响子视图在您体内的位置。换句话说,Layout Gravity 让你自己定位,而 Gravity 定位你的孩子。
回答by sushildlh
There is many difference in the gravity
and layout-gravity
. I am going to explain my experience about these 2 concepts(All information i got due to my observation and some websites).
有一个在许多差异gravity
和layout-gravity
。我将解释我对这两个概念的体验(我通过观察和一些网站获得的所有信息)。
Use Of Gravity and Layout-gravity in
FrameLayout
.....
在
FrameLayout
..... 中使用 Gravity 和 Layout-gravity
Note:-
笔记:-
Gravityis used inside the View Contentas some User have answer and it is same for all
ViewGroup Layout
.Layout-gravity
is used with the parent View as some User have answer.Gravity and Layout-gravity
is work more usefulwith theFrameLayout
childs .We can't use Gravity and Layout-gravity
in FrameLayout's Tag ....We can set Child View any where in the
FrameLayout
usinglayout-gravity
.We can use every single value of gravity inside the FrameLayout (eg:-
center_vertical
,center_horizontal
,center
,top
, etc), but it is not possible with other ViewGroup Layouts .FrameLayout
fully working onLayout-gravity
. Example:- If you work onFrameLayout
then you don't need to change whole Layoutfor adding new View. You just add Viewas last in theFrameLayout
and give himLayout-gravity
with value.(This is adavantages of layout-gravity with FrameLayout).
重力在视图内容中使用,因为某些用户有答案,并且对所有人都是一样的
ViewGroup Layout
。Layout-gravity
与父视图一起使用,因为某些用户有答案。Gravity and Layout-gravity
是工作更有用与FrameLayout
孩子的。We can't use Gravity and Layout-gravity
在 FrameLayout 的标签中......我们可以在
FrameLayout
using 中的任何位置设置子视图layout-gravity
。我们可以使用的FrameLayout内重力的每一个值(例如: - ,
center_vertical
,center_horizontal
,center
,top
等),但它是不可能与其他的ViewGroup布局。FrameLayout
全力以赴Layout-gravity
。示例:- 如果您继续工作,FrameLayout
则无需更改整个布局即可添加新视图。您只需在最后添加 ViewFrameLayout
并给他Layout-gravity
值。(这是 FrameLayout 布局重力的优点)。
have look on example ......
看看例子......
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#264bd1"
android:gravity="center"
android:layout_gravity="center"
android:text="Center Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#1b64b9"
android:gravity="bottom"
android:layout_gravity="bottom|center"
android:text="Bottom Layout Gravity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#d75d1c"
android:gravity="top"
android:layout_gravity="top|center"
android:text="Top Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginTop="100dp"
android:textColor="#d71f1c"
android:gravity="top|right"
android:layout_gravity="top|right"
android:text="Top Right Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginBottom="100dp"
android:textColor="#d71cb2"
android:layout_gravity="bottom"
android:gravity="bottom"
android:text="Top Left Layout Gravity"/>
</FrameLayout>
Output:-
输出:-
Use Of Gravity and Layout-gravity in LinearLayout .....
在 LinearLayout 中使用 Gravity 和 Layout-gravity .....
Gravity
working same as above but here differnce is that we can use Gravity inside the LinearLayout View
and RelativeLayout View
which is not possible in FrameLayout View
.
Gravity
工作与上面相同,但这里differnce是,我们可以利用重力里面LinearLayout View
和RelativeLayout View
这是不可能的FrameLayout View
。
LinearLayout with orientation vertical ....
具有垂直方向的 LinearLayout ....
Note:- Here we can set only 3 values of layout_gravity
that is (left
| right
| center
(also called center_horizontal
)).
注: -在这里,我们可以只设置3个值layout_gravity
是(left
| right
| center
(又称center_horizontal
))。
have look on example :-
看看例子:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#264bd1"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:text="Center Layout Gravity \nor \nCenter_Horizontal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginTop="20dp"
android:textColor="#d75d1c"
android:layout_gravity="right"
android:text="Right Layout Gravity"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginBottom="100dp"
android:textColor="#d71cb2"
android:layout_gravity="left"
android:layout_marginTop="20dp"
android:gravity="bottom"
android:text="Left Layout Gravity"/>
</LinearLayout>
Output:-
输出:-
LinearLayout with orientation horizontal ....
具有水平方向的 LinearLayout ....
Note:- Here we can set also 3 values of layout_gravity
that is (top
| bottom
| center
(also called center_vertical
)).
注: -在这里,我们还可以设置3个的值layout_gravity
是(top
| bottom
| center
(又称center_vertical
))。
have look on example :-
看看例子:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<TextView
android:layout_width="120dp"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:textColor="#264bd1"
android:gravity="center"
android:layout_gravity="bottom"
android:text="Bottom \nLayout \nGravity"/>
<TextView
android:layout_width="120dp"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginTop="20dp"
android:textColor="#d75d1c"
android:layout_gravity="center"
android:text="Center \nLayout \nGravity"/>
<TextView
android:layout_width="150dp"
android:layout_height="100dp"
android:textSize="25dp"
android:background="#000"
android:layout_marginBottom="100dp"
android:textColor="#d71cb2"
android:layout_gravity="left"
android:layout_marginTop="20dp"
android:text="Left \nLayout \nGravity"/>
</LinearLayout>
output:-
输出:-
Note:- We can't use layout_gravity
in the RelativeLayout Views
but we can use gravity
to set RelativeLayout
childs to same position....
注意:- 我们不能使用layout_gravity
inRelativeLayout Views
但我们可以gravity
用来将RelativeLayout
孩子设置到相同的位置....
回答by Mahendra Liya
An easy trick to remember this is gravity applies to us inside earth. So, android:gravity
is for insidethe view.
记住这一点的一个简单技巧是重力适用于地球内部的我们。所以,android:gravity
是为内部视图。
Rememeber the outin layout_gravitywhich would help you to remember that android:layout_gravity
would refer to outsidethe view
Rememeber的出在奠定了_gravity这将有助于你要记住,android:layout_gravity
要提到外界的看法