Button动态样式取代xml
还在为 textview以及button 的各种样式而烦恼的童鞋们请往这里看~~~~
一次性解决 textview以及button的样式,再也不用写xml了!!!
全部动态预设置,拒绝堆代码,拒绝xml...
支持的样式如下
private int backColor = 0;//背景色
private int backColorSelected = 0;//按下后的背景色
private int backGroundImage = 0;//背景图
private int backGroundImageSeleted = 0;//按下后的背景图
private int textColor = Color.BLACK;//文字颜色
private int textColorSeleted = 0;//按下后的文字颜色
private float radius = 8;//圆角半径
private int shape = 0;//圆角样式,矩形、圆形等,由于矩形的Id为0,默认为矩形
private Boolean fillet = false;//是否设置圆角
private int strokeWidth = 1;//边框宽度
private int strokeColor = Color.TRANSPARENT;//边框颜色
自定义属性:
<declare-styleable name="GCButton">
<attr name="backColors" format="color"/>
<attr name="backColorSelected" format="color"/>
<attr name="backGroundImage" format="reference"/>
<attr name="backGroundImageSeleted" format="reference"/>
<attr name="textColorSeleted" format="color"/>
<attr name="radius" format="dimension"/>
<attr name="shape">
<enum name="RECTANGLE" value="0"/>
<enum name="OVAL" value="1"/>
<enum name="LINE" value="2"/>
<enum name="RING" value="3"/>
</attr>
<attr name="fillet" format="boolean"/>
<attr name="strokeWidth" format="dimension"/>
<attr name="strokeColor" format="color"/>
</declare-styleable>
完整源码:
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button; @SuppressLint("AppCompatCustomView")
public class GCButton extends Button {
private Context mContext;
private GradientDrawable gradientDrawable;
private int backColor = 0;//背景色
private int backColorSelected = 0;//按下后的背景色
private int backGroundImage = 0;//背景图
private int backGroundImageSeleted = 0;//按下后的背景图
private int textColor = Color.BLACK;//文字颜色
private int textColorSeleted = 0;//按下后的文字颜色
private float radius = 8;//圆角半径
private int shape = 0;//圆角样式,矩形、圆形等,由于矩形的Id为0,默认为矩形
private Boolean fillet = false;//是否设置圆角
private int strokeWidth = 1;//边框宽度
private int strokeColor = Color.TRANSPARENT;//边框颜色 public GCButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.mContext = context;
init();
initAttrs(attrs);
} public GCButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} public GCButton(Context context) {
this(context, null);
} private void initAttrs(AttributeSet attrs) {
TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.GCButton);
setStroke(a.getDimensionPixelSize(R.styleable.GCButton_strokeWidth, strokeWidth), a.getColor(R.styleable
.GCButton_strokeColor, strokeColor));
setBackColor(a.getColor(R.styleable.GCButton_backColors, 0));
setBackColorSelected(a.getColor(R.styleable.GCButton_backColorSelected, 0));
setBackGroundImage(a.getInteger(R.styleable.GCButton_backGroundImage, 0));
setBackGroundImageSeleted(a.getInteger(R.styleable.GCButton_backGroundImageSeleted, 0));
setTextColorSelected(a.getColor(R.styleable.GCButton_textColorSeleted, 0));
setRadius(a.getDimensionPixelSize(R.styleable.GCButton_radius, 0));
setShape(a.getInt(R.styleable.GCButton_shape, 0));
setFillet(a.getBoolean(R.styleable.GCButton_fillet, false));
a.recycle();
TypedArray b = mContext.obtainStyledAttributes(attrs, new int[]{android.R.attr.textColor});
setTextColor(b.getColor(0, textColor));
b.recycle();
invalidateView();
} @Override
public void setOnClickListener(@Nullable final OnClickListener l) {
super.setOnClickListener(new NoDoubleClickListener() {
@Override
protected void onNoDoubleClick(View v) {
l.onClick(v);
}
});
} private void init() {
//将Button的默认背景色改为透明
// if (fillet) {
// if (gradientDrawable == null) {
// gradientDrawable = new GradientDrawable();
// }
// gradientDrawable.setColor(Color.TRANSPARENT);
// } else {
// setBackgroundColor(Color.TRANSPARENT);
// }
//设置文字默认居中
setGravity(Gravity.CENTER);
//设置Touch事件
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent event) {
//按下改变样式
setColor(event.getAction());
//此处设置为false,防止Click事件被屏蔽
return false;
}
});
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { }
});
} //改变样式
@SuppressWarnings("Range")
private void setColor(int state) {
if (state == MotionEvent.ACTION_DOWN) {
//按下
if (backColorSelected != 0) {
//先判断是否设置了按下后的背景色int型
if (fillet) {
if (gradientDrawable == null) {
gradientDrawable = new GradientDrawable();
}
gradientDrawable.setColor(backColorSelected);
} else {
setBackgroundColor(backColorSelected);
}
} else if (backColorSelected != 0) {
if (fillet) {
if (gradientDrawable == null) {
gradientDrawable = new GradientDrawable();
}
gradientDrawable.setColor(backColorSelected);
} else {
setBackgroundColor(backColorSelected);
}
}
//判断是否设置了按下后文字的颜色
if (textColorSeleted != 0) {
setTextColor(textColorSeleted);
} else if (backColorSelected != 0) {
setTextColor(backColorSelected);
}
//判断是否设置了按下后的背景图
if (backGroundImageSeleted != 0) {
setBackgroundResource(backGroundImageSeleted);
}
}
if (state == MotionEvent.ACTION_UP) {
//抬起
if (backColor == 0) {
//如果没有设置背景色,默认改为透明
if (fillet) {
if (gradientDrawable == null) {
gradientDrawable = new GradientDrawable();
}
gradientDrawable.setColor(Color.TRANSPARENT);
} else {
setBackgroundColor(Color.TRANSPARENT);
}
} else if (backColor != 0) {
if (fillet) {
if (gradientDrawable == null) {
gradientDrawable = new GradientDrawable();
}
gradientDrawable.setColor(backColor);
} else {
setBackgroundColor(backColor);
}
} else {
if (fillet) {
if (gradientDrawable == null) {
gradientDrawable = new GradientDrawable();
}
gradientDrawable.setColor(backColor);
} else {
setBackgroundColor(backColor);
}
}
//如果为设置字体颜色,默认为黑色
if (textColor == 0) {
setTextColor(Color.BLACK);
} else if (textColor != 0) {
setTextColor(textColor);
} else {
setTextColor(textColor);
}
if (backGroundImage != 0) {
setBackgroundResource(backGroundImage);
}
}
} /**
* 设置按钮的背景色,如果未设置则默认为透明
*
* @param backColor
*/
public void setBackColor(int backColor) {
this.backColor = backColor;
} /**
* 设置按钮按下后的颜色
*
* @param backColorSelected
*/
public void setBackColorSelected(int backColorSelected) {
this.backColorSelected = backColorSelected;
} /**
* 设置按钮的背景图
*
* @param backGroundImage
*/
public void setBackGroundImage(int backGroundImage) {
this.backGroundImage = backGroundImage;
} /**
* 设置按钮按下的背景图
*
* @param backGroundImageSeleted
*/
public void setBackGroundImageSeleted(int backGroundImageSeleted) {
this.backGroundImageSeleted = backGroundImageSeleted;
} /**
* 设置按钮圆角半径大小
*
* @param radius
*/
public void setRadius(float radius) {
this.radius = radius;
} /**
* 设置按钮文字颜色
*
* @param textColor
*/
public void settextColor(int textColor) {
this.textColor = textColor;
setTextColor(textColor);
} /**
* 设置按钮按下的文字颜色
*
* @param textColor
*/
public void setTextColorSelected(int textColor) {
this.textColorSeleted = textColor;
} /**
* 按钮的形状
*
* @param shape
*/
public void setShape(int shape) {
this.shape = shape;
} /**
* 设置其是否为圆角
*
* @param fillet
*/
@SuppressWarnings("deprecation")
public void setFillet(Boolean fillet) {
this.fillet = fillet;
} public void setStroke(int strokeWidth, int strokeColor) {
this.strokeWidth = strokeWidth;
this.strokeColor = strokeColor;
} public void invalidateView() {
if (gradientDrawable == null) {
gradientDrawable = new GradientDrawable();
}
if (backColor == 0) {
if (fillet) {
if (gradientDrawable == null) {
gradientDrawable = new GradientDrawable();
}
gradientDrawable.setColor(Color.TRANSPARENT);
} else {
setBackgroundColor(Color.TRANSPARENT);
}
} else {
if (fillet) {
if (gradientDrawable == null) {
gradientDrawable = new GradientDrawable();
}
gradientDrawable.setColor(backColor);
} else {
setBackgroundColor(backColor);
}
}
if (backGroundImage != 0) {
setBackgroundResource(backGroundImage);
}
//GradientDrawable.RECTANGLE
if (fillet) {
gradientDrawable.setShape(shape);
gradientDrawable.setCornerRadius(radius);
gradientDrawable.setStroke(strokeWidth, strokeColor);
setBackgroundDrawable(gradientDrawable);
}
} public abstract class NoDoubleClickListener implements OnClickListener { public static final int MIN_CLICK_DELAY_TIME = 1000;
private long lastClickTime = 0; @Override
public void onClick(View v) {
long currentTime = System.currentTimeMillis();
if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
lastClickTime = currentTime;
onNoDoubleClick(v);
}
} protected abstract void onNoDoubleClick(View v);
}
}
需要设置边框颜色,形状等 fillet 属性设置为true
后续会增加阴影等更多效果~~
Button动态样式取代xml的更多相关文章
- WPF 中,动态创建Button,并使Button得样式按照自定义的Resource样式显示
第一步:自定义一个Button的样式 1.新建一个xaml文件,在其中自定义好自己的Resources 这个Resource 的根节点是 <ResourceDictionary xmlns=&q ...
- 利用StateListDrawable给button动态设置背景
项目中,遇到相同样式的Button,只是stroke颜色不一样.为了实现一个,就得写两个shape文件,一个selector文件:多个还得重复写. 解决方法: 结合StateListDrawable给 ...
- 深入理解脚本化CSS系列第五篇——动态样式
前面的话 很多时候,DOM操作比较简单明了,因此用javascript生成那些通常原本是HTML代码生成的内容并不麻烦.但由于浏览器充斥着隐藏的陷阱和不兼容问题,处理DOM中的某些部分时要复杂一些,比 ...
- 动态样式语言Sass&Less介绍与区别
一. Sass/Scss&Less是什么? Sass (Syntactically Awesome Stylesheets)是一种动态样式语言,语法跟css一样(但多了些功能),比css好写, ...
- less 一种 动态 样式 语言
LESS « 一种动态样式语言 http://www.bootcss.com/p/lesscss/ 一种 动态 样式 语言. LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承,运算, 函数 ...
- 前端笔记之React(三)使用动态样式表&antd&React脚手架&props实战
一.使用动态样式表 1.1 LESS使用 全局安装Less npm install -g less 创建1.less文件,然后可以用lessc命令来编译这个文件: lessc 1.less 1.css ...
- DOM动态脚本和动态样式
动态脚本 [定义] 在页面加载时不存在,但将来的某一时刻通过修改DOM动态添加的脚本. [方式] [1]插入外部文件方式 var script = document.createElement(&qu ...
- Less (一种动态样式语言)
Less (一种动态样式语言). LESS是一种由Alexis Sellier设计的动态层叠样式表语言,受Sass所影响,同时也影响了 Sass的新语法:SCSS. LESS是开源的,其第一个版本由R ...
- JSON取代XML?--JSON入门指南
定义 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C# ...
随机推荐
- EntityFramework6.X之DataAnnotations
DataAnnotations 在web开发中不仅在客户端需要执行验证逻辑,会对会对用户向表单中输入的数据给出一个即时反馈:且在服务器端也需验证逻辑,因为来自网络的信息都是不能信任的.在MVC中通常是 ...
- sparklyr包:实现Spark与R的接口
日前,Rstudio公司发布了sparklyr包.该包具有以下几个功能: 实现R与Spark的连接—sparklyr包提供了一个完整的dplyr后端 筛选并聚合Spark数据集,接着在R中实现分析与可 ...
- elasticsearch 不能通过9200端口访问
修改配置文件 config/elasticsearch.yml network.host: 0.0.0.0
- 《HelloGitHub》第 14 期
公告 欢迎通过在 GitHub 上新建 issues 方式推荐项目,我真心希望读者可以在 HelloGItHub,找到真正的编程乐趣! <HelloGitHub>第 14 期 兴趣是最好的 ...
- C语言之变量和数据类型
常量:程序在运行过程中无法对值进行更改. 变量:是在计算机内存空间一种表示,声明变量将会向计算机内存申请存储空间,用于保存数据,计算机的CPU会从内存中加载数据. 声明变量: 数据类型 变量名[=值 ...
- css 中的背景图片小技巧和存在的坑
body 的背景图设置 第一种 :这种情况下背景图片可以缩放 但是不能完全等比缩放 background: url(imgs/1.jpg)no-repeat; background-position: ...
- Unity 打包总结和资源的优化和处理
1. Texture,都去掉alpha通道,作为背景展示的图片,基本都没有透明要求,有特殊要求的则放到atlas里面 a. Loading图这类需要比较精细的,则把图片设置为Automatic Tru ...
- 记住 Python 变量类型的三种方式
title: 记住变量类型的三种方式 date: 2017-06-11 15:25:03 tags: ['Python'] category: ['Python'] toc: true comment ...
- NLTK学习笔记(四):自然语言处理的一些算法研究
自然语言处理中算法设计有两大部分:分而治之 和 转化 思想.一个是将大问题简化为小问题,另一个是将问题抽象化,向向已知转化.前者的例子:归并排序:后者的例子:判断相邻元素是否相同(与排序). 这次总结 ...
- Spring学习(18)--- AOP基本概念及特点
AOP:Aspect Oriented Programing的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序程序功能的统一维护的一种技术 主要的功能是:日志记录,性能统计,安全控制, ...