在TextView前面(或后面)添加文本(或者圖片)標(biāo)簽

前言

大家都知道,前幾年在TextView文本前添加自定義標(biāo)簽文本或標(biāo)簽圖片還是比較火的,特別是一些電商項(xiàng)目。不過這幾年,UI設(shè)計(jì)基本很少見到這種設(shè)計(jì)了,不過還是有些時(shí)候,還是會(huì)遇到一些在文本前加標(biāo)簽需求。如果使用TextView自帶的drawableLeft屬性,雖然可以滿足一些常規(guī)需求,但是可定制化太低,而且文本換行時(shí)圖片和文本會(huì)分離,達(dá)不到想要的效果。當(dāng)然,我這里也只是提供了一種方法,實(shí)現(xiàn)一個(gè)需求是有千千萬萬種方法的!

效果圖

廢話不多說,按照慣例,來一波圖:

有的朋友肯定會(huì)說這很簡(jiǎn)單啊,對(duì)的,的確很簡(jiǎn)單。其實(shí)我就是把常用的一些方法寫到一起,供大家調(diào)用,而且不用修改原來的代碼就可以設(shè)置標(biāo)簽,當(dāng)然還有一些其他方法(比如:下劃線,刪除線,格式文本顏色,超鏈等等),只需要調(diào)用對(duì)應(yīng)方法就可以了,節(jié)約大波時(shí)間。

如何使用

Step 1.先在 build.gradle(Project:XXX) 的 repositories 添加:

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

Step 2. 然后在 build.gradle(Module:XXX) 的 dependencies 添加:

dependencies {
       implementation 'com.github.ChinaLike:TagTextView:0.0.8'
}

Step 3. 在XML中引用即可(其中一種方式舉例):

    <com.view.text.TagTextView
        android:id="@+id/tagTextView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginVertical="10dp"
        android:text="榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 支持移動(dòng)聯(lián)通電信"
        android:textColor="#666666"
        app:tvt_tag_background_color="#FF6464"
        app:tvt_tag_bottom_padding="1dp"
        app:tvt_tag_left_padding="20dp"
        app:tvt_tag_right_padding="20dp"
        app:tvt_tag_text="新品"
        app:tvt_tag_space="10dp"
        app:tvt_tag_top_padding="1dp"
        app:tvt_text_space="5dp" />

API介紹

TagTextView功能

xml屬性

API 類型 默認(rèn)值 描述 版本說明 說明
tvt_left_top_radius dimension 標(biāo)簽左上圓角
tvt_left_bottom_radius dimension 標(biāo)簽左下圓角
tvt_right_top_radius dimension 標(biāo)簽右上圓角
tvt_right_bottom_radius dimension 標(biāo)簽右下圓角
tvt_radius dimension 9999dp 標(biāo)簽圓角 當(dāng)單獨(dú)設(shè)置了標(biāo)簽圓角此方法對(duì)應(yīng)方位圓角將失效
tvt_tag_left_padding dimension 10dp 標(biāo)簽左邊內(nèi)邊距
tvt_tag_top_padding dimension 標(biāo)簽頂部內(nèi)邊距
tvt_tag_right_padding dimension 10dp 標(biāo)簽右邊內(nèi)邊距
tvt_tag_bottom_padding dimension 標(biāo)簽底部內(nèi)邊距
tvt_tag_padding dimension 0 標(biāo)簽內(nèi)邊距 當(dāng)單獨(dú)設(shè)置了標(biāo)簽內(nèi)邊距此方法對(duì)應(yīng)方位內(nèi)邊距將失效
tvt_tag_background_color color Color.GRAY 標(biāo)簽背景顏色
tvt_tag_space dimension 0 標(biāo)簽與標(biāo)簽的間隔
tvt_text_space dimension 0 標(biāo)簽與文本的間隔
tvt_tag_location enum start 標(biāo)簽顯示的位置 start開始位置,end結(jié)束位置
tvt_tag_start_background_color color 漸變背景開始顏色
tvt_tag_end_background_color color 漸變背景結(jié)束顏色
tvt_tag_text_size dimension 14sp 標(biāo)簽文本大小
tvt_tag_text_color color Color.WHITE 標(biāo)簽文本顏色
tvt_first_tag_left_space dimension 0 第一個(gè)標(biāo)簽距離左邊距離
tvt_tag_text string 標(biāo)簽內(nèi)容 如果tvt_tag_text 、tvt_tag_image都設(shè)置的話,只有tvt_tag_image生效
tvt_tag_image reference 圖片標(biāo)簽 如果tvt_tag_text 、tvt_tag_image都設(shè)置的話,只有tvt_tag_image生效
tvt_tag_width dimension 標(biāo)簽的自定義寬度
tvt_tag_height dimension 標(biāo)簽的自定義高度

屬性

