1. Download our Official Android App: Forums for Android!

Apps Difference between layout_gravity and gravity

Discussion in 'Android Development' started by raduq, Sep 24, 2011.

  1. raduq

    raduq Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    10
    Joined:
    Sep 18, 2011

    Sep 18, 2011
    10
    1
    16
    Student
    Cluj Napoca - Romania
    Hello!

    I have just started about two weeks ago to develop Android applications and also, just started working with XML. I think I understood most of the things in XML and the way it influences the application, but I can't understand the difference between android:layout_gravity and android:gravity, applied for example on a button.

    I have tried to align a button to the center and used this code:

    Code (Text):
    1. <Button
    2.            android:id="@+id/login_button"
    3.            android:layout_width="wrap_content"
    4.            android:layout_height="wrap_content"
    5.            android:text="Log in"
    6.            android:layout_gravity="center" />
    and it worked properly. But also, I have tried the one where "android:layout_gravity" is replaced with the simple "android:gravity" with the same center option, but it does not work.

    I have read in the small pop-up box with the options that appears when I type in the code that the "layout_gravity" is "Standard gravity constant that a child can supply to its parent."

    So, the child in my situation is the <Button /> and the parent is the <LinearLayout />, right?

    If that's right, it means that the layout_gravity:center in the <Button />tag influences the <LinearLayout /> tag? I'm almost sure that's not the deal, but what's the thing with CHILD and PARENT? I got it a bit mixed up.. as I think you can see already.

    Any help is very much appreciated!
     

    Advertisement

  2. jonbonazza

    jonbonazza Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
    android:gravity applies graivity to the content of the view, while
    android:layout_gravity applies gravity to the view itself inside its parent.
     
    CreatorCool likes this.

Share This Page

Loading...