Android超簡(jiǎn)單實(shí)現(xiàn)文字展開收起效果

目錄
目錄
效果展示
image
實(shí)現(xiàn)原理

原理很簡(jiǎn)單鸵熟,需要用3個(gè)TextView,其中一個(gè)是展示文字的流强,一個(gè)是用來計(jì)算文字的行數(shù),一個(gè)是用來展開和收起的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<!--    展示文字-->
    <TextView
        android:id="@+id/view_seemore_tvcontent"
        android:textColor="#000"
        android:maxLines="2"
        android:ellipsize="end"
        android:textSize="15sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
<!--    占位置的隱藏队腐,并且高度設(shè)置的很小奏篙,用來獲取行數(shù)-->
    <TextView
        android:id="@+id/view_seemore_tvlinecount"
        android:textColor="#000"
        android:visibility="invisible"
        android:textSize="15sp"
        android:layout_width="match_parent"
        android:layout_height="1dp"/>
<!--    顯示更多和收起-->
    <TextView
        android:id="@+id/view_seemore_tv_seemore"
        android:layout_marginTop="5dp"
        android:text="查看更多"
        android:textColor="#00f"
        android:visibility="gone"
        android:maxLines="2"
        android:ellipsize="end"
        android:textSize="15sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

然后需要在自定義控件的類中做相應(yīng)的處理:

class SeeMoreView:FrameLayout {
    //是否展開和收起的標(biāo)記
    private var mIsShowAll:Boolean = false
    constructor(context: Context) : this(context,null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs,0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ){
        View.inflate(context, R.layout.view_seemore,this)
        initListener()
    }
    private fun initListener() {
        //查看更多的點(diǎn)擊事件
        view_seemore_tv_seemore.setOnClickListener {
            if(mIsShowAll){
                //這是收起的關(guān)鍵代碼,將最大行數(shù)設(shè)置為你想展示的最小行數(shù)即可
                view_seemore_tvcontent.maxLines = 2
                view_seemore_tv_seemore.text = "查看更多"
            }else{
                //這是查看更多的關(guān)鍵代碼报破,將最大行數(shù)設(shè)置一個(gè)大數(shù)即可
                view_seemore_tvcontent.maxLines = 20
                view_seemore_tv_seemore.text = "收起"
            }
            mIsShowAll = !mIsShowAll
        }
        //attachedToWindow之后執(zhí)行操作
        post {
            //這里必須這樣寫,這是在attachedToWindow之后執(zhí)行操作充易,否則獲取行數(shù)會(huì)出問題
            Log.e("測(cè)試","OnLayout${view_seemore_tvlinecount.lineCount}")
            if(view_seemore_tvlinecount.lineCount>2){
                view_seemore_tv_seemore.visibility = View.VISIBLE
            }else{
                view_seemore_tv_seemore.visibility = View.GONE
            }
        }

    }
    /**
     * 設(shè)置文字
     */
    fun setText(text:String){
        //每次設(shè)置文字后都要進(jìn)行重置
        view_seemore_tvcontent.text = text
        view_seemore_tvlinecount.text = text
        view_seemore_tv_seemore.text = "查看更多"
        view_seemore_tvcontent.maxLines = 2
        mIsShowAll = false
        if(view_seemore_tvlinecount.lineCount>2){
            view_seemore_tv_seemore.visibility = View.VISIBLE
        }else{
            view_seemore_tv_seemore.visibility = View.GONE
        }
    }
}

項(xiàng)目源碼

https://github.com/myml666/SeeMore

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末盹靴,一起剝皮案震驚了整個(gè)濱河市炸茧,隨后出現(xiàn)的幾起案子稿静,更是在濱河造成了極大的恐慌,老刑警劉巖改备,帶你破解...
    沈念sama閱讀 216,843評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異盐捷,居然都是意外死亡偶翅,警方通過查閱死者的電腦和手機(jī)碉渡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,538評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門滞诺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人习霹,你說我怎么就攤上這事。” “怎么了等限?”我有些...
    開封第一講書人閱讀 163,187評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)形娇。 經(jīng)常有香客問我筹误,道長(zhǎng),這世上最難降的妖魔是什么厨剪? 我笑而不...
    開封第一講書人閱讀 58,264評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮祷膳,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘搀军。我一直安慰自己,他們只是感情好勇皇,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,289評(píng)論 6 390
  • 文/花漫 我一把揭開白布罩句。 她就那樣靜靜地躺著,像睡著了一般敛摘。 火紅的嫁衣襯著肌膚如雪门烂。 梳的紋絲不亂的頭發(fā)上兄淫,一...
    開封第一講書人閱讀 51,231評(píng)論 1 299
  • 那天诅福,我揣著相機(jī)與錄音,去河邊找鬼氓润。 笑死赂乐,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的咖气。 我是一名探鬼主播挨措,決...
    沈念sama閱讀 40,116評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼崩溪,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼浅役!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起伶唯,我...
    開封第一講書人閱讀 38,945評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤觉既,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后乳幸,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,367評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡粹断,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,581評(píng)論 2 333
  • 正文 我和宋清朗相戀三年符欠,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了希柿。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,754評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡曾撤,死狀恐怖盾戴,靈堂內(nèi)的尸體忽然破棺而出兵多,到底是詐尸還是另有隱情尖啡,我是刑警寧澤剩膘,帶...
    沈念sama閱讀 35,458評(píng)論 5 344
  • 正文 年R本政府宣布怠褐,位于F島的核電站畏梆,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜奠涌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,068評(píng)論 3 327
  • 文/蒙蒙 一宪巨、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧溜畅,春花似錦捏卓、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,692評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至浴捆,卻和暖如春蒜田,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背选泻。 一陣腳步聲響...
    開封第一講書人閱讀 32,842評(píng)論 1 269
  • 我被黑心中介騙來泰國打工冲粤, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人页眯。 一個(gè)月前我還...
    沈念sama閱讀 47,797評(píng)論 2 369
  • 正文 我出身青樓梯捕,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親餐茵。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,654評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容