我有一个布局与7个按钮,我希望他们中的6个在屏幕上居中,最后一个在底部。

<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>使用这段代码,我让按钮从左上角开始。
我怎么才能修复它?
发布于 2013-03-19 18:00:47
使用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>和输出。

发布于 2013-03-19 18:00:25
试试这个:使用android:layout_gravity="center"的android: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>发布于 2013-03-19 18:16:58
为您提供我的完整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>https://stackoverflow.com/questions/15496236
复制相似问题