Reputation: 11191
I have a layout with 7 buttons, I want 6 of them centered in the screen and the last one at bottom.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="@+id/account_but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dip"
android:background="@null"
android:contentDescription="@string/accountDesc"
android:src="@drawable/boton_usos" />
<ImageButton
android:id="@+id/alquilar_but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:background="@null"
android:contentDescription="@string/alquilarDesc"
android:src="@drawable/boton_alquilar" />
<ImageButton
android:id="@+id/paradas_but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:background="@null"
android:contentDescription="@string/paradasDesc"
android:src="@drawable/boton_paradas" />
<ImageButton
android:id="@+id/noticias_but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:background="@null"
android:contentDescription="@string/noticiasDesc"
android:src="@drawable/boton_noticias" />
<ImageButton
android:id="@+id/incidencia_but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:background="@null"
android:contentDescription="@string/incidenciaDesc"
android:src="@drawable/boton_incidencia" />
<ImageButton
android:id="@+id/informacion_but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:background="@null"
android:contentDescription="@string/infoDesc"
android:src="@drawable/boton_informacion" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical" >
<ImageButton
android:id="@+id/sabadell_but"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/banner_sabadell"
android:contentDescription="@string/infoDesc" />
</LinearLayout>
</LinearLayout>
With this code, I got the buttons start at left top.
How can I fix it?
Upvotes: 0
Views: 572
Reputation: 6635
Giving you the my full XML file, as i had worked on it in my app
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_mainscreen">
<LinearLayout
android:id="@+id/lnrContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >
<Button
android: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"/>
<Button
android: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"/>
<Button
android: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" />
<Button
android: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"/>
<Button
android: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>
<Button
android: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>
Upvotes: 0
Reputation: 9507
Use android:gravity
instead of layout_gravity
.
<LinearLayout
android: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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/account_but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dip"
android:text="Button 1" />
<Button
android:id="@+id/alquilar_but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="Button 2" />
<Button
android:id="@+id/paradas_but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="Button 3" />
<Button
android:id="@+id/noticias_but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="Button 4" />
<Button
android:id="@+id/incidencia_but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="Button 5" />
<Button
android:id="@+id/informacion_but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="Button 6" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android: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.
Upvotes: 3
Reputation: 1311
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>
Upvotes: 2