2010-12-17 16 views
0

同じ行に4つのボタンを使ってXMLでレイアウトを作成するにはどうすればよいですか?最初と最後のボタンは特定の幅(40ピクセル)にする必要があり、中間の2つはコンテンツの幅(wrap_content)にする必要がありますか?私は、ボタンを配置したいと思いますかAndroidレイアウトの問題

サンプル...

| -b1- | .... | --b2-- || --- B3 --- | .... | - b4- |

ありがとうございます!

+0

うわー、ずっと前。あなたがドキュメントを読んだことを前提としていますが、そこには同様のものがあります。 – jocap

+0

私はSDKのドキュメントを読んでみましたが、私は何が必要なのかを見つけることができません... – hades985

答えて

2

4つのボタンを水平のLinearLayoutに配置するだけです。 layout_widthを40px、b2およびb3のlayout_widthにwrap_contentのlayout_width、次に40pxのlayout_widthでb4を指定してb1を定義します。中央に配置する場合は、LinearLayoutのlayout_widthをfill_parentに設定し、それにcenter_horizo​​ntalの重力を与えます。何かのように:

EDIT:ああ、2つの40pxボタンを両側に配置し、2つのwrap_contentボタンを中央に配置する場合は、2つの方法があります。最も簡単な方法は、より長い道はRelativeLayoutを使用し、そのRelativeLayout内のLinearLayout内の2つの中央のボタンを配置することだろうが、(私は以下を証明します)あなたのLinearLayoutにいくつかの空白のビューを追加することです:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_width="wrap_content" 
    > 
    <Button 
     android:layout_width="40px" 
     android:layout_height="wrap_content" 
     android:text="Button 1" 
     /> 
    <View 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button 2" 
     /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button 3" 
     /> 
    <View 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     /> 
    <Button 
     android:layout_width="40px" 
     android:layout_height="wrap_content" 
     android:text="Button 4" 
     /> 
</LinearLayout> 
+0

ありがとう!それはまさに私が望んでいたものです! – hades985

関連する問題