Android 自定义下划线(打折效果)
2020-09-30 本文已影响0人
jimdear
效果图如下:
![](https://img.haomeiwen.com/i1447307/9232afb3bce59584.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();
}
}