Android 自定义TextView实现描边

前言:

这里呢就是给自己的做的项目一些小功能做一个笔记。也希望能对大家能有帮助。

正文:

这里就简单的自定义了TextView 描边的效果。更多效果,

大家可以参照这个大神的:点击打开链接      以及这位大神:点击打开链接

我这里写的就不像上面两位大神功能那么强大了。

第一步:好现在就来开始我们的自定义的第一步吧!!!

 
public class StrokeTextView extends android.support.v7.widget.AppCompatTextView { public TextView borderText;///用于描边的TextView private int colors; public StrokeTextView(Context context) { this(context,null); } public StrokeTextView(Context context, AttributeSet attrs) { this(context, attrs,0); } public StrokeTextView(Context context, AttributeSet attrs,int defStyle) { super(context, attrs, defStyle); borderText = new TextView(context,attrs); init(attrs); } public void init(AttributeSet attrs){ TypedArray ta = getContext().obtainStyledAttributes(attrs,R.styleable.StrokeTextView); colors = ta.getColor(R.styleable.StrokeTextView_stroke_color,0xFFFFFF); ta.recycle(); TextPaint tp1 = borderText.getPaint(); //new tp1.setStrokeWidth(4); //设置描边宽度 tp1.setStyle(Style.STROKE); //对文字只描边 borderText.setTextColor(colors); //设置描边颜色 borderText.setGravity(getGravity()); } @Override public void setLayoutParams (ViewGroup.LayoutParams params){ super.setLayoutParams(params); borderText.setLayoutParams(params); } /** * onMeasure通过父View传递过来的大小和模式, * 以及自身的背景图片的大小得出自身最终的大小, * 然后通过setMeasuredDimension()方法设置给mMeasuredWidth和mMeasuredHeight. * * @param widthMeasureSpec * @param heightMeasureSpec */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { CharSequence tt = borderText.getText(); //两个TextView上的文字必须一致 if(tt== null || !tt.equals(this.getText())){ borderText.setText(getText()); this.postInvalidate(); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); borderText.measure(widthMeasureSpec, heightMeasureSpec); } /** * 该方法是View的放置方法,在View类实现。 * 调用该方法需要传入放置View的矩形空间左上角left、top值和右下角right、bottom值。 * 这四个值是相对于父控件而言的。 * * @param changed * @param left * @param top * @param right * @param bottom */ protected void onLayout (boolean changed, int left, int top, int right, int bottom){ super.onLayout(changed, left, top, right, bottom); borderText.layout(left, top, right, bottom); } @Override protected void onDraw(Canvas canvas) { borderText.draw(canvas); super.onDraw(canvas); } } 

 

 
 

第二步:在Values资源文件中去定义自定义TextView 在xml布局中使用的属性

第三步:在xml布局中去使用自定义控件

注意:需要使用app:...自定义属性需要在根布局加上这句

 xmlns:app="http://schemas.android.com/apk/res-auto"

第四步:为了方便使用我在自定义文件中(StrokeTextView.java)

里面的 borderText 定义为了 public .这样就省了我再去定义一个方法

            在代码中使用也是很方便的,使用Butterknife(黄油刀)去到到控件。
			当然你也可以去findViewbyId去找到控件

  接下来就直接使用就好了

找到控件后

这样使用就可以啦

就这样就好啦。好了到这里简单的自定义TextView描边就结束了

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Android自定义TextView中显示全部内容,可以使用以下两种方法: 1. 使用setEllipsize()方法 通过设置setEllipsize()方法,可以在TextView的末尾添加省略号,从而指示文本被截断。你可以使用以下代码来实现: ``` yourTextView.setEllipsize(TextUtils.TruncateAt.END); yourTextView.setSingleLine(true); ``` 上述代码将设置TextView只显示一行并在末尾添加省略号。 2. 自定义TextView 你可以从TextView类继承一个新类,并覆盖onMeasure()方法以测量控件的高度和宽度。 你可以使用以下代码实现: ``` public class CustomTextView extends TextView { public CustomTextView(Context context) { super(context); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //获取TextView的内容 CharSequence text = getText(); if (text != null) { //测量TextView的高度 int width = getMeasuredWidth(); int height = getMeasuredHeight(); int lineCount = getLineCount(); int lineHeight = getLineHeight(); int totalHeight = lineCount * lineHeight; if (totalHeight > height) { setMeasuredDimension(width, totalHeight); } } } } ``` 上述代码将测量TextView的高度,如果文本的高度超出了TextView的高度,则调整TextView的高度以适应文本。然后你可以使用此自定义TextView来显示你的文本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WL-鬼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值