2010-11-29 12 views
63

私は場所にこの質問の目的のためにAndroidのレイアウト右揃え

<?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"> 

    <LinearLayout android:orientation="vertical" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:layout_weight="1"> 


     <WebView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/webview" android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 
    </LinearLayout> 

    <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="13"> 
     <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
      <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> 
        <ImageButton android:background="@null" android:id="@+id/back" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/back" android:padding="10dip" /> 
      </LinearLayout> 

      <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> 
       <ImageButton android:background="@null" android:id="@+id/forward" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/forward" android:padding="10dip" /> 
      </LinearLayout> 

     </LinearLayout> 

     <RelativeLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" > 
       <ImageButton android:background="@null" android:id="@+id/special" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/barcode" android:padding="10dip" android:layout_gravity="right"/> 
     </RelativeLayout> 




    </LinearLayout> 


</LinearLayout> 

を以下のレイアウトを持っているが、私は、レイアウトの下半分が心配です。今は3つの画像ボタンがあります。最初の2つは、私はお互いに隣り合わせで、左に揃えたいと思っています。第3のものは、私は右側に整列したい。

これは、最初の2つのボタンは私が望むところですが、3番目のボタンはちょっと左に揃っています。どのように私はそれを正しく整列させるでしょう。

答えて

124

レイアウトは非常に非効率的で膨大です。あなたはそれほど多くは必要ありませんLinearLayout s。実際には、LinearLayoutはまったく必要ありません。

RelativeLayoutを1つだけ使用してください。このような。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <ImageButton android:background="@null" 
     android:id="@+id/back" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/back" 
     android:padding="10dip" 
     android:layout_alignParentLeft="true"/> 
    <ImageButton android:background="@null" 
     android:id="@+id/forward" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/forward" 
     android:padding="10dip" 
     android:layout_toRightOf="@id/back"/> 
    <ImageButton android:background="@null" 
     android:id="@+id/special" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/barcode" 
     android:padding="10dip" 
     android:layout_alignParentRight="true"/> 
</RelativeLayout> 
+0

、ありがとう私のトラブルの多くを保存しました。 –

18

RelativeLayout(btwは、android:orientationパラメータを必要としません)を1つだけ使用することですべてを行うことができます。

<RelativeLayout> 
    <ImageButton 
     android:layout_width="wrap_content" 
     android:id="@+id/the_first_one" 
     android:layout_alignParentLeft="true"/> 
    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_toRightOf="@+id/the_first_one"/> 
    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_alignParentRight="true"/> 
</RelativeLayout> 

あなたが気づいているように、不足しているいくつかのXMLパラメータがありますので、代わりに、LinearLayoutを持つ原料の束を含有すると、次のような何かを行うことができます。私はちょうどあなたが入れなければならなかった基本的なパラメータを示していました。あなたは残りを完了することができます。

3

これはRelativeLayoutの例です:

RelativeLayout relativeLayout=(RelativeLayout)vi.findViewById(R.id.RelativeLayoutLeft); 
       RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams(); 
       params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
       relativeLayout.setLayoutParams(params); 

あなたは単にのLinearLayoutためRelativeLayoutを変更する必要があり、レイアウトの別の種類(例のLinearLayout)で。

3

LinearLayoutを使用する場合は、layout_weightSpace要素のアライメントを行うことができます。

など。お互いに次のレイアウトの場所textViewtextView2以下とtextView3は右寄せ

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

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:id="@+id/textView" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:id="@+id/textView2" /> 

    <Space 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="20dp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:id="@+id/textView3" /> 
</LinearLayout> 

あなたはtextView2layout_weightを設定するならば、あなたがSpaceせずに同様の効果を得ることができるでしょう。私は物事がより分かれていることをプラスして、Spaceという要素を実証することができます。

<TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:id="@+id/textView2" /> 

you should (not must though) setlayout_widthが明示的にそれはとにかく重量(あなたが垂直LinearLayoutの要素に高さを設定しなければならないのと同じ方法)だに応じて再計算されますように注意してください。他のレイアウトのパフォーマンスのヒントについてはAndroid Layout Tricksシリーズを参照してください。

1

古いバージョンをサポートするにはSpaceを以下のように表示に置き換えることができます。このビューは、左端のコンポーネントの後と右端のコンポーネントの間に追加します。重み= 1のこのビューは、伸びて空白を埋めます

<View 
     android:layout_width="0dp" 
     android:layout_height="20dp" 
     android:layout_weight="1" /> 

完全なサンプルコードはここに示します。それは4つのコンポーネントを持っています。2つの矢印が左右にあります。テキストとスピナーは中間にあります。

<ImageButton 
     android:id="@+id/btnGenesis" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center|center_vertical" 
     android:layout_marginBottom="2dp" 
     android:layout_marginLeft="0dp" 
     android:layout_marginTop="2dp" 
     android:background="@null" 
     android:gravity="left" 
     android:src="@drawable/prev" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="20dp" 
     android:layout_weight="1" /> 

    <TextView 
     android:id="@+id/lblVerseHeading" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:gravity="center" 
     android:textSize="25sp" /> 

    <Spinner 
     android:id="@+id/spinnerVerses" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:gravity="center" 
     android:textSize="25sp" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="20dp" 
     android:layout_weight="1" /> 

    <ImageButton 
     android:id="@+id/btnExodus" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center|center_vertical" 
     android:layout_marginBottom="2dp" 
     android:layout_marginLeft="0dp" 
     android:layout_marginTop="2dp" 
     android:background="@null" 
     android:gravity="right" 
     android:src="@drawable/next" /> 
</LinearLayout>