Skip to content Skip to sidebar Skip to footer

Layout_gravity Doesnt Work

I have a layout with 7 buttons, I want 6 of them centered in the screen and the last one at bottom.

Solution 1:

Use android:gravity instead of layout_gravity.

<LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:layout_gravity="center"
          >

I implement it and It is working.

EDIT:

I put Entire XML.

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:gravity="center"android:orientation="vertical" ><Buttonandroid:id="@+id/account_but"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="50dip"android:text="Button 1" /><Buttonandroid:id="@+id/alquilar_but"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="5dip"android:text="Button 2" /><Buttonandroid:id="@+id/paradas_but"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="5dip"android:text="Button 3" /><Buttonandroid:id="@+id/noticias_but"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="5dip"android:text="Button 4" /><Buttonandroid:id="@+id/incidencia_but"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="5dip"android:text="Button 5" /><Buttonandroid:id="@+id/informacion_but"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="5dip"android:text="Button 6" /></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical" ><Buttonandroid:id="@+id/sabadell_but"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_gravity="bottom"android:text="Button 7" /></LinearLayout></LinearLayout>

And Output.

enter image description here

Solution 2:

Try this: Use android:gravity="center" insted of android:layout_gravity="center"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
>

  <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:orientation="vertical"
         android:gravity="center"
      >

  <ImageButton
        android:id="@+id/account_but"
        android:background="@null"
        android:contentDescription="@string/accountDesc"
        android:layout_marginTop="50dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/boton_usos" />

    <ImageButton
        android:id="@+id/alquilar_but"
        android:background="@null"
        android:contentDescription="@string/alquilarDesc"
        android:layout_marginTop="5dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/boton_alquilar" />


    <ImageButton
        android:id="@+id/paradas_but"
        android:background="@null"
        android:contentDescription="@string/paradasDesc"
        android:layout_marginTop="5dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/boton_paradas" />

    <ImageButton
        android:id="@+id/noticias_but"
        android:background="@null"
        android:contentDescription="@string/noticiasDesc"
        android:layout_marginTop="5dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/boton_noticias" />

    <ImageButton
        android:id="@+id/incidencia_but"
        android:background="@null"
        android:contentDescription="@string/incidenciaDesc"
        android:layout_marginTop="5dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/boton_incidencia" />

     <ImageButton
        android:id="@+id/informacion_but"
        android:background="@null"
        android:contentDescription="@string/infoDesc"
        android:layout_marginTop="5dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/boton_informacion" /> 

    </LinearLayout>

     <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:orientation="vertical"
        android:layout_gravity="bottom"
      >     

        <ImageButton
            android:id="@+id/sabadell_but"
            android:background="@drawable/banner_sabadell"
            android:contentDescription="@string/infoDesc"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
    </LinearLayout>  

</LinearLayout>

Solution 3:

Giving you the my full XML file, as i had worked on it in my app

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@drawable/bg_mainscreen"><LinearLayoutandroid:id="@+id/lnrContent"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:orientation="vertical" ><Buttonandroid:id="@+id/btnRecipe"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginLeft="80dip"android:layout_marginRight="80dip"android:background="@string/RecipeButton_background"/><Buttonandroid:id="@+id/btnIngredients"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginLeft="80dip"android:layout_marginRight="80dip"android:layout_marginTop="20dip"android:background="@string/IngredientButton_background"/><Buttonandroid:id="@+id/btnMenu"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginLeft="80dip"android:layout_marginRight="80dip"android:layout_marginTop="20dip"android:background="@string/MenuButton_background" /><Buttonandroid:id="@+id/btnShoppingList"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginLeft="80dip"android:layout_marginRight="80dip"android:layout_marginTop="20dip"android:background="@string/ShoppingListButton_background"/><Buttonandroid:id="@+id/btnSynctoServer"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginLeft="80dip"android:layout_marginRight="80dip"android:layout_marginTop="20dip"android:text="Synchronize"android:textColor="@android:color/black"android:background="@string/ShoppingListButton_background"/></LinearLayout><Buttonandroid:id="@+id/btnSynctoServer"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:text="Synchronize"android:layout_marginLeft="80dip"/></RelativeLayout>

Post a Comment for "Layout_gravity Doesnt Work"