设置relativelayout的宽度为屏幕宽度的1/3?

Kel*_*Tan 14 android width android-layout android-relativelayout

我有一个如下的布局.现在,我不想将相对布局的宽度设置为固定240 dp.我想将相对布局的宽度设置为屏幕宽度的1/3.是否可以在xml文件中执行此操作.如果不可能,我怎么能用java代码实现呢?

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/fullscreen"
        style="@style/translucent">
           <RelativeLayout
            android:layout_width="240dp"
            android:layout_height="fill_parent"
            android:layout_gravity="right"
            android:gravity="center"
            android:background="#88000000"
            android:id="@+id/sidebar">

           </RelativeLayout>

    </FrameLayout>
Run Code Online (Sandbox Code Playgroud)

Car*_*les 25

使用weightsum="3"在父母layout_weight=1的孩子.看看这个参考

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/fullscreen"
    style="@style/translucent"
    android:orientation="horizontal"
    android:weightSum="3">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_gravity="right"
        android:gravity="center"
        android:background="#88000000"
        android:id="@+id/sidebar"
        android:layout_weight="1" 
        >

    </RelativeLayout>

    <!-- other views, with a total layout_weight of 2 -->

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)