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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:24:31  来源:igfitidea点击:

What is the difference between gravity and layout_gravity in Android?

androidandroid-layoutandroid-gravity

提问by Paresh Mayani

I know we can set the following values to the android:gravityand android:layout_gravityproperties:

我知道我们可以为android:gravityandroid:layout_gravity属性设置以下值:

  1. center
  2. center_vertical
  3. center_horizontal, etc.
  1. center
  2. center_vertical
  3. center_horizontal, 等等。

But I am confused regarding both of these.

但我对这两者感到困惑。

What is the difference between the usage of android:gravityand android:layout_gravity?

android:gravity和的用法有什么区别android:layout_gravity

回答by Sephy

Their names should help you:

他们的名字应该可以帮助你:

  • android:gravitysets the gravity of the contents (i.e. its subviews) of the Viewit's used on.
  • android:layout_gravitysets the gravity of the Viewor Layoutrelative to its parent.
  • android:gravity设置View它所使用的内容(即它的子视图)的重力。
  • android:layout_gravity设置ViewLayout相对于其父项的重力。

And an example is here.

一个例子是here

回答by Suragch

Inside - Outside

内部 - 外部

  • gravityarranges the content insidethe view.
  • layout_gravityarranges the view's position outsideof itself.
  • gravity排列视图的内容。
  • layout_gravity将视图的位置排列在其自身之外

Sometimes it helps to see a picture, too. The green and blue are TextViewsand the other two background colors are LinearLayouts.

有时看图片也有帮助。绿色和蓝色是TextViews,其他两种背景颜色是LinearLayouts

enter image description here

在此处输入图片说明

Notes

