0%

[原创] Android ListView 在右上角添加三角形图标和文字

最终显示效果如下图,在右上角添加三角形图标并在图标内显示文字:

注意:右上角的红色三角形和里面的文字不是图片。
原理是使用 Drawable 画出一个正方形,然后将其旋转 45 度,使其达到三角形的效果。
虽然可以直接使用 TextView 将其旋转 45 度,并添加背景 Drawable,但是这么做完之后我发现,如果想调整文字在三角形中的相对位置或者调整三角形的大小,却不是很容易。
因此我的方案是,使用 View 单独显示三角形背景,然后再使用 TextView 单独显示文字。这样做的好处就是可以随意的调整文字的相对位置以及三角形的大小。最终效果详见上图。
代码如下:在 drawable 文件夹下新建 triangle_shape.xml 文件,用以显示三角形图标。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="-45"
android:toDegrees="45"
android:pivotX="0%"
android:pivotY="-45%" >
<shape android:shape="rectangle" >
<!-- Border -->
<stroke
android:width="10dp"
android:color="@color/red" />
<!-- Background -->
<solid android:color="@color/red" />
</shape>
</rotate>
</item>
</layer-list>

在自己的 Layout 中,使用 triangle_shape.xml 作为背景,并添加三角形内需要显示的文字。
核心代码如下:

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
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/thumbnail"
android:layout_width="150dp"
android:layout_height="85dp"
fresco:placeholderImage="@drawable/placehoderImg" />
<FrameLayout
android:id="@+id/onAirLayout"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/thumbnail"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/thumbnail">
<!-- Attention Please -->
<!--layout_width must be equal to layout_height and its value plus layout_marginBottom must be equal 85-->
<!--PS: 85 is the height of placeholder image-->
<View
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_gravity="top|right|end"
android:layout_marginBottom="-30dp"
android:background="@drawable/triangle_shape"
android:rotation="0" />
<TextView
android:id="@+id/txtOnAir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|end"
android:layout_marginTop="12dp"
android:rotation="45"
android:text="@string/cmn_on_air"
android:textColor="@android:color/white"
android:textSize="14sp" />
</FrameLayout>
</RelativeLayout>

完整代码如下:

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/thumbnail"
android:layout_width="150dp"
android:layout_height="85dp"
fresco:placeholderImage="@drawable/placehoderImg" />
<FrameLayout
android:id="@+id/onAirLayout"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/thumbnail"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/thumbnail">
<!-- Attention Please -->
<!--layout_width must be equal to layout_height and its value plus layout_marginBottom must be equal 85-->
<!--PS: 85 is the height of placeholder image-->
<View
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_gravity="top|right|end"
android:layout_marginBottom="-30dp"
android:background="@drawable/triangle_shape"
android:rotation="0" />
<TextView
android:id="@+id/txtOnAir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|end"
android:layout_marginTop="12dp"
android:rotation="45"
android:text="@string/cmn_on_air"
android:textColor="@android:color/white"
android:textSize="14sp" />
</FrameLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="3dp"
android:ellipsize="end"
android:maxLines="1"
android:text="Title"
android:textAppearance="?attr/textAppearanceListItem"
android:textColor="@android:color/black"
android:textStyle="bold" />
<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="-3dp"
android:drawableLeft="@drawable/ic_place"
android:drawablePadding="3dp"
android:ellipsize="end"
android:gravity="fill_vertical"
android:maxLines="1"
android:text="Sub Title"
android:textColor="@color/dim_gray"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</FrameLayout>

参考文献:
https://stackoverflow.com/a/32223301/6091500

坚持原创及高品质技术分享,您的支持将鼓励我继续创作!