1.自定义属性
新建attrs.xml文件(res->values->attrs.xml),定义要自定义的TextVIEw属性
<?xml version="1.0" enCoding="utf-8"?><resources> <declare-styleable name="MyTextVIEw"> <!--name 属性名称 format 格式--> <attr name="myText" format="string" /> <attr name="myTextcolor" format="color" /> <attr name="myTextSize" format="dimension" /> </declare-styleable></resources>
2.在布局中使用,对比系统TextVIEw
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" tools:context=".MainActivity"> <com.future.Coding.custom_vIEw.MyTextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:padding="10dp" app:myText="American PresIDential Election" app:myTextcolor="#D81B60" app:myTextSize="16sp" /> <!--与上面自定义TextVIEw的对比--> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:padding="10dp" androID:text="American PresIDential Election" androID:textcolor="#D81B60" androID:textSize="16sp" /></linearLayout>
3.通过继承VIEw,实现自定义TextVIEw
public class MyTextVIEw extends VIEw { private String mText; private int mTextSize = 16; private int mTextcolor = color.BLACK; private Paint mPaint; private static final String TAG = "MyTextVIEw"; //在代码中使用 public MyTextVIEw(Context context) { this(context, null); } //在布局layout中使用 public MyTextVIEw(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } //在布局layout中使用,但会有style public MyTextVIEw(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); //获取自定义属性 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextVIEw); mText = typedArray.getString(R.styleable.MyTextVIEw_myText); mTextcolor = typedArray.getcolor(R.styleable.MyTextVIEw_myTextcolor, mTextcolor); Log.d(TAG, "MyTextVIEw: " + sp2px(mTextSize)); mTextSize = typedArray.getDimensionPixelSize(R.styleable.MyTextVIEw_myTextSize, sp2px(mTextSize)); /** * 解析:TypedArray 为什么需要调用recycle() * https://blog.csdn.net/Monicabg/article/details/45014327 */ typedArray.recycle(); mPaint = new Paint(); mPaint.setAntiAlias(true);//抗锯齿 mPaint.setTextSize(mTextSize); mPaint.setcolor(mTextcolor); } private int sp2px(int sp) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getResources().getdisplayMetrics()); } /** * 自定义view的测量方法 * * @param wIDthMeasureSpec * @param heightmeasureSpec */ @OverrIDe protected voID onMeasure(int wIDthMeasureSpec, int heightmeasureSpec) { super.onMeasure(wIDthMeasureSpec, heightmeasureSpec); //指定控件的宽高,需要测量 int wIDthMode = MeasureSpec.getMode(wIDthMeasureSpec); int heightmode = MeasureSpec.getMode(heightmeasureSpec); int wIDthSize = MeasureSpec.getSize(wIDthMeasureSpec); int heightSize = MeasureSpec.getSize(heightmeasureSpec); if (wIDthMode == MeasureSpec.AT_MOST) {//在布局中指定了wrap_content Rect bounds = new Rect(); mPaint.getTextBounds(mText, 0, mText.length(), bounds); wIDthSize = bounds.wIDth() + getpaddingleft() + getpaddingRight(); } else if (wIDthMode == MeasureSpec.EXACTLY) {//在布局中指定了确定的值 比如:100dp / match_parent } else if (wIDthMode == MeasureSpec.UnspecIFIED) {//尽可能的大,很少能用到 //ListVIEw、ScrollVIEw在测量子布局的时候会用 } if (heightmode == MeasureSpec.AT_MOST) {//在布局中指定了wrap_content Rect bounds = new Rect(); mPaint.getTextBounds(mText, 0, mText.length(), bounds); heightSize = bounds.height() + getpaddingtop() + getpaddingBottom(); } else if (heightmode == MeasureSpec.EXACTLY) {//在布局中指定了确定的值 比如:100dp / match_parent } else if (heightmode == MeasureSpec.UnspecIFIED) {//尽可能的大,很少能用到 //ListVIEw、ScrollVIEw在测量子布局的时候会用 } setMeasuredDimension(wIDthSize, heightSize); } @OverrIDe protected voID onDraw(Canvas canvas) { super.onDraw(canvas); /** * 自定义view之绘图篇(四):baseline和FontMetrics * https://blog.csdn.net/u012551350/article/details/51361778 */ Paint.FontMetrics FontMetrics = mPaint.getFontMetrics(); //中心线到基线的距离 int dy = (int) ((FontMetrics.bottom - FontMetrics.top) / 2 - FontMetrics.bottom); //得到基线(Baseline) int baseline = getHeight() / 2 + dy; int x = getpaddingleft(); canvas.drawText(mText, x, baseline, mPaint); }}
--END
总结
以上是内存溢出为你收集整理的Android自定义TextView全部内容,希望文章能够帮你解决Android自定义TextView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)