笔记

  • The layout_gravitydoes not work for views in a RelativeLayout. Use it for views in a LinearLayoutor FrameLayout. See my supplemental answerfor more details.
  • The view's width (or height) has to be greater than its content. Otherwise gravitywon't have any effect. Thus, wrap_contentand gravityare meaningless together.
  • The view's width (or height) has to be less than the parent. Otherwise layout_gravitywon't have any effect. Thus, match_parentand layout_gravityare meaningless together.
  • The layout_gravity=centerlooks the same as layout_gravity=center_horizontalhere because they are in a vertical linear layout. You can't center vertically in this case, so layout_gravity=centeronly centers horizontally.
  • This answer only dealt with setting gravityand layout_gravityon the views within a layout. To see what happens when you set the gravityof the of the parent layout itself, check out the supplemental answerthat I referred to above. (Summary: gravitydoesn't work well on a RelativeLayoutbut can be useful with a LinearLayout.)
  • layout_gravity不适合的意见在工作RelativeLayout。将其用于 aLinearLayout或 中的视图FrameLayout。有关更多详细信息,请参阅我的补充答案
  • 视图的宽度(或高度)必须大于其内容。否则gravity不会有任何影响。因此,wrap_contentgravity在一起是毫无意义的。
  • 视图的宽度(或高度)必须小于父视图。否则layout_gravity不会有任何影响。因此,match_parentlayout_gravity在一起是毫无意义的。
  • layout_gravity=center外观一样layout_gravity=center_horizontal,因为他们是在一个垂直线性布局在这里。在这种情况下你不能垂直居中,所以layout_gravity=center只能水平居中。
  • 该答案仅涉及布局中的设置gravitylayout_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_gravityis the Outsidegravity of the View. Specifies the direction in which the View should touch its parent's border.

android:layout_gravity是视图的外部重力。指定视图应该接触其父边框的方向。

android:gravityis 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-gravityas "Lay-outside-gravity".

layout-gravity为“外置重力”。

回答by Ying

Short Answer: use android:gravityor setGravity()to control gravity of all child views of a container; use android:layout_gravityor setLayoutParams()to control gravity of an individual view in a container.

简答:使用android:gravitysetGravity()控制容器所有子视图的重力;使用android:layout_gravitysetLayoutParams()控制容器中单个视图的重力。

Long story: to control gravity in a linear layout container such as LinearLayoutor RadioGroup, there are two approaches:

长话短说:要在线性布局容器(例如LinearLayout或 )中控制重力RadioGroup,有两种方法:

1) To control the gravity of ALL child views of a LinearLayoutcontainer (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_gravityXML attribute. In code, one needs to get the LinearLayout.LayoutParamsof 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_gravityXML 属性。在代码中,需要获取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 LinearLayoutcontainer, the horizontal gravity of its child view is left-aligned one after another and cannot be changed. Setting android:layout_gravityto center_horizontalhas no effect. The default vertical gravity is center (or center_vertical) and can be changed to top or bottom. Actually the default layout_gravityvalue is -1but Android put it center vertically.

对于水平LinearLayout容器,其子视图的水平重力是左对齐的,无法更改。设置android:layout_gravitycenter_horizontal无效。默认垂直重力为 center(或 center_vertical),可以更改为顶部或底部。实际上默认layout_gravity值是-1Android 把它垂直居中。

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:gravityXML 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:gravityXML 属性和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 gravityand layout-gravity. I am going to explain my experience about these 2 concepts(All information i got due to my observation and some websites).

有一个在许多差异gravitylayout-gravity。我将解释我对这两个概念的体验(我通过观察和一些网站获得的所有信息)。

Use Of Gravity and Layout-gravity in FrameLayout.....

FrameLayout..... 中使用 Gravity 和 Layout-gravity

Note:-

笔记:-

  1. Gravityis used inside the View Contentas some User have answer and it is same for all ViewGroup Layout.

  2. Layout-gravityis used with the parent View as some User have answer.

  3. Gravity and Layout-gravityis work more usefulwith the FrameLayoutchilds . We can't use Gravity and Layout-gravityin FrameLayout's Tag ....

  4. We can set Child View any where in the FrameLayoutusing layout-gravity.

  5. 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 .

  6. FrameLayoutfully working on Layout-gravity. Example:- If you work on FrameLayoutthen you don't need to change whole Layoutfor adding new View. You just add Viewas last in the FrameLayoutand give him Layout-gravitywith value.(This is adavantages of layout-gravity with FrameLayout).

  1. 重力视图内容中使用,因为某些用户有答案,并且对所有人都是一样的ViewGroup Layout

  2. Layout-gravity与父视图一起使用,因为某些用户有答案。

  3. Gravity and Layout-gravity工作更有用FrameLayout孩子的。We can't use Gravity and Layout-gravity在 FrameLayout 的标签中......

  4. 我们可以在FrameLayoutusing 中的任何位置设置子视图layout-gravity

  5. 我们可以使用的FrameLayout内重力的每一个值(例如: - ,center_verticalcenter_horizontalcentertop等),但它是不可能与其他的ViewGroup布局。

  6. 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:-

输出:-

g1

g1

Use Of Gravity and Layout-gravity in LinearLayout .....

在 LinearLayout 中使用 Gravity 和 Layout-gravity .....

Gravityworking same as above but here differnce is that we can use Gravity inside the LinearLayout Viewand RelativeLayout Viewwhich is not possible in FrameLayout View.

Gravity工作与上面相同,但这里differnce是,我们可以利用重力里面LinearLayout ViewRelativeLayout View这是不可能的FrameLayout View

LinearLayout with orientation vertical ....

具有垂直方向的 LinearLayout ....

Note:- Here we can set only 3 values of layout_gravitythat 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:-

输出:-

g2

g2

LinearLayout with orientation horizontal ....

具有水平方向的 LinearLayout ....

Note:- Here we can set also 3 values of layout_gravitythat 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:-

输出:-

g3

g3

Note:- We can't use layout_gravityin the RelativeLayout Viewsbut we can use gravityto set RelativeLayoutchilds to same position....

注意:- 我们不能使用layout_gravityinRelativeLayout Views但我们可以gravity用来将RelativeLayout孩子设置到相同的位置....

回答by Mahendra Liya

An easy trick to remember this is gravity applies to us inside earth. So, android:gravityis for insidethe view.

记住这一点的一个简单技巧是重力适用于地球内部的我们。所以,android:gravity是为内部视图。

Rememeber the outin layout_gravitywhich would help you to remember that android:layout_gravitywould refer to outsidethe view

Rememeber的奠定_gravity这将有助于你要记住,android:layout_gravity要提到外界的看法