API 類型 默認(rèn)值 描述 版本說明 說明
leftTopRadius Float 標(biāo)簽左上圓角
leftBottomRadius Float 標(biāo)簽左下圓角
rightTopRadius Float 標(biāo)簽右上圓角
rightBottomRadius Float 標(biāo)簽右下圓角
radius Float 9999F 標(biāo)簽圓角 當(dāng)單獨(dú)設(shè)置了標(biāo)簽圓角此方法對(duì)應(yīng)方位圓角將失效
tagLeftPadding Int 10dp 標(biāo)簽左邊內(nèi)邊距
tagTopPadding Int 標(biāo)簽頂部內(nèi)邊距
tagRightPadding Int 10dp 標(biāo)簽右邊內(nèi)邊距
tagBottomPadding Int 標(biāo)簽底部內(nèi)邊距
tagPadding Int 0 標(biāo)簽內(nèi)邊距 當(dāng)單獨(dú)設(shè)置了標(biāo)簽內(nèi)邊距此方法對(duì)應(yīng)方位內(nèi)邊距將失效
tagBackgroundColor Int Color.GRAY 標(biāo)簽背景顏色
tagSpace Int 0 標(biāo)簽與標(biāo)簽的間隔
textSpace Int 0 標(biāo)簽與文本的間隔
tagLocation Int TagLocation.START 標(biāo)簽顯示的位置 TagLocation.START開始位置,TagLocation.END結(jié)束位置
tagStartBackgroundColor Int 漸變背景開始顏色
tagEndBackgroundColor Int 漸變背景結(jié)束顏色
tagTextSize Float 14sp 標(biāo)簽文本大小
tagTextColor Int Color.WHITE 標(biāo)簽文本顏色
firstTagLeftSpace Int 0 第一個(gè)標(biāo)簽距離左邊距離
tagText String 標(biāo)簽內(nèi)容 如果tagTexttagImage都設(shè)置的話,只有tagImage生效
tagImage Drawable 圖片標(biāo)簽 如果tagText 、tagImage都設(shè)置的話,只有tagImage生效
tagWidth Int 標(biāo)簽的自定義寬度
tagHeight Int 標(biāo)簽的自定義高度

方法

  • setTextTag(params)

    設(shè)置標(biāo)簽

    params:標(biāo)簽內(nèi)容,支持以下格式

     BaseTagAdapter<*>:自定義適配器,自己實(shí)現(xiàn)標(biāo)簽顯示樣式,自定義中只有tvt_tag_space、tvt_text_space、tvt_tag_location、tvt_first_tag_left_space字段有效
    
     MutableList<T>:多個(gè)標(biāo)簽,T支持String、DrawableRes、Bitmap、Drawable
    
     String:字符串標(biāo)簽,如果是一個(gè)只需要傳入一個(gè)字符串,多個(gè)就傳入多個(gè)字符串
    
     Bitmap:圖片標(biāo)簽,如果是一個(gè)只需要傳入一個(gè)Bitmap,多個(gè)就傳入多個(gè)Bitmap
    
     Int:圖片標(biāo)簽,如果是一個(gè)只需要傳入一個(gè)DrawableRes,多個(gè)就傳入多個(gè)DrawableRes
    
     Drawable:圖片標(biāo)簽,如果是一個(gè)只需要傳入一個(gè)Drawable,多個(gè)就傳入多個(gè)Drawable
    

