【问题标题】:RelativeLayout weight相对布局权重
【发布时间】:2012-12-10 03:51:21
【问题描述】:

在布局资源 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=1layout_width=0dp,并且这种技术适用于按钮,我认为 relativeLayout 也是如此,看来我的想法是错误的。有什么想法吗?

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

【问题讨论】:

  • 我已经完成了我想要的工作,感谢 cmets
  • 请作为答案分享。很多好奇的小伙伴都想知道你是怎么做到的。
  • 请检查您的问题。它可能会误导读者,您希望 FrameLayout 作为一个组视图,您希望图像的子级 RelativeLayout 将一个覆盖在另一个之上。但是,FYKI,如果您也可以使用 RelativeLayout 实现这种覆盖,如果它的所有子项在 layout_widthlayout_height 上都有 match_parent 而没有指定彼此之间的关系。方向和权重是LinearLayout 的属性。

标签: android android-layout


【解决方案1】:

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,如果这样有助于更好地组织布局。

【讨论】:

  • 我在 relativeLayout 中确实有元素,必须以精确的方式定位,这将是一种带有图像等的自定义可点击元素。由于我的编码知识很差
  • @Daler - 如果您提供有关所需布局的更多详细信息(草图或屏幕截图会很棒),我们可以帮助您实现它。另请注意,垂直 LinearLayout 可以包含嵌套布局,以便在需要的任何行上提供水平组织。
  • 感谢您的反馈,我已经更新了我的帖子,您可以看到我想要的。我想让每个 relativeLayout 可点击,所以它们将充当一个按钮。三者都具有相同的设计,但图像类型和文本不同
  • @Daler - 我看到你已经接受了我的回答,但我对其进行了编辑以建议我将如何处理你想要完成的事情。
  • 没关系,我做了一点不同,RelativeLayout->LinearLayout->Relativelayout。就我而言,我必须这样做,因为我必须定位元素。
【解决方案2】:

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

【讨论】:

  • 如果父级是relativeLayout怎么办?我可以在linearLayout中添加其余的relativeLayouts。有点硬核但可能
  • 我猜你可以有一个RelativeLayout -> LinearLayout -> RelativeLayout x 3,但我不确定那会完成什么。
【解决方案3】:

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

对于所有这些情况来说,这是一个小技巧。

android:orientation="horizo​​ntal" 中使用 LinearLayout

【讨论】:

  • 您确定 android:orientationRelativeLayout 的属性吗?兄弟,那个属性属于LinearLayout。滚动呢?这是由HorizontalScrollView 给出的。请至少详细说明答案!
【解决方案4】:

您可以在Recycler View 中使用面向LinearLayout ManagerHorizontally,并将每个RelativeLayout 放置在其Adapter 的每个项目中。

链接:How to build a Horizontal ListView with RecyclerView?

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

链接:Get Screen width and height

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

请看:How to create RecyclerView with multiple view type?

快乐编码:-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 2011-06-26
    相关资源
    最近更新 更多