美文网首页程序员
Android 自定义下划线(打折效果)

Android 自定义下划线(打折效果)

作者: jimdear | 来源:发表于2020-09-30 16:59 被阅读0次

    效果图如下:


    image.png

    最近练习kotlin,随手写了一下:以下是代码,仅供参考.规则是 &- 你需要下划线的文案-& 就是画横线效果啦.

    package com.jimdear.kotlindemo.view
    
    import android.content.Context
    import android.graphics.Paint
    import android.util.AttributeSet
    import android.widget.LinearLayout
    import android.widget.TextView
    import com.jimdear.kotlindemo.R
    
    /**
    * Created by jimdear on 2020/9/30
    * Comment: kotlin 自定义组件
    */
    class CommonTextView : LinearLayout {
     private var str: String? = null;
     constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
     constructor(context: Context, attrs: AttributeSet?, style: Int) : super(context, attrs, style)
     constructor(context: Context,str:String) : super(context) {
         initView(str);
     }
     fun initView(str: String) {
         this.str = str;
         removeAllViews();
         if (!str.equals("")) {
             var strs = str.split("&");
             for (element in strs) {
                 val textView = TextView(context)
                 textView.textSize = 14f
                 textView.setTextColor(resources.getColor(R.color.color_999999))
                 if (element.startsWith("-") && element.endsWith("-")) {
                     textView.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                     if (element.contains("-")) {
                         textView.setText(element.replace("-", ""))
                     }
                 }else{
                     textView.setText(element);
                 }
                 addView(textView);
             }
         }
         postInvalidate();
     }
    
    }
    

    相关文章

      网友评论

        本文标题:Android 自定义下划线(打折效果)

        本文链接:https://www.haomeiwen.com/subject/xaxpuktx.html