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>

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...