Difference between gravity and layout_gravity on A

2018-12-31 02:05发布

I know we can set the following values to the android:gravity and android:layout_gravity properties:

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

But I am confused regarding both of these.

What is the difference between the usage of android:gravity and android:layout_gravity?

20条回答
泪湿衣
2楼-- · 2018-12-31 02:43

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

查看更多
若你有天会懂
3楼-- · 2018-12-31 02:44

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

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

Note:-

  1. Gravity is used inside the View Content as some User have answer and it is same for all ViewGroup Layout.

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

  3. Gravity and Layout-gravity is work more useful with the FrameLayout childs . We can't use Gravity and Layout-gravity in FrameLayout's Tag ....

  4. We can set Child View any where in the FrameLayout using 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. FrameLayout fully working on Layout-gravity. Example:- If you work on FrameLayout then you don't need to change whole Layout for adding new View. You just add View as last in the FrameLayout and give him Layout-gravity with value.(This is adavantages of layout-gravity with 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

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

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.

LinearLayout with orientation vertical ....

Note:- Here we can set only 3 values of layout_gravity that is (left | right | center (also called 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

LinearLayout with orientation horizontal ....

Note:- Here we can set also 3 values of layout_gravity that is (top | bottom | center (also called 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

Note:- We can't use layout_gravity in the RelativeLayout Views but we can use gravity to set RelativeLayout childs to same position....

查看更多
姐姐魅力值爆表
4楼-- · 2018-12-31 02:46

Something I saw on Sandip's blog that I almost missed, fixed my problem. He said layout_gravity DOES NOT WORK WITH LinearLayout.

If you're using a LinearLayout and the gravity settings are driving you nuts (like me), then switch to something else.

I actually switched to a RelativeLayout then used layout_alignParentLeft and layout_alignParentRight on the 2 contained TextViews to get them on one line to go far left and far right.

查看更多
一个人的天荒地老
5楼-- · 2018-12-31 02:49

android:gravity -> Sets the gravity of the content of the View its used on.

android:layout_gravity -> Sets the gravity of it's Parent's view or Layout

查看更多
与君花间醉酒
6楼-- · 2018-12-31 02:50

From what I can gather layout_gravity is the gravity of that view inside its parent, and gravity is the gravity of the children inside that view.

I think this is right but the best way to find out is to play around.

查看更多
美炸的是我
7楼-- · 2018-12-31 02:50

Look at the image to be clear about gravity

查看更多
登录 后发表回答