71

私は次のレイアウトを用意しています

<?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 番目のボタンは頑固に左揃えのままです。どうすればそれを正しく整列させることができますか。

4

5 に答える 5

147

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

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

<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>
于 2010-11-29T16:16:30.700 に答える
21

1つだけを使用してすべてを実行できます(ところで、パラメーターRelativeLayoutは必要ありません)。android:orientationしたがって、LinearLayoutたくさんのものを含む を使用する代わりに、次のようなことができます。

<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 パラメーターが欠落しています。設定する必要がある基本的なパラメーターを示しただけです。残りは完了できます。

于 2010-11-29T16:13:58.453 に答える
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) では、 LinearLayoutのRelativeLayoutを変更するだけです。

于 2013-08-07T16:53:39.233 に答える
2

古いバージョンをサポートするには、以下のように Space を View に置き換えることができます。このビューを、左端のコンポーネントの後と右端のコンポーネントの前の間に追加します。weight=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>
于 2016-03-02T00:28:47.260 に答える