[英]How to put layout between others layout in RelativeLayout?
I have a RelativeLayout
. 我有一个RelativeLayout
。 Inside it I have: 在它里面我有:
ImageView
120x120 dp in the right. 右侧的ImageView
120x120 dp。 alignParentTop=true
第一种布局(称为Top )具有alignParentTop=true
alignParentBottom=true
第二布局(称为Bottom )具有alignParentBottom=true
The problem is: if I set layout_width="wrap_content"
for the container ( RelativeLayout
), I don't see the Middle layout. 问题是:如果我为容器( RelativeLayout
)设置layout_width="wrap_content"
,则看不到中间布局。
And if I set it to some values (for example: 144dp
) I will see the Middle layout. 如果将其设置为某些值(例如: 144dp
),我将看到中间布局。
Here is layout structure (I hide some child layouts inside it and shows only main layouts). 这是布局结构(我在其中隐藏了一些子布局,仅显示主要布局)。
<?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>
if you want to put layout between some layout than its better to use linearlayout as parent of all the three (top,middle,bottom) layout.and use its orientation and weight. 如果您想将布局放置在某个布局之间,则最好使用linearlayout作为所有三个(顶部,中间,底部)布局的父级,并使用其方向和权重。 i have used fixed height of linearlayout just to differenciate the top,middle and bottom view. 我已经使用了固定高度的linearlayout来区别顶视图,中视图和底视图。 change it according to your need. 根据您的需要进行更改。
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:id="@+id/top"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#eeaaee"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#22eeaa"
android:orientation="horizontal" />
<LinearLayout
android:id="@+id/middle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#44ee22">
<TextView
android:id="@+id/hotnews_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/image"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_weight="1"
android:scaleType="centerCrop" />
</LinearLayout>
</RelativeLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.