微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>

解决方法

您应该将视图的宽度设置为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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