Android布局设置两个按钮垂直和水平居中

问题描述:

我正在开发一个Android应用程序。Android布局设置两个按钮垂直和水平居中

我想设置按钮保存并关闭居中,但它们显示在左侧。

这里是我的XML布局代码:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/dialogGameName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_marginBottom="2sp" 
     android:layout_marginLeft="5sp" 
     android:layout_marginRight="5sp" 
     android:layout_marginTop="5sp" 
     android:typeface="sans" 
     android:textSize="26sp" 
     android:textStyle="bold"/> 
    <TextView 
     android:id="@+id/dialogGameDescription" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="left" 
     android:layout_marginBottom="5sp" 
     android:layout_marginLeft="5sp" 
     android:layout_marginRight="5sp" 
     android:layout_marginTop="2sp" 
     android:typeface="monospace" 
     android:textSize="18sp"/> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_gravity="center_vertical|center_horizontal"> 
     <Button 
      android:id="@+id/registerGameButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/save" 
      android:gravity="center" 
      android:layout_marginBottom="5sp" 
      android:layout_marginLeft="5sp" 
      android:layout_marginRight="2sp" 
      android:layout_marginTop="0sp" /> 
     <Button 
      android:id="@+id/closeButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/close" 
      android:gravity="center" 
      android:layout_marginBottom="5sp" 
      android:layout_marginLeft="2sp" 
      android:layout_marginRight="5sp" 
      android:layout_marginTop="0sp"/> 
    </LinearLayout> 
</LinearLayout> 

你真的不能居中任何东西,除非它的父是比它大。你有父母的按钮为WRAP_CONTENT,所以没有空间中心......尝试一下本作的按钮:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:gravity="center"> 
    <Button 
     android:id="@+id/registerGameButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="save" 
     android:layout_marginBottom="5sp" 
     android:layout_marginLeft="5sp" 
     android:layout_marginRight="2sp" 
     android:layout_marginTop="0sp" /> 
    <Button 
     android:id="@+id/closeButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="close" 
     android:layout_marginBottom="5sp" 
     android:layout_marginLeft="2sp" 
     android:layout_marginRight="5sp" 
     android:layout_marginTop="0sp"/> 
</LinearLayout> 
+0

这工作非常出色。谢谢。 – Jonathan 2014-07-06 20:39:19

为U(我在你的XML所做的更改)

    的一些技巧
  • android:layout_gravity是布局其子,我们需要设置android:gravity

  • 得到确切的中心,我们设置android:gravity="center"

  • 为了设置在正中央孩子的父母应与android:layout_width="fill_parent"android:layout_height="fill_parent"

  • 如果布局是任何其他布局的孩子其也应与android:layout_width="fill_parent"android:layout_height="fill_parent"

    <TextView 
        android:id="@+id/dialogGameName" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center" 
        android:layout_marginBottom="2sp" 
        android:layout_marginLeft="5sp" 
        android:layout_marginRight="5sp" 
        android:layout_marginTop="5sp" 
        android:text="asdff" 
        android:typeface="sans" 
        android:textSize="26sp" 
        android:textStyle="bold"/> 
    <TextView 
        android:id="@+id/dialogGameDescription" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="left" 
        android:text="asdff" 
        android:layout_marginBottom="5sp" 
        android:layout_marginLeft="5sp" 
        android:layout_marginRight="5sp" 
        android:layout_marginTop="2sp" 
        android:typeface="monospace" 
        android:textSize="18sp"/> 
    
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:orientation="horizontal" 
        android:gravity="center"> 
        <Button 
         android:id="@+id/registerGameButton" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/save" 
         android:gravity="center" 
         android:layout_marginBottom="5sp" 
         android:layout_marginLeft="5sp" 
         android:layout_marginRight="2sp" 
         android:layout_marginTop="0sp" /> 
        <Button 
         android:id="@+id/closeButton" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/close" 
         android:gravity="center" 
         android:layout_marginBottom="5sp" 
         android:layout_marginLeft="2sp" 
         android:layout_marginRight="5sp" 
         android:layout_marginTop="0sp"/> 
    </LinearLayout> 
    

+0

没必要设置android:layout_height =“fill_parent”来使其工作。 – VansFannel 2011-01-15 06:50:37

地址:

android:gravity="center_horizontal" 

到你的水平线性布局,其中包含你的两个按钮。

的“机器人:layout_gravity”决定的看法如何定位自身在它的父,而“安卓重力”决定的意见,孩子应该如何排列