擴(kuò)展功能(不支持xml配置樣式)

  • setTextTag(config:TagConfig,params:Any)

    設(shè)置標(biāo)簽

    config: 標(biāo)簽樣式,可選,參考TagConfig

    params:標(biāo)簽內(nèi)容,支持以下格式:

     BaseTagAdapter<*>:自定義適配器,自己實(shí)現(xiàn)標(biāo)簽顯示樣式MutableList<T>:多個(gè)標(biāo)簽,T支持String、DrawableRes、Bitmap、Drawable
     
     String:字符串標(biāo)簽,如果是一個(gè)只需要傳入一個(gè)字符串,多個(gè)就傳入多個(gè)字符串
     
     Bitmap:圖片標(biāo)簽,如果是一個(gè)只需要傳入一個(gè)Bitmap,多個(gè)就傳入多個(gè)Bitmap
     
     Int:圖片標(biāo)簽,如果是一個(gè)只需要傳入一個(gè)DrawableRes,多個(gè)就傳入多個(gè)DrawableRes
     
     Drawable:圖片標(biāo)簽,如果是一個(gè)只需要傳入一個(gè)Drawable,多個(gè)就傳入多個(gè)Drawable
    
  • setUnderline(underline: String)

    設(shè)置文本下劃線

    underline:需要加下劃線的文本,如果多個(gè)一樣,只有第一個(gè)加下劃線

  • setUnderline(startIndex: Int, endIndex: Int)

    設(shè)置文本下劃線

    startIndex:開始下標(biāo)

    endIndex:結(jié)束下標(biāo)

  • setUnderline(vararg indexRang: IntArray)

    設(shè)置文本下劃線

    indexRang:整型數(shù)組,大小為2,可傳多組

  • setUnderline()

    設(shè)置文本下劃線

    所有文本都設(shè)置下劃線

  • setDeleteLine(underline: String)

    設(shè)置文本刪除線

    underline:需要加刪除線的文本,如果多個(gè)一樣,只有第一個(gè)加刪除線

  • setDeleteLine(startIndex: Int, endIndex: Int)

    設(shè)置文本刪除線

    startIndex:開始下標(biāo)

    endIndex:結(jié)束下標(biāo)

  • setDeleteLine(vararg indexRang: IntArray)

    設(shè)置文本刪除線

    indexRang:整型數(shù)組,大小為2,可傳多組

  • setDeleteLine()

    設(shè)置文本刪除線

    所有文本都設(shè)置刪除線

  • setSpecificTextColor(@ColorInt color: Int,specificText: String,isUnderlineText: Boolean = false,onTagClickListener: OnTagClickListener? = null)

    設(shè)置指定文字顏色

    color:制定文本顏色

    specificText:指定文本

    isUnderlineText:是否顯示下劃線,可選,默認(rèn)值false

    onTagClickListener:點(diǎn)擊事件響應(yīng),可選,-1文本被點(diǎn)擊,其他是對(duì)應(yīng)下標(biāo)被點(diǎn)擊

  • setSpecificTextColor(@ColorInt color: Int,startIndex: Int,endIndex: Int,isUnderlineText: Boolean = false,onTagClickListener: OnTagClickListener? = null)

    設(shè)置指定文字顏色

    color:制定文本顏色

    startIndex:開始下標(biāo)

    endIndex:結(jié)束下標(biāo)

    isUnderlineText:是否顯示下劃線,可選,默認(rèn)值false

    onTagClickListener:點(diǎn)擊事件響應(yīng),可選,-1文本被點(diǎn)擊,其他是對(duì)應(yīng)下標(biāo)被點(diǎn)擊

  • setSpecificTextColor(data: MutableList<SpanConfig>,onTagClickListener: OnTagClickListener? = null)

    設(shè)置指定文字顏色

    data:自定義多個(gè)指定文本的顏色

    onTagClickListener:點(diǎn)擊事件響應(yīng),可選,-1文本被點(diǎn)擊,其他是對(duì)應(yīng)下標(biāo)被點(diǎn)擊

  • setURLSpan(startIndex: Int,endIndex: Int,type: SpanType,linkText: String,@ColorInt color: Int? = null,isUnderlineText: Boolean = false)

    設(shè)置超鏈

    startIndex:開始下標(biāo)

    endIndex:結(jié)束下標(biāo)

    type:超鏈接類型,參考SpanType

    linkText:鏈接文本 ,比如跳轉(zhuǎn)電話,只需要傳入電話號(hào)碼就可以

    color:超鏈文本的顏色

    isUnderlineText:是否顯示下劃線,可選,默認(rèn)值false

  • setURLSpan(data: MutableList<URLSpanConfig>?)

    設(shè)置超鏈

    data:自定義超鏈,參考URLSpanConfig

示例介紹

普通標(biāo)簽

  • xml實(shí)現(xiàn)方式
        <com.view.text.TagTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)"
                android:textColor="#666666"
                app:tvt_tag_background_color="#FF0040"
                app:tvt_tag_bottom_padding="1dp"
                app:tvt_tag_left_padding="15dp"
                app:tvt_tag_right_padding="15dp"
                app:tvt_tag_top_padding="1dp"
                app:tvt_tag_text="新品" />
  • Java代碼實(shí)現(xiàn)方式
        TagTextView tagTextView = new TagTextView(this);
        tagTextView.setTagLeftPadding((int) DensityUtil.INSTANCE.dp2px(this,15F));
        tagTextView.setTagRightPadding((int) DensityUtil.INSTANCE.dp2px(this,15F));
        tagTextView.setTagTopPadding((int) DensityUtil.INSTANCE.dp2px(this,1F));
        tagTextView.setTagBottomPadding((int) DensityUtil.INSTANCE.dp2px(this,1F));
        tagTextView.setTagBackgroundColor(Color.parseColor("#FF0040"));
        tagTextView.setText("榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)");
        tagTextView.setTextTag("新品");
  • Kotlin代碼實(shí)現(xiàn)方式
    TagTextView(this).apply {
            tagLeftPadding = DensityUtil.dp2px(this@MainActivity3, 15f).toInt()
            tagRightPadding = DensityUtil.dp2px(this@MainActivity3, 15f).toInt()
            tagTopPadding = DensityUtil.dp2px(this@MainActivity3, 1f).toInt()
            tagBottomPadding = DensityUtil.dp2px(this@MainActivity3, 1f).toInt()
            tagBackgroundColor = Color.parseColor("#FF0040")
            text = "榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)"
            setTextTag("新品")
        }

