1、编写界面配置的.xml属性
<com.test.testgame.view.custom.StrokeTextView
android:id="@+id/btn_action"
android:layout_width="122px"
android:layout_height="64px"
android:layout_gravity="center_vertical"
android:layout_marginRight="5px"
android:background="@drawable/btn_diamondtasks"
android:gravity="center"
android:text="去完成"
android:textColor="@android:color/white"
android:textSize="22px"
android:textStyle="bold"
app:strokeColor="@color/color_diamondtasks"
app:strokeWidth="2dp" />
其中最后两个属性是自定义的:
app:strokeColor="@color/color_diamondtasks"
app:strokeWidth="2dp"
如何自定义属性?
在styles.xml下添加如下代码:
<declare-styleable name="StrokeTextView">
<attr name="strokeWidth" format="dimension" />
<attr name="strokeColor" format="reference|color" />
</declare-styleable>
代码中format 中添的是参数,就是在以下代码中可以填写的数值:
2、还要在界面配置的.xml文件的根Layout下添加具体自定义控件的路径
xmlns:app="http://schemas.android.com/apk/res/com.test.testgame"
这相当于查找路径或命名空间
3、编写对应的解析类
该解析类的路径需跟.xml中配置的相同即同上面的:com.test.testgame.view.custom.StrokeTextView 路径相同。
所以我们在我们工程下的view/custom下建了个StrokTextView类,用来构造和初始化我们的按钮
package com.test.testgame.view.custom;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.TextView;
import com.test.testgame.R;
public class StrokeTextView extends TextView {
// fields
private int mStrokeColor;
private float mStrokeWidth;
private TextPaint mStrokePaint;
// constructors 构造方法 供.xml文件渲染时内部调用(系统自己调用)
public StrokeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StrokeTextView, defStyle, 0);
mStrokeWidth = a.getDimension(R.styleable.StrokeTextView_strokeWidth, 0);
mStrokeColor = a.getColor(R.styleable.StrokeTextView_strokeColor, 0xFF000000);
a.recycle();
}
public StrokeTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public StrokeTextView(Context context) {
super(context);
}
// ===================getters + setters(自定义的一些属性,在代码中可以设置)==========================
public int getStrokeColor()
{
return mStrokeColor;
}
public int getWidth()
{
return mStrokeWidth;
}
public void setStrokeColor(int color) {
mStrokeColor = color;
}
public void setStrokeWidth(int width) {
mStrokeWidth = width;
}
// ===================getters + setters(自定义的一些属性,在代码中可以设置)==========================
// overridden methods(重载父类的一些方法)
@Override
protected boolean onSetAlpha(int alpha) {
return false;
}
@Override
protected void onDraw(Canvas canvas) {
if (mStrokeWidth != 0) {
// lazy load
if (mStrokePaint == null) {
mStrokePaint = new TextPaint();
}
// copy
TextPaint paint = getPaint();
mStrokePaint.setTextSize(paint.getTextSize());
mStrokePaint.setTypeface(paint.getTypeface());
mStrokePaint.setFlags(paint.getFlags());
mStrokePaint.setAlpha(paint.getAlpha());
// custom
mStrokePaint.setStyle(Paint.Style.STROKE);
mStrokePaint.setColor(mStrokeColor);
mStrokePaint.setStrokeWidth(mStrokeWidth);
String text = getText().toString();
float x = (getWidth() - mStrokePaint.measureText(text)) / 2;
if (((int) x) % 2 != 0) {
x -= 1;
}
canvas.drawText(text, x, getBaseline(), mStrokePaint);
}
super.onDraw(canvas);
}
@Override
public boolean isInEditMode() {
return true;
}
}
4、按钮的效果
按钮的颜色可以在.xml属性中设置。