2010-11-25 88 views
8

如何將這些按鈕置於Android中心? alt text中心佈局Android

我使用的layout的代碼是:

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

     <TextView android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/hello" /> 

     <LinearLayout android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="horizontal"> 

      <Button android:text="Test" 
       android:layout_width="100px" 
       android:layout_height="40px" /> 
      <Button android:text="Test" 
       android:layout_width="100px" 
       android:layout_height="40px" /> 
      <Button android:text="Test" 
       android:layout_width="100px" 
       android:layout_height="40px" /> 
      <Button android:text="Test" 
       android:layout_width="100px" 
       android:layout_height="40px" /> 
     </LinearLayout> 
</LinearLayout> 

回答

15

使LinearLayout包含按鈕的寬度爲wrap_content,並使center_horizo​​ntal具有重力。

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

<TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

<LinearLayout android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:layout_gravity="center_horizontal"> 
    <Button android:text="Test" 
     android:layout_width="100px" 
     android:layout_height="40px" /> 
    <Button android:text="Test" 
     android:layout_width="100px" 
     android:layout_height="40px" /> 
    <Button android:text="Test" 
     android:layout_width="100px" 
     android:layout_height="40px" /> 
    <Button android:text="Test" 
     android:layout_width="100px" 
     android:layout_height="40px" /> 
     </LinearLayout> 
</LinearLayout> 
+1

優秀。謝謝! – 2010-11-25 18:01:09

1

我想知道如果任何人有任何這意見爲好。我一直使用TableLayout完成它,並在所有列上設置stretchColumns和collapseColumns,以便在所有屏幕尺寸上縮放。有點像這樣:

<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="47dp" 
    android:stretchColumns="*" 
    android:collapseColumns="*" 
    android:padding="0px" 
    android:background="@drawable/bottom_bg"> 
    <TableRow> 
    <ImageView 
     android:id="@+id/article_button_home" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/button_home_selector" 
     android:layout_centerHorizontal="true" /> 
    <ImageView 
     android:id="@+id/article_button_share" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/button_sharearticle_selector" 
     android:layout_centerHorizontal="true" /> 
    <ImageView 
     android:id="@+id/article_button_settings" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/button_settings_selector" 
     android:layout_centerHorizontal="true"/> 
    <ImageView 
     android:id="@+id/article_button_about" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true"  
     android:src="@drawable/button_about_selector"/> 
</TableRow> 
</TableLayout> 

我不知道它是不是最好的方法。