多個(gè)標(biāo)簽

  • xml實(shí)現(xiàn)方式
  1. 第一步
    <com.view.text.TagTextView
        android:id="@+id/tagTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginVertical="10dp"
        android:text="榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)"
        android:textColor="#666666"
        app:tvt_tag_background_color="#FF6464"
        app:tvt_tag_bottom_padding="1dp"
        app:tvt_tag_left_padding="20dp"
        app:tvt_tag_right_padding="20dp"
        app:tvt_tag_space="10dp"
        app:tvt_tag_top_padding="1dp"
        app:tvt_text_space="5dp" />
  1. 第二步(Java實(shí)現(xiàn))
        TagTextView tagTextView = findViewById(R.id.tagTextView);
        tagTextView.setTextTag("新品", "黃金", "心悅");

或(Kotlin實(shí)現(xiàn))

        tagTextView.setTextTag("新品", "黃金", "心悅")
  • Java代碼實(shí)現(xiàn)方式
        TagTextView tagTextView = new TagTextView(this);
        tagTextView.setTagLeftPadding((int) DensityUtil.INSTANCE.dp2px(this,20F));
        tagTextView.setTagRightPadding((int) DensityUtil.INSTANCE.dp2px(this,20F));
        tagTextView.setTagTopPadding((int) DensityUtil.INSTANCE.dp2px(this,1F));
        tagTextView.setTagBottomPadding((int) DensityUtil.INSTANCE.dp2px(this,1F));
        tagTextView.setTagBackgroundColor(Color.parseColor("#FF6464"));
        tagTextView.setText("榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)");
        tagTextView.setTextColor(Color.parseColor("#666666"));
        tagTextView.setTagSpace((int) DensityUtil.INSTANCE.dp2px(this,10F));
        tagTextView.setTextSpace((int) DensityUtil.INSTANCE.dp2px(this,5F));
        tagTextView.setTextTag("新品", "黃金", "心悅");
  • Kotlin代碼實(shí)現(xiàn)方式
        val tagTextView = TagTextView(this).apply {
            tagLeftPadding = DensityUtil.dp2px(this@MainActivity3, 20f).toInt()
            tagRightPadding = DensityUtil.dp2px(this@MainActivity3, 20f).toInt()
            tagTopPadding = DensityUtil.dp2px(this@MainActivity3, 1f).toInt()
            tagBottomPadding = DensityUtil.dp2px(this@MainActivity3, 1f).toInt()
            tagBackgroundColor = Color.parseColor("#FF6464")
            text = "榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)"
            setTextColor(Color.parseColor("#666666"))
            tagSpace = DensityUtil.dp2px(this@MainActivity3, 10f).toInt()
            textSpace = DensityUtil.dp2px(this@MainActivity3, 5f).toInt()
            setTextTag("新品", "黃金", "心悅")
        }

漸變背景

  • xml實(shí)現(xiàn)方式
  1. 第一步
    <com.view.text.TagTextView
        android:id="@+id/tagTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)"
        android:textColor="#666666"
        app:tvt_tag_end_background_color="@color/tagEndBackgroundColor"
        app:tvt_tag_start_background_color="@color/tagStartBackgroundColor"/>
  1. 第二部(Java實(shí)現(xiàn))
        TagTextView tagTextView = findViewById(R.id.tagTextView);
        tagTextView.setTextTag("優(yōu)選", "新上市", "推薦");

或(Kotlin實(shí)現(xiàn))

        tagTextView.setTextTag("優(yōu)選", "新上市", "推薦")
  • Java代碼實(shí)現(xiàn)方式
        TagTextView tagTextView = new TagTextView(this);
        tagTextView.setTagStartBackgroundColor(ContextCompat.getColor(this,R.color.tagStartBackgroundColor));
        tagTextView.setTagEndBackgroundColor(ContextCompat.getColor(this,R.color.tagEndBackgroundColor));
        tagTextView.setText("榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)");
        tagTextView.setTextColor(Color.parseColor("#666666"));
        tagTextView.setTextTag("優(yōu)選", "新上市", "推薦");
  • Kotlin代碼實(shí)現(xiàn)方式
        val tagTextView = TagTextView(this).apply {
            tagStartBackgroundColor = ContextCompat.getColor(this@MainActivity3, R.color.tagStartBackgroundColor)
            tagEndBackgroundColor = ContextCompat.getColor(this@MainActivity3, R.color.tagEndBackgroundColor)
            setText("榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)")
            setTextColor(Color.parseColor("#666666"))
            setTextTag("優(yōu)選", "新上市", "推薦")
        }

普通圖片標(biāo)簽

  • xml實(shí)現(xiàn)方式
    <com.view.text.TagTextView
        android:id="@+id/tagTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)"
        android:textColor="#666666"
        app:tvt_tag_image="@mipmap/notice"
        app:tvt_tag_space="10dp"
        app:tvt_text_space="5dp"/>
  • Java代碼實(shí)現(xiàn)方式
        TagTextView tagTextView = new TagTextView(this);
        tagTextView.setText("榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)");
        tagTextView.setTextColor(Color.parseColor("#666666"));
        tagTextView.setTagSpace((int) DensityUtil.INSTANCE.dp2px(this,10F));
        tagTextView.setTextSpace((int) DensityUtil.INSTANCE.dp2px(this,5F));
        tagTextView.setTextTag(getResources().getDrawable(R.mipmap.notice));
  • Kotlin代碼實(shí)現(xiàn)方式
        val tagTextView = TagTextView(this).apply {
            text = "榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)"
            setTextColor(Color.parseColor("#666666"))
            tagSpace = dp2px(this@MainActivity3, 10f).toInt()
            textSpace = dp2px(this@MainActivity3, 5f).toInt()
            setTextTag(resources.getDrawable(R.mipmap.notice))
        }

