22

在布局资源 XML 中,我有 3 个 RelativeLayout(s),它们位于主 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=1andlayout_width=0dp并且这种技术适用于按钮,我认为 relativeLayout 也是如此,看来我的想法是错误的。任何的想法?

UPD1:我添加了一张我想要的图片

在此处输入图像描述

4

4 回答 4

25

RelativeLayout不注意android:layout_weight。(这是 的属性LinearLayout.LayoutParams,但不是 的属性RelativeLayout.LayoutParams。)

您应该能够使用更简单的视图层次结构获得所需的布局。目前尚不清楚您要做什么,因为最后两个RelativeLayouts 是空的。如果您需要一个纯粹的垂直组织,我建议您使用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视图替换为RelativeLayout是否有助于更好地组织布局。

于 2012-12-23T07:01:00.607 回答
8

相对布局不支持权重。如果要使用权重,则需要使用 LinearLayout 作为父容器。

于 2012-12-23T06:59:02.260 回答
0

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

对于所有这些类型的情况,这是一个小技巧。

使用带有 android:orientation=" horizo​​ntal" 的 LinearLayout

于 2014-03-03T21:47:00.530 回答
0

您可以在 中使用orient Horizontally,并将其放置在每个项目中。LinearLayout ManagerRecycler ViewRelativeLayoutAdapter

链接:如何使用 RecyclerView 构建水平 ListView?

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

链接:获取屏幕宽度和高度

如果您的 RelativeLayouts 的内容不同,那么您可以使用getItemViewType()方法。

请参阅:如何创建具有多种视图类型的 RecyclerView?

快乐编码:-)

于 2019-01-25T06:14:59.847 回答