本文实例讲述了Android开发实现的文本折叠点击展开功能。分享给大家供大家参考,具体如下:
信息栏,景点介绍,购物信息,进场会使用到文本折叠的方法
实现非常简单,这里就不哆嗦了
效果如下:
Demo:https://github.com/LonglyWolf/NavigationSystemHLJU
这里用到了三方类库,在app/gradle添加依赖如下:
1
2
|
//文本过长 点击展开全部 implementation 'com.ms-square:expandableTextView:0.1.4' |
上面的实例是通过adapter就和listView实现的,这里就不搞那么复杂,直接看折叠文本的方法实现:
首先是主活动:
1
2
3
4
5
6
7
8
9
|
@Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // sample code snippet to set the text content on the ExpandableTextView ExpandableTextView expTv1 = (ExpandableTextView) findViewById(R.id.expand_text_view); // IMPORTANT - call setText on the ExpandableTextView to set the text content to display expTv1.setText( "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq" ); } |
重点在于布局文件的设置:
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
|
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto" xmlns:tools = "http://schemas.android.com/tools" android:id = "@+id/container" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = ".MainActivity" > < com.ms.square.android.expandabletextview.ExpandableTextView xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:expandableTextView = "http://schemas.android.com/apk/res-auto" android:id = "@+id/expand_text_view" android:layout_width = "match_parent" android:layout_height = "wrap_content" expandableTextView:maxCollapsedLines = "4" expandableTextView:animDuration = "200" > < TextView android:id = "@id/expandable_text" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginLeft = "10dp" android:layout_marginRight = "10dp" android:textSize = "16sp" android:textColor = "#666666" /> < ImageButton android:id = "@id/expand_collapse" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:padding = "16dp" android:layout_gravity = "right|bottom" android:background = "@android:color/transparent" /> </ com.ms.square.android.expandabletextview.ExpandableTextView > </ LinearLayout > |
希望本文所述对大家Android程序设计有所帮助。
原文链接:https://blog.csdn.net/qq_43377749/article/details/84727627