What is the difference between gravity and layout_gravity in Android?

前端 未结 20 1183
庸人自扰
庸人自扰 2020-11-21 11:41

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

    <
20条回答
  •  一生所求
    2020-11-21 12:18

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

    
    
    
        
    
        
    
        
    
        
    
    
        
    
    
    

    Output:-

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

    
    
    
        
    
        
    
    
        
    
    
    

    Output:-

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

    
    
    
        
    
        
    
    
        
    
    
    

    output:-

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

提交回复
热议问题