2013-03-19 59 views
0

我有一個7個按鈕的佈局,我想讓他們中的6個居中在屏幕上,最後一個在底部。layout_gravity doesnt work

enter image description here

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

有了這個代碼,我得到的按鈕,左上方開始。

我該如何解決?

+0

可以顯示圖像畫面實際上是如何想? – 2013-03-19 09:58:53

回答

3

使用android:gravity而不是layout_gravity

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

我執行它,它正在工作。

編輯:

我把整個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> 

和輸出。

enter image description here

+0

所以,我應該寫兩個,我得到第一部分居中,但不是最後一個底部 – user1256477 2013-03-19 10:06:21

+1

@ user1256477現在檢查答案。我更新它。 – 2013-03-19 10:10:15

2

試試這個:使用安卓重力= 「中心」 insted的的android:layout_gravity = 「中心」

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

給你的我的完整的XML文件,因爲我已經在我的應用上,它的工作

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