多個(gè)圖片標(biāo)簽

  • xml實(shí)現(xiàn)方式
  1. 第一步
    <com.view.text.TagTextView
        android:id="@+id/tagTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)"
        android:textColor="#666666"
        app:tvt_tag_space="10dp"/>
  1. 第二步(Java實(shí)現(xiàn))
        TagTextView tagTextView = findViewById(R.id.tagTextView);
        tagTextView.setTextTag(R.mipmap.icon_hot, R.mipmap.icon_new2, R.mipmap.icon_new3);

或(Kotlin實(shí)現(xiàn))

        tagTextView.setTextTag(R.mipmap.icon_hot, R.mipmap.icon_new2, R.mipmap.icon_new3);
  • Java代碼實(shí)現(xiàn)方式
        TagTextView tagTextView = new TagTextView(this);
        tagTextView.setText("榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)");
        tagTextView.setTextColor(Color.parseColor("#666666"));
        tagTextView.setTagSpace((int) DensityUtil.INSTANCE.dp2px(this,10F));
        tagTextView.setTextTag(R.mipmap.icon_hot, R.mipmap.icon_new2, R.mipmap.icon_new3);
  • Kotlin代碼實(shí)現(xiàn)方式
        val tagTextView = TagTextView(this).apply {
            text = "榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)"
            setTextColor(Color.parseColor("#666666"))
            tagSpace = dp2px(this@MainActivity3, 10f).toInt()
            setTextTag(R.mipmap.icon_hot, R.mipmap.icon_new2, R.mipmap.icon_new3);
        }

自定義標(biāo)簽

  • xml實(shí)現(xiàn)方式
  1. 第一步
    <com.view.text.TagTextView
        android:id="@+id/tagTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)"
        android:textColor="#666666"
        app:tvt_text_space="10dp"
        app:tvt_tag_space="5dp"/>
  1. 第二步(Java實(shí)現(xiàn))
        List<String> data = new ArrayList();
        data.add("");
        data.add("圓角標(biāo)簽");
        data.add("有邊框標(biāo)簽");
        data.add("漸變標(biāo)簽");
        data.add("自定義圓角");

        TagTextView tagTextView = findViewById(R.id.tagTextView);
        tagTextView.setTextTag(new BaseTagAdapter<String>(this,data) {
            @NotNull
            @Override
            public View convert(int i) {
                View view = new View(MainActivity.this);
                if (i == 0){
                    view = new AppCompatImageView(MainActivity.this);
                    ((AppCompatImageView) view).setImageResource(R.mipmap.notice);
                }else if (i == 1){
                    view = LayoutInflater.from(MainActivity.this).inflate(R.layout.adapter_custom_tag,null);
                    view.findViewById(R.id.rootView).setBackgroundResource(R.drawable.radius_fill_bg);
                    AppCompatTextView textView = view.findViewById(R.id.tagText);
                    textView.setText(data.get(i));
                    textView.setTextColor(Color.RED);
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,DensityUtil.INSTANCE.sp2px(MainActivity.this,10F));
                }else if (i == 2){
                    view = LayoutInflater.from(MainActivity.this).inflate(R.layout.adapter_custom_tag,null);
                    AppCompatTextView textView = view.findViewById(R.id.tagText);
                    textView.setText(data.get(i));
                }else if (i == 3){
                    view = LayoutInflater.from(MainActivity.this).inflate(R.layout.adapter_custom_tag,null);
                    view.findViewById(R.id.rootView).setBackgroundResource(R.drawable.gra_bg);
                    AppCompatTextView textView = view.findViewById(R.id.tagText);
                    textView.setText(data.get(i));
                }else if (i == 4){
                    view = LayoutInflater.from(MainActivity.this).inflate(R.layout.adapter_custom_tag,null);
                    view.findViewById(R.id.rootView).setBackgroundResource(R.drawable.custom_radius);
                    AppCompatTextView textView = view.findViewById(R.id.tagText);
                    textView.setText(data.get(i));
                }
                return view;
            }
        });

