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

前端之家收集整理的这篇文章主要介绍了android – 设置relativelayout的宽度1/3屏幕的宽度?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个如下的布局.现在,我不想将相对布局的宽度设置为固定的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>

解决方法

在parent中使用weightum =“3”,在child中使用layout_weight = 1. Take a look a this reference
<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>
原文链接:https://www.f2er.com/android/311386.html

猜你在找的Android相关文章