布局中写法
<ZTButton
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_margin="20dp"
android:text="圆角按钮"
android:textColor="#ffffff"
android:textSize="30sp"
zt_btn:bgColor="@color/colorPrimaryDark"
zt_btn:borderColor="#ff0000"
zt_btn:borderWidth="5dp"
zt_btn:bottom_left_radius="0"
zt_btn:bottom_right_radius="100"
zt_btn:radius="20"
zt_btn:shape="rectangle"
zt_btn:top_left_radius="100"
zt_btn:top_right_radius="0" />
attrs.xml 文件中添加如下代码
<declare-styleable name="ZTButton">
<attr name="bgColor" format="color" /><!--背景颜色-->
<attr name="borderWidth" format="dimension" /><!--边框宽度-->
<attr name="borderColor" format="color" /><!--边框颜色-->
<attr name="radius" format="float" /><!--圆角-->
<attr name="top_left_radius" format="float" /><!--圆角左上-->
<attr name="top_right_radius" format="float" /><!--圆角右上-->
<attr name="bottom_left_radius" format="float" /><!--圆角左下-->
<attr name="bottom_right_radius" format="float" /><!--圆角右下-->
<attr name="shape">
<enum name="rectangle" value="0" /><!--矩形(圆角矩形)-->
<enum name="oval" value="1" /><!--椭圆,圆-->
<enum name="line" value="2" /><!--线-->
<enum name="ring" value="3" /><!--圆环-->
</attr>
</declare-styleable>
控件代码
/**
* 作者:zt
* 时间:on 18/12/26
* 说明: 自定义 按钮, 实现不同角的圆角 ,添加边框
*/
public class ZTButton extends android.support.v7.widget.AppCompatButton {
//形状 图
private GradientDrawable shapeDrawable;
public ZTButton(Context context) {
super(context);
}
public ZTButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ZTButton);
if (a != null) {
//背景颜色
int bgColor = a.getColor(R.styleable.ZTButton_bgColor, 0);
setBgColor(bgColor);
//获取形状
int shape = a.getInteger(R.styleable.ZTButton_shape, 0);
setShape(shape);
//获取圆角
float radius = a.getFloat(R.styleable.ZTButton_radius, 0);
setRadius(radius);
//获取左上圆角
float topLeftRadius = a.getFloat(R.styleable.ZTButton_top_left_radius, 0);
//获取右上圆角
float topRightRadius = a.getFloat(R.styleable.ZTButton_top_right_radius, 0);
//获取左下圆角
float bottomLeftRadius = a.getFloat(R.styleable.ZTButton_bottom_left_radius, 0);
//获取右下圆角
float bottomRightRadius = a.getFloat(R.styleable.ZTButton_bottom_right_radius, 0);
if (topLeftRadius != 0 || bottomLeftRadius != 0 || topRightRadius != 0 || bottomRightRadius != 0) {
setRadius(topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
}
//边框宽度
int borderWidth = (int) a.getDimension(R.styleable.ZTButton_borderWidth, 0);
//边框颜色
int borderColor = a.getColor(R.styleable.ZTButton_borderColor, bgColor);
setBorder(borderWidth, borderColor);
}
}
/**
* 设置边框
*
* @param borderColor 边框颜色
* @param borderWidth 边框宽度
*/
private void setBorder(int borderWidth, @ColorInt int borderColor) {
getGradientDrawable();
shapeDrawable.setStroke(borderWidth, borderColor);
setDrawable(shapeDrawable);
}
/**
* 设置背景颜色
*
* @param bgColor
*/
private void setBgColor(@ColorInt int bgColor) {
getGradientDrawable();
shapeDrawable.setColor(bgColor);
setDrawable(shapeDrawable);
}
/**
* 设置圆角
*
* @param radius 角度
*/
private void setRadius(float radius) {
getGradientDrawable();
shapeDrawable.setCornerRadius(radius);
setDrawable(shapeDrawable);
}
/**
* 设置圆角
*
* @param topLeftRadius 左上
* @param topRightRadius 右上
* @param bottomLeftRadius 左下
* @param bottomRightRadius 右下
*/
private void setRadius(float topLeftRadius, float topRightRadius, float bottomLeftRadius, float bottomRightRadius) {
getGradientDrawable();
shapeDrawable.setCornerRadii(new float[]{
topLeftRadius, topLeftRadius,
topRightRadius, topRightRadius,
bottomRightRadius, bottomRightRadius,
bottomLeftRadius, bottomLeftRadius
});
setDrawable(shapeDrawable);
}
/**
* 设置图形类型
*
* @param shape 形状
*/
private void setShape(int shape) {
getGradientDrawable();
shapeDrawable.setShape(shape);
setDrawable(shapeDrawable);
}
/**
* 设置背景
*
* @param drawable 背景
*/
private void setDrawable(Drawable drawable) {
setBackgroundDrawable(drawable);
}
/**
* 获取需要设置到背景的图片
*/
private void getGradientDrawable() {
if (shapeDrawable == null) {
shapeDrawable = new GradientDrawable();
}
}
}
网友评论