或(Kotlin實(shí)現(xiàn))

        val data: MutableList<String> = mutableListOf("", "圓角標(biāo)簽", "有邊框標(biāo)簽", "漸變標(biāo)簽", "自定義圓角")
        findViewById<TagTextView>(R.id.tagTextView).setTextTag(
                object : BaseTagAdapter<String>(this@MainActivity3, data) {
                    override fun convert(position: Int): View {
                        var view: View = View(context)
                        when (position) {
                            0 -> {
                                view = AppCompatImageView(context).apply {
                                    setImageResource(R.mipmap.notice)
                                }
                            }
                            1 -> {
                                view = LayoutInflater.from(context).inflate( R.layout.adapter_custom_tag, null )
                                view.findViewById<LinearLayout>(R.id.rootView).setBackgroundResource(R.drawable.radius_fill_bg)
                                view.findViewById<AppCompatTextView>(R.id.tagText).apply {
                                    text = "${data[position]}"
                                    setTextColor(Color.RED)
                                    setTextSize( TypedValue.COMPLEX_UNIT_PX, DensityUtil.sp2px(this@MainActivity3, 10F))
                                }
                            }
                            2 -> {
                                view = LayoutInflater.from(context).inflate( R.layout.adapter_custom_tag, null)
                                view.findViewById<AppCompatTextView>(R.id.tagText).text = "${data[position]}"
                            }
                            3 -> {
                                view = LayoutInflater.from(context).inflate(R.layout.adapter_custom_tag,null)
                                view.findViewById<LinearLayout>(R.id.rootView).setBackgroundResource(R.drawable.gra_bg)
                                view.findViewById<AppCompatTextView>(R.id.tagText).text = "${data[position]}"
                            }
                            4 -> {
                                view = LayoutInflater.from(context).inflate(R.layout.adapter_custom_tag,null)
                                view.findViewById<LinearLayout>(R.id.rootView).setBackgroundResource(R.drawable.custom_radius)
                                view.findViewById<AppCompatTextView>(R.id.tagText).text = "${data[position]}"
                            }
                        }
                        return view
                    }

                }
        )
  • Java代碼實(shí)現(xiàn)方式
        List<String> data = new ArrayList();
        data.add("");
        data.add("圓角標(biāo)簽");
        data.add("有邊框標(biāo)簽");
        data.add("漸變標(biāo)簽");
        data.add("自定義圓角");

        TagTextView tagTextView = new TagTextView(this);
        tagTextView.setTagSpace((int) DensityUtil.INSTANCE.dp2px(this,5F));
        tagTextView.setTextSpace((int) DensityUtil.INSTANCE.dp2px(this,10F));
        tagTextView.setText("榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)");
        tagTextView.setTextColor(Color.parseColor("#666666"));
        tagTextView.setTextTag(new BaseTagAdapter<String>(this,data) {
            @NotNull
            @Override
            public View convert(int i) {
                View view = new View(MainActivity.this);
                if (i == 0){
                    view = new AppCompatImageView(MainActivity.this);
                    ((AppCompatImageView) view).setImageResource(R.mipmap.notice);
                }else if (i == 1){
                    view = LayoutInflater.from(MainActivity.this).inflate(R.layout.adapter_custom_tag,null);
                    view.findViewById(R.id.rootView).setBackgroundResource(R.drawable.radius_fill_bg);
                    AppCompatTextView textView = view.findViewById(R.id.tagText);
                    textView.setText(data.get(i));
                    textView.setTextColor(Color.RED);
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,DensityUtil.INSTANCE.sp2px(MainActivity.this,10F));
                }else if (i == 2){
                    view = LayoutInflater.from(MainActivity.this).inflate(R.layout.adapter_custom_tag,null);
                    AppCompatTextView textView = view.findViewById(R.id.tagText);
                    textView.setText(data.get(i));
                }else if (i == 3){
                    view = LayoutInflater.from(MainActivity.this).inflate(R.layout.adapter_custom_tag,null);
                    view.findViewById(R.id.rootView).setBackgroundResource(R.drawable.gra_bg);
                    AppCompatTextView textView = view.findViewById(R.id.tagText);
                    textView.setText(data.get(i));
                }else if (i == 4){
                    view = LayoutInflater.from(MainActivity.this).inflate(R.layout.adapter_custom_tag,null);
                    view.findViewById(R.id.rootView).setBackgroundResource(R.drawable.custom_radius);
                    AppCompatTextView textView = view.findViewById(R.id.tagText);
                    textView.setText(data.get(i));
                }
                return view;
            }
        });
  • Kotlin代碼實(shí)現(xiàn)方式
        val data: MutableList<String> = mutableListOf("", "圓角標(biāo)簽", "有邊框標(biāo)簽", "漸變標(biāo)簽", "自定義圓角")
        val tagTextView = TagTextView(this).apply {
            tagSpace = DensityUtil.dp2px(this@MainActivity3, 5F).toInt()
            textSpace = DensityUtil.dp2px(this@MainActivity3, 10F).toInt()
            text = "榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)"
            setTextColor(Color.parseColor("#666666"))
            setTextTag(
                    object : BaseTagAdapter<String>(this@MainActivity3, data) {
                        override fun convert(position: Int): View {
                            var view: View = View(context)
                            when (position) {
                                0 -> {
                                    view = AppCompatImageView(context).apply {
                                        setImageResource(R.mipmap.notice)
                                    }
                                }
                                1 -> {
                                    view = LayoutInflater.from(context).inflate(R.layout.adapter_custom_tag, null)
                                    view.findViewById<LinearLayout>(R.id.rootView).setBackgroundResource(R.drawable.radius_fill_bg)
                                    view.findViewById<AppCompatTextView>(R.id.tagText).apply {
                                        text = "${data[position]}"
                                        setTextColor(Color.RED)
                                        setTextSize(TypedValue.COMPLEX_UNIT_PX, DensityUtil.sp2px(this@MainActivity3, 10F))
                                    }
                                }
                                2 -> {
                                    view = LayoutInflater.from(context).inflate(R.layout.adapter_custom_tag, null)
                                    view.findViewById<AppCompatTextView>(R.id.tagText).text = "${data[position]}"
                                }
                                3 -> {
                                    view = LayoutInflater.from(context).inflate(R.layout.adapter_custom_tag, null)
                                    view.findViewById<LinearLayout>(R.id.rootView).setBackgroundResource(R.drawable.gra_bg)
                                    view.findViewById<AppCompatTextView>(R.id.tagText).text = "${data[position]}"
                                }
                                4 -> {
                                    view = LayoutInflater.from(context).inflate(R.layout.adapter_custom_tag, null)
                                    view.findViewById<LinearLayout>(R.id.rootView).setBackgroundResource(R.drawable.custom_radius)
                                    view.findViewById<AppCompatTextView>(R.id.tagText).text = "${data[position]}"
                                }
                            }
                            return view
                        }

                    }
            )
        }

