2011-01-14 566 views
0

我正在開發一個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> 

回答

5

你真的不能居中任何東西,除非它的父是比它大。你有父母的按鈕爲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

0

爲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

3

地址:

android:gravity="center_horizontal" 

到你的水平線性佈局,其中包含你的兩個按鈕。

的「機器人:layout_gravity」決定的看法如何定位自身在它的父,而「安卓重力」決定的意見,孩子應該如何排列