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

标签 android android-layout width 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>

最佳答案

在父级中使用 weightsum="3",在子级中使用 layout_weight=1Take 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>

关于android - 将relativelayout的宽度设置为屏幕宽度的1/3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21225213/

相关文章:

android - android中 Activity 方向变化的奇怪行为

android - 使用 FirebaseMessagingService 类时出现错误 "return type int is not compatible with Intent"

java - 如何将新的依赖项插入 Gradle 项目?

android - 如何更改特定 ListView 的项目或元素的颜色

css - 哪个父元素扰乱了我的宽度 : 100%

Java android 10 创建文件的问题

android - 操作栏、工具栏、 Activity 继承

android - 如何在布局的中心准确显示 Switch 小部件?

css width % 工作不正常;比必要的时间更长

AlertDialog 中的 Android EditText 似乎太宽了