<font color=red>涉及到的示例文件如果沒有,可以滑動(dòng)到底部查看源碼,在demo可以找到</font>

下劃線

  • Java實(shí)現(xiàn)
        AppCompatTextView tagTextView = new AppCompatTextView(this);
        tagTextView.setText("榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)");
        //根據(jù)下標(biāo)設(shè)置
        TextViewExKt.setUnderline(tagTextView,0,10);
        //根據(jù)指定字符串設(shè)置
        TextViewExKt.setUnderline(tagTextView,"5G");
        //支持多個(gè)設(shè)置
        int[] params = new int[2];
        params[0] = 0;
        params[1] = 10;
        TextViewExKt.setUnderline(tagTextView,params);
        //設(shè)置全部
        TextViewExKt.setUnderline(tagTextView);

  • Kotlin實(shí)現(xiàn)
        val tagTextView = AppCompatTextView(this).apply {
            text = "榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)"
        }
        //根據(jù)下標(biāo)設(shè)置
        tagTextView.setUnderline(0,10)
        //根據(jù)指定字符串設(shè)置
        tagTextView.setUnderline("5G")
        //支持多個(gè)設(shè)置
        tagTextView.setUnderline(intArrayOf(0,10))
        //設(shè)置全部
        tagTextView.setUnderline()

刪除線

  • Java實(shí)現(xiàn)
        AppCompatTextView tagTextView = new AppCompatTextView(this);
        tagTextView.setText("榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)");
        //根據(jù)下標(biāo)設(shè)置
        TextViewExKt.setDeleteLine(tagTextView,0,10);
        //根據(jù)指定字符串設(shè)置
        TextViewExKt.setDeleteLine(tagTextView,"5G");
        //支持多個(gè)設(shè)置
        int[] params = new int[2];
        params[0] = 0;
        params[1] = 10;
        TextViewExKt.setDeleteLine(tagTextView,params);
        //設(shè)置全部
        TextViewExKt.setDeleteLine(tagTextView);
  • Kotlin實(shí)現(xiàn)
        val tagTextView = AppCompatTextView(this).apply {
            text = "榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)"
        }
        //根據(jù)下標(biāo)設(shè)置
        tagTextView.setDeleteLine(0,10)
        //根據(jù)指定字符串設(shè)置
        tagTextView.setDeleteLine("5G")
        //支持多個(gè)設(shè)置
        tagTextView.setDeleteLine(intArrayOf(0,10))
        //設(shè)置全部
        tagTextView.setDeleteLine()

