Android的自定义按钮

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属性中设置。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值