日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android实现ExpandableTextView可扩展TextView

發布時間:2024/1/17 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android实现ExpandableTextView可扩展TextView 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

介紹

在應用開發中,總會遇到一些類似于公告,說明等長文本的TextView,但是為了排版美觀等因素,我們通常是要隱藏后半部的文本,只顯示部分文字,然后在尾部會提供用戶一個擴展/收縮的按鈕,使得文本框可以在需要的時候擴展開來查看全文,這就需要實現一個ExpendableTextView,類似于ExpendableList。

原理

1、開始時使用android:lines來設置TextView的行數,點擊按鈕之后,解除限制。

2、使用android:ellipsize來設置文本的省略位置。

3、要記得設置android:layout_height="wrap_content",不然固定了高度,就沒法實現了。

實現

main.xml布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.appupdate.MainActivity" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ellipsize="end"android:lines="3"android:text="我是說明我是公告我是說明我是公告我是說明我是公告我是說明我是公告我是說明我是公告我是說明我是公告我是說明我是公告我是說明我是公告我是說明我是公告我是說明我是公告我是說明我是公告我是說明我是公告我是說明我是公告我是說明我是公告我是說明我是公告我是說明我是公告我是說明我是公告我是說明我是公告我是說明我是公告我是說明我是公告我是說明我是公告"android:textColor="@android:color/black" /><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/textView1"android:layout_centerHorizontal="true"android:text="點擊展開" /></RelativeLayout>

核心代碼

  private TextView textView1;private Button button1;private boolean isExpend = false;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView1 = (TextView) findViewById(R.id.textView1);button1 = (Button) findViewById(R.id.button1);button1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (!isExpend) {// 復原textView1.setMinLines(0);textView1.setMaxLines(Integer.MAX_VALUE);button1.setText("點擊收縮");isExpend=true;} else {textView1.setLines(3);button1.setText("點擊展開");isExpend=false;}}});}

?

運行之后看起來是這樣的

?

找個時候好好重構一下,做一個自定義控件,實現真正意義上的ExpendableTextView

轉載于:https://www.cnblogs.com/leestar54/p/4246068.html

總結

以上是生活随笔為你收集整理的Android实现ExpandableTextView可扩展TextView的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。