2010-11-25 9 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をwidthに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

優れています。ありがとう! –

+0

完璧に働いて、ありがとう。 –

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> 

私はそれが最善の方法だと思います。