Android百分比宽度布局

6 layout android

我需要将视图的宽度设置为屏幕宽度的50%,然后将此视图水平居中,同时可能有一个或多个按钮,这些按钮可以显示为附加到屏幕的左侧或右侧.

我正在使用相对布局,这样我就可以放置一个带有权重的线性布局,使我的50%居中,同时将任何按钮放在连接到RL左边或右边的LL的顶部.但是这个布局缺少蓝色中间栏.如果我将中间视图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>
Run Code Online (Sandbox Code Playgroud)

Vla*_*nov 18

您应该将视图的宽度设置为 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>
Run Code Online (Sandbox Code Playgroud)