如何在RelativeLayout中将布局放在其他布局之间?

时间:2017-07-25 09:01:33

标签: android android-layout android-view android-relativelayout view-hierarchy

我有RelativeLayout。在里面我有:

  • 右侧ImageView 120x120 dp。
  • 左边的3个其他布局:
    • 第一个布局(名为顶部)有alignParentTop=true
    • 第二个布局(名为底部)有alignParentBottom=true
    • 第3个布局(名为中间)位于中间(顶部底部之上)。

问题是:如果我为容器设置了layout_width="wrap_content"RelativeLayout),我就看不到中间布局。

如果我将其设置为某些值(例如:144dp),我会看到中间布局。

这是布局结构(我隐藏了一些子布局,只显示了主要布局)。

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <LinearLayout
        android:id="@+id/top"
        android:background="#eeaaee"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom"
        android:background="#22eeaa"
        android:layout_toLeftOf="@+id/image"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </LinearLayout>

    <LinearLayout
        android:id="@+id/middle"
        android:layout_width="match_parent"
        android:background="#44ee22"
        android:layout_toLeftOf="@+id/image"
        android:layout_height="64dp"
        android:layout_below="@+id/top"
        android:layout_above="@+id/bottom">
        <TextView
            android:id="@+id/hotnews_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="14sp"
            />
    </LinearLayout>

    <ImageView
            android:id="@+id/image"
            android:layout_alignParentEnd="true"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:scaleType="centerCrop"/>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

如果你想在一些布局之间放置布局而不是更好地使用linearlayout作为所有三个(顶部,中间,底部)布局的父级,并使用它的方向和重量。我使用了fixedlayout的固定高度来区分顶视图,中视图和底视图。根据你的需要改变它。

    Me.Location = New Point(27, 55)
    Me.Location = New Point(502, 624)
    Me.Location = New Point(858, 477)
    Me.Location = New Point(564, 50)
    Me.Location = New Point(898, 41)
    Me.Location = New Point(468, 944)
    Me.Location = New Point(417, 7)
    Me.Location = New Point(841, 697)
    Me.Location = New Point(953, 438)