简体   繁体   English

相对布局中的布局权重

[英]Layout weight in relative layout

I am aware about layout weight in linear layout. 我知道线性布局中的布局重量。 Can i assign layout weight in relative layout. 我可以在相对布局中指定布局权重。

example: two image view in a layout which fills the layout in the ratio 60:40. 示例:布局中的两个图像视图以60:40的比例填充布局。 first image should take up 60% of the whole screen height and the second image has to take the remaining 40% of the screen. 第一张图像应占整个屏幕高度的60%,第二张图像必须占据屏幕剩余的40%。

Don't just answer for this example problem alone please tell me the concept precisely or post some reference links about layout weight in relative layout. 不要单独回答这个示例问题,请准确地告诉我这个概念,或者在相对布局中发布一些关于布局权重的参考链接。 Thanks in advance. 提前致谢。

You can place an invisible view in center of your layout and align your view in left and right. 您可以在布局的中心放置一个不可见的视图,并左右对齐您的视图。 Here is an example 这是一个例子

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@+id/view"
        android:background="#fffba2" />

    <View 
        android:id="@+id/view"
        android:layout_width="1dp"
        android:layout_height="1dp"
        android:layout_centerHorizontal="true"
        android:visibility="invisible" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@+id/view"
        android:background="#ba2fff" />

</RelativeLayout>

Actually you cannot use weight in a RelativeLayout but you can use a combination of Relative and Linear layouts in order to take both advantages of them! 实际上你不能在RelativeLayout使用weight ,但是你可以使用相对和线性布局的组合来获得它们的优点!

Tip: Try to use as less layouts as possible to avoid slow UI, because of multiple screen measurements! 提示:由于多个屏幕测量,尽量使用尽可能少的布局以避免缓慢的UI! [1] [2] [1] [2]

There is no need of weights with Relative Layout. 相对布局不需要权重。 You can move around the Image Views to make sure that they are in correct proportion. 您可以移动图像视图以确保它们的比例正确。 Weights are only used with LinearLayout. 权重仅与LinearLayout一起使用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM