首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >layout_gravity不工作

layout_gravity不工作
EN

Stack Overflow用户
提问于 2013-03-19 17:56:14
回答 3查看 549关注 0票数 0

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

代码语言:javascript
复制
<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>

使用这段代码,我让按钮从左上角开始。

我怎么才能修复它?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-03-19 18:00:47

使用android:gravity而不是layout_gravity

代码语言:javascript
复制
 <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:gravity="center"
            android:layout_gravity="center"
          >

我实现了它,它正在工作。

编辑:

我把整个XML。

代码语言:javascript
复制
<?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>

和输出。

票数 3
EN

Stack Overflow用户

发布于 2013-03-19 18:00:25

试试这个:使用android:layout_gravity="center"android:gravity="center"实例

代码语言:javascript
复制
<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>
票数 2
EN

Stack Overflow用户

发布于 2013-03-19 18:16:58

为您提供我的完整XML文件,因为我在应用程序中对其进行了处理

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15496236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档