简体   繁体   English

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

[英]Set the width of relativelayout 1/3 the width of the screen?

I have a layout like below. 我有一个如下的布局。 Now, I don't want to set the width of relative layout to fixed 240 dp. 现在,我不想将相对布局的宽度设置为固定240 dp。 I want to set the width of the relative layout to 1/3 the width of the screen. 我想将相对布局的宽度设置为屏幕宽度的1/3。 Is it possible to do that in the xml file. 是否可以在xml文件中执行此操作。 If it is impossible, how can I achieve that using java code ? 如果不可能,我怎么能用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>

use weightsum="3" in the parent and layout_weight=1 in the child. 在父级中使用weightsum="3" ,在子级中使用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>

You have to use a LinearLayout to get the width of a view to be a third of its parentview. 您必须使用LinearLayout将视图的宽度设置为其父视图的三分之一。

Something like: 就像是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#88000000">
    </RelativeLayout>
    <ViewGroup
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2">
    </ViewGroup>
</LinearLayout>

The key bit is the ratio of the layout_weights. 关键位是layout_weights的比率。 The documentation is pretty good. 文档非常好。

A LinearLayout with a android:layout_orientation="horizontal" is what you want, along with weights. 带有android:layout_orientation="horizontal"LinearLayout就是你想要的,还android:layout_orientation="horizontal"重。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        ...all your stuff... />

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />


</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将RelativeLayout的宽度设置为屏幕宽度? - How I set the width of RelativeLayout to screen width? RelativeLayout中的按钮填充屏幕宽度 - Buttons in RelativeLayout fill screen width 如何设置RelativeLayout内部的LinearLayout的宽度与屏幕分辨率无关? - How to set the width of LinearLayout inside RelativeLayout to be independent of screen resolution? Android:如何将RelativeLayout填充到整个屏幕宽度? - Android: How to fill RelativeLayout to full width of screen? 在屏幕宽度的50%的RelativeLayout中加载WebView时出现问题 - Problems loading a WebView in a RelativeLayout with the 50% of the width of the screen 如何在RelativeLayout中将图像分配到屏幕宽度? - How to make image distributed to screen width in RelativeLayout? 如何在Android RelativeLayout中为3个按钮设置相同的宽度? - How to set the same width for 3 buttons in android RelativeLayout? 在 relativelayout 中动态设置 LinearLayout 的高度/宽度 - dynamically set LinearLayout height/width in relativelayout 充气时RelativeLayout不遵守设置的宽度 - RelativeLayout does not adhere to set width when inflated 如何设置相对布局高度等于其宽度? - How to set relativelayout height equal to its width?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM