8

ビューの幅を画面の幅の 50% に設定し、このビューを水平方向に中央に配置する必要がありますが、画面の左側または右側に 1 つ以上のボタンが取り付けられている可能性があります。

RL の左端または右端に取り付けられた LL の上にボタンを配置しながら、50% を中央に配置するためにウェイトを使用して線形レイアウトを配置できるように、相対レイアウトを使用しています。ただし、このレイアウトには青い中央のバーがありません。中央のビューの layout_weight を 1 に設定すると、3 つの同じサイズのバーが表示されます。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="48dp">

    <LinearLayout
        android:id="@+id/stupid_android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <View
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:background="#FF0000"
            android:layout_weight="1" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:background="#0000FF"
            android:layout_weight="2" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:background="#00FF00"
            android:layout_weight="1" />

    </LinearLayout>

</RelativeLayout>
4

4 に答える 4

19

ビューの幅をに設定する必要があります0dip

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="48dp">

    <LinearLayout
        android:id="@+id/stupid_android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <View
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:background="#FF0000"
            android:layout_weight="1" />

        <View
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:background="#0000FF"
            android:layout_weight="2" />

        <View
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:background="#00FF00"
            android:layout_weight="1" />

    </LinearLayout>

</RelativeLayout>
于 2013-03-04T14:47:48.087 に答える
2

これは、新しいパーセント ライブラリを使用して実現できます。

https://developer.android.com/tools/support-library/features.html#percent

このようなことをすることによって:

<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="48dp">

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="#FF0000"
        app:layout_widthPercent="25%" />

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="#0000FF"
        android:centerHorizontal="true"
        app:layout_marginLeftPercent="25%"
        app:layout_widthPercent="50%" />

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:alignParentRight="true"
        android:background="#00FF00"
        app:layout_marginLeftPercent="75%"
        app:layout_widthPercent="25%" />

</android.support.percent.PercentRelativeLayout>
于 2015-08-24T07:25:10.127 に答える
0

新しいパーセンテージ サポート ライブラリを使用します。

compile 'com.android.support:percent:24.0.0'

例 :

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <android.support.percent.PercentRelativeLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"

        >

        <View
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#FF0000"
            app:layout_widthPercent="33%" />

        <View
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="#0000FF"
            app:layout_marginLeftPercent="33%"
            app:layout_widthPercent="33%" />

        <View
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#00FF00"
            app:layout_marginLeftPercent="66%"
            app:layout_widthPercent="33%" />

    </android.support.percent.PercentRelativeLayout>


</LinearLayout>

詳細情報Android Doc

※サンプル&チュートリアル用 ※チュートリアル 1 チュートリアル2 チュートリアル3

于 2016-10-26T13:33:02.177 に答える
-2

私が正しく理解していれば、これが必要です:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp">

    <LinearLayout
        android:id="@+id/stupid_android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <View
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:background="#FF0000"
            android:layout_weight="1" ></View>

        <View
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:background="#0000FF"
            android:layout_weight="1.5" ></View>

        <View
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:background="#00FF00"
            android:layout_weight="1.5" ></View>

    </LinearLayout>

</RelativeLayout>
于 2013-03-05T07:54:06.923 に答える