文章

RelativeLayout

RelativeLayout

RelativeLayout

效果

RelativeLayout 左右靠齐,不重叠

示例 1:左边固定,右边依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:background="#f00"
        android:text="Left Text"
        android:textColor="#fff"
        android:textSize="16sp" />

    <!--  多嵌套一层LinearLayout, 是为了让右侧TextView宽度自适应,否则是撑满右侧的-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@id/tv_title"
        android:background="#f0f"
        android:gravity="end">

        <TextView
            android:id="@+id/tv_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00f"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="Right Text Right Text Right Text Right Text Right Text"
            android:textColor="#fff"
            android:textSize="16sp" />
    </LinearLayout>

</RelativeLayout>

效果:

image.png

image.png

示例 2:右边固定,左边依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<RelativeLayout
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:layout_marginTop="8dp"
	android:layout_marginBottom="8dp">

	<TextView
		android:id="@+id/tv_risk_source_time"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_alignParentEnd="true"
		android:layout_centerVertical="true"
		android:maxLines="1"
		android:paddingTop="1dp"
		android:textColor="#FF8C8C8C"
		android:textSize="12sp"
		tools:text="汇财汇资讯 · 2022-08-08" />

<!--多嵌套一层Layout,是为了子控件宽度自适应-->
	<LinearLayout
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_alignParentStart="true"
		android:layout_marginEnd="6dp"
		android:layout_toStartOf="@id/tv_risk_source_time">

		<TextView
			android:id="@+id/tv_risk_company"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:background="@drawable/shape_bg_r1_fff5f9fd"
			android:ellipsize="end"
			android:maxLines="1"
			android:paddingStart="6dp"
			android:paddingTop="2dp"
			android:paddingEnd="6dp"
			android:paddingBottom="2dp"
			android:textColor="#FF1482F0"
			android:textSize="12sp"
			tools:text="122插上的擦拭成大事的擦飒飒的插上的擦拭超大上档次茶水单擦上档次" />

	</LinearLayout>

</RelativeLayout>

image.png

本文由作者按照 CC BY 4.0 进行授权