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

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.jb51.cc/android/311386.html

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

相关推荐