Android 相对重量

Android 相对重量,android,android-layout,Android,Android Layout,在布局资源XML中,我有3个RelativeLayout,它们位于主RelativeLayout中。视图将垂直显示。这3个RelativeLayout()彼此相邻,我希望它们填满整个屏幕,而不管屏幕大小如何。我的,布局视图: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layo

在布局资源XML中,我有3个RelativeLayout,它们位于主RelativeLayout中。视图将垂直显示。这3个RelativeLayout()彼此相邻,我希望它们填满整个屏幕,而不管屏幕大小如何。我的,布局视图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/backg"
 >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="@dimen/top_mr_image"
    android:src="@drawable/temp" />

<RelativeLayout
    android:id="@+id/r1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/imageView1"
    android:layout_marginLeft="10dp"
    android:background="@drawable/r1bg"
    android:layout_weight="1"

     >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="@dimen/txt_mr_right"
        android:layout_marginRight="@dimen/txt_mr_right"
        android:layout_marginTop="39dp"
        android:text="S"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/textView1"
        android:layout_marginLeft="@dimen/txt_mr_right"
        android:layout_marginRight="@dimen/txt_mr_right"
        android:text="T"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

    <RelativeLayout
        android:id="@+id/r2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignTop="@+id/r1"
        android:layout_toRightOf="@+id/r1"
        android:layout_weight="1" >
    </RelativeLayout>

            <RelativeLayout
        android:id="@+id/r3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignTop="@+id/r2"
        android:layout_toRightOf="@+id/r2"
        android:layout_weight="1" >

    </RelativeLayout>

我为每个relativeLayout设置了
weight=1
layout\u width=0dp
,这种技术适用于按钮,我认为relativeLayout也会如此,似乎我的想法是错误的。有什么想法吗

UPD1:我已经添加了我想要的图像


相对长度不支持重量。如果要使用权重,则需要使用线性布局作为父容器。

RelativeLayout
不注意
android:layout\u weight
。(这是的属性,但不是的属性。)

您应该能够通过更简单的视图层次结构获得所需的布局。因为最后两个
RelativeLayout
s是空的,所以不清楚您想做什么。如果您需要一个纯粹的垂直组织,我建议使用
LinearLayout
而不是
RelativeLayout

编辑根据您的编辑,看起来您需要三个复合视图的水平布局,每个视图都可以单击。我认为下面的方法会奏效:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <!-- First column -->
    <LinearLayout
        android:id="@+id/firstColumn"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="..." />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="text 1"
            . . . />
    </LinearLayout>

    <!-- Second column -->
    <LinearLayout . . . >
        . . .
    </LinearLayout>
</LinearLayout>

. . .

如果按钮的内容不正确,您可以将第二级
线性布局
视图替换为
相对布局
,如果这有助于更好地组织布局。

解决方案非常简单。我一直在寻找相对布局中的重量分布

这对于所有这些情况来说都是个小把戏


将LinearLayout与android:orientation=“horizontal”

一起使用。您可以在
回收器视图中水平使用面向
LinearLayout管理器
,并将每个
RelativeLayout
放在其
适配器的每个项目中

链接:

如果您的
RelativeLayouts
设置为固定的宽度和高度,即屏幕大小,您可以从
DisplayMatrics
中获得,那就可以了

链接:

如果RelativeLayouts的内容不同,则可以使用
getItemViewType()
方法

请参见:


快乐编码:-)

如果父母是相对年轻的呢?我可以在linearLayout中添加其余的relativeLayouts。有点硬核,但可能我猜你可以有一个RelativeLayout->LinearLayout->RelativeLayout x 3,但我不确定这能实现什么。我确实在RelativeLayout中有元素,必须以精确的方式定位,这将是一种自定义的可点击的图像等元素,因为我的编码Knowledge是poor@Daler-如果您提供更多有关所需布局的详细信息(草图或屏幕截图会更好),我们可以帮助您实现。另外请注意,垂直
线性布局可以包含嵌套布局,以便在任何需要的行上提供水平组织。感谢您的反馈,我已经更新了我的帖子,您可以看到我想要的内容。我想让每个relativeLayout都可以点击,这样它们就可以充当一个按钮。这三个都有相同的设计,不同的图像类型和颜色texts@Daler-我知道你已经接受了我的答案,但我对它进行了编辑,以建议我如何接近你试图实现的目标。没关系,我让它有点不同,RelativeLayout->LinearLayout->RelativeLayout。在我的情况下,我必须这样做,因为我必须定位元素。我已经完成了我所寻找的,谢谢评论。请分享这一点作为答案。许多好奇的家伙想知道你是如何做到的。请检查你的问题。它可能会误导读者,让读者认为您希望将
框架布局
作为一个组视图,在该组视图中,您希望图像的子项
相对长度
重叠在另一个上。但是,FYKI,如果您可以使用
RelativeLayout
实现此覆盖,并且如果它的所有子项在
布局宽度和
布局高度上都具有匹配父项,而不指定彼此之间的关系。方向和权重是
LinearLayout
的属性。您确定
android:Orientation
RelativeLayout
的属性吗?兄弟,该属性属于
LinearLayout
。滚动怎么样?这是由
水平滚动视图
给出的。请至少详细说明答案!