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

目錄

image

前言

公司項(xiàng)目需要一個(gè)類似微信朋友圈文字展開收起的效果,為了方便一開始我在網(wǎng)上找類似的效果實(shí)現(xiàn),結(jié)果發(fā)現(xiàn)代碼量都很多計(jì)算量很大,而我頭腦這么簡單所以看起來比較費(fèi)勁因此我就用非常簡單的方法寫了一個(gè)自定義的展開收起控件。

效果展示

image

實(shí)現(xià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("測試","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/dahui888/SeeMore

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 目錄 前言 公司項(xiàng)目需要一個(gè)類似微信朋友圈文字展開收起的效果,為了方便一開始我在網(wǎng)上找類似的效果實(shí)現(xiàn),結(jié)果發(fā)現(xiàn)代碼...
    itfitness閱讀 4,235評(píng)論 0 20
  • 1.計(jì)算目標(biāo) 2.編寫程序 3.編譯程序 4.運(yùn)行程序 5.顯示結(jié)果
    一路向后閱讀 1,959評(píng)論 0 0
  • 說起跑步,現(xiàn)在的我可謂是別人眼中的強(qiáng)者。雖然跑步時(shí)間不算太久,從2015年5月17號(hào)到今天算下來好像剛剛好100天...
    靈犀一點(diǎn)閱讀 1,028評(píng)論 1 50
  • 清明朔雨風(fēng)悲泣。卻到我、猶如喜。錦帽龍披街畔里。子規(guī)春送,瓦灰墻圮,一路行人密。 夜天佇樓明燈細(xì)。玉宇十年?可難憶...
    東武居士閱讀 549評(píng)論 0 4

友情鏈接更多精彩內(nèi)容