格式化字符串

  • Java實(shí)現(xiàn)
        AppCompatTextView tagTextView = new AppCompatTextView(this);
        tagTextView.setText("榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)");
        //指定單個(gè)字符串
        TextViewExKt.setSpecificTextColor(tagTextView, Color.GREEN, "榮耀V40輕奢版", true, index -> {
            if (index == -1){
                Toast.makeText(MainActivity.this,"文本被點(diǎn)擊",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MainActivity.this, "第"+index+"被點(diǎn)擊", Toast.LENGTH_SHORT).show();
            }
        });
        //指定單個(gè)下標(biāo)
        TextViewExKt.setSpecificTextColor(tagTextView, Color.GREEN, 0, 10, true, index -> {
            if (index == -1){
                Toast.makeText(MainActivity.this,"文本被點(diǎn)擊",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MainActivity.this, "第"+index+"被點(diǎn)擊", Toast.LENGTH_SHORT).show();
            }
        });
        //多個(gè)
        List<SpanConfig> params = new ArrayList<>();
        SpanConfig spanConfig1 = new SpanConfig(0, 2);
        params.add(spanConfig1);
        SpanConfig spanConfig2 = new SpanConfig(17, 26, Color.GREEN, true);
        params.add(spanConfig2);
        TextViewExKt.setSpecificTextColor(tagTextView, params, index -> {
            if (index == -1){
                Toast.makeText(MainActivity.this,"文本被點(diǎn)擊",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MainActivity.this, "第"+index+"被點(diǎn)擊", Toast.LENGTH_SHORT).show();
            }
        });
  • Kotlin實(shí)現(xiàn)
        val tagTextView = AppCompatTextView(this).apply {
            text = "榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)"
        }
        //指定單個(gè)字符串
        tagTextView.setSpecificTextColor(Color.GREEN, "榮耀V40輕奢版", true, object : OnTagClickListener {
            override fun onTagClick(tagIndex: Int) {
                if (tagIndex == -1) {
                    Toast.makeText(this@MainActivity3, "文本被點(diǎn)擊", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(this@MainActivity3, "第${tagIndex}被點(diǎn)擊", Toast.LENGTH_SHORT).show()
                }
            }

        })
        //指定單個(gè)下標(biāo)
        tagTextView.setSpecificTextColor(Color.GREEN, 0, 10, true, object : OnTagClickListener {
            override fun onTagClick(tagIndex: Int) {
                if (tagIndex == -1) {
                    Toast.makeText(this@MainActivity3, "文本被點(diǎn)擊", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(this@MainActivity3, "第${tagIndex}被點(diǎn)擊", Toast.LENGTH_SHORT).show()
                }
            }
        })
        //多個(gè)
        tagTextView.setSpecificTextColor(mutableListOf(SpanConfig(0, 2), SpanConfig(17, 26, Color.GREEN, true)), object : OnTagClickListener {
            override fun onTagClick(tagIndex: Int) {
                if (tagIndex == -1) {
                    Toast.makeText(this@MainActivity3, "文本被點(diǎn)擊", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(this@MainActivity3, "第${tagIndex}被點(diǎn)擊", Toast.LENGTH_SHORT).show()
                }
            }

        })

超鏈

  • Java實(shí)現(xiàn)
        AppCompatTextView tagTextView = new AppCompatTextView(this);
        tagTextView.setText("榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)");
        //單個(gè)超鏈實(shí)現(xiàn)
        TextViewExKt.setURLSpan(tagTextView, 0, 10, TEL.INSTANCE, "1234", Color.RED, true);
        //  多個(gè)超鏈實(shí)現(xiàn)
        List<URLSpanConfig> params = new ArrayList<>();
        URLSpanConfig config1 = new URLSpanConfig(0,11,TEL.INSTANCE,"12345",Color.RED,false);
        params.add(config1);
        URLSpanConfig config2 = new URLSpanConfig(12,15,HTTP.INSTANCE,"http://www.baidu.com",Color.GREEN,true);
        params.add(config2);
        TextViewExKt.setURLSpan(tagTextView,params);
  • Kotlin實(shí)現(xiàn)
        val tagTextView = AppCompatTextView(this).apply {
            text = "榮耀V40輕奢版 5G 超級(jí)快充 6400萬超清四攝 8GB+128GB鈦空銀 移動(dòng)聯(lián)通電信5G 雙卡雙待手機(jī)"
        }
        //單個(gè)超鏈實(shí)現(xiàn)
        tagTextView.setURLSpan(0, 10, SpanType.TEL, "1234", Color.RED, true)
        //  多個(gè)超鏈實(shí)現(xiàn)
        tagTextView.setURLSpan(mutableListOf(URLSpanConfig(0,11,SpanType.TEL,"12345"),URLSpanConfig(12,15,SpanType.HTTP,"http://www.baidu.com",Color.GREEN,true)))

如有問題歡迎留言 源碼地址

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

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

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標(biāo)準(zhǔn)。 注意:講述HT...
    kismetajun閱讀 28,815評(píng)論 1 45
  • HTML是一種超文本標(biāo)記語言 一、選擇器 類選擇器:.class id選擇器:#id 通配選擇器:*(選擇所有元素...
    行走在路上的大熊閱讀 534評(píng)論 0 1
  • HTML 5 HTML5概述 因特網(wǎng)上的信息是以網(wǎng)頁的形式展示給用戶的,因此網(wǎng)頁是網(wǎng)絡(luò)信息傳遞的載體。網(wǎng)頁文件是用...
    阿啊阿吖丁閱讀 4,951評(píng)論 0 0
  • 1.1、什么是HTML語義化?有什么好處? 根據(jù)內(nèi)容的結(jié)構(gòu)化(內(nèi)容語義化),選擇合適的標(biāo)簽(代碼語義化)便于開發(fā)者...
    傾城一夢(mèng)1123閱讀 2,580評(píng)論 1 11
  • 我是黑夜里大雨紛飛的人啊 1 “又到一年六月,有人笑有人哭,有人歡樂有人憂愁,有人驚喜有人失落,有的覺得收獲滿滿有...
    陌忘宇閱讀 8,834評(píng)論 28 54

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