还在为 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的更多相关文章

  1. WPF 中,动态创建Button,并使Button得样式按照自定义的Resource样式显示

    第一步:自定义一个Button的样式 1.新建一个xaml文件,在其中自定义好自己的Resources 这个Resource 的根节点是 <ResourceDictionary xmlns=&q ...

  2. 利用StateListDrawable给button动态设置背景

    项目中,遇到相同样式的Button,只是stroke颜色不一样.为了实现一个,就得写两个shape文件,一个selector文件:多个还得重复写. 解决方法: 结合StateListDrawable给 ...

  3. 深入理解脚本化CSS系列第五篇——动态样式

    前面的话 很多时候,DOM操作比较简单明了,因此用javascript生成那些通常原本是HTML代码生成的内容并不麻烦.但由于浏览器充斥着隐藏的陷阱和不兼容问题,处理DOM中的某些部分时要复杂一些,比 ...

  4. 动态样式语言Sass&Less介绍与区别

    一. Sass/Scss&Less是什么? Sass (Syntactically Awesome Stylesheets)是一种动态样式语言,语法跟css一样(但多了些功能),比css好写, ...

  5. less 一种 动态 样式 语言

    LESS « 一种动态样式语言 http://www.bootcss.com/p/lesscss/ 一种 动态 样式 语言. LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承,运算, 函数 ...

  6. 前端笔记之React(三)使用动态样式表&antd&React脚手架&props实战

    一.使用动态样式表 1.1 LESS使用 全局安装Less npm install -g less 创建1.less文件,然后可以用lessc命令来编译这个文件: lessc 1.less 1.css ...

  7. DOM动态脚本和动态样式

    动态脚本 [定义] 在页面加载时不存在,但将来的某一时刻通过修改DOM动态添加的脚本. [方式] [1]插入外部文件方式 var script = document.createElement(&qu ...

  8. Less (一种动态样式语言)

    Less (一种动态样式语言). LESS是一种由Alexis Sellier设计的动态层叠样式表语言,受Sass所影响,同时也影响了 Sass的新语法:SCSS. LESS是开源的,其第一个版本由R ...

  9. JSON取代XML?--JSON入门指南

    定义 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C# ...

随机推荐

  1. 读书笔记系列01-《收获、不止Oracle》

    读书笔记系列01-<收获.不止Oracle> 最近计划将看过的Oracle书籍依次系统的总结下读书笔记. 这本书是我个人觉得写的最有趣的Oracle书籍,也是我接触Oracle后第一本完全 ...

  2. Dockerfile 最佳实践

    之前 一篇文章介绍 docker 的镜像基本原理和概念 ,主要介绍在编写 docker 镜像的时候一些需要注意的事项和推荐的做法. 虽然 Dockerfile 简化了镜像构建的过程,并且把这个过程可以 ...

  3. 常用统计分析 SQL 在 AWK 中的实现(转)

    转自:http://my.oschina.net/leejun2005/blog/100710 最近有需求需要本地处理一些临时的数据,用做统计分析.如果单纯的 MYSQL 也能实现, 不过一堆临时数据 ...

  4. js实现分页

    <html> <head> <meta charset='utf-8'> <script type="text/javascript" s ...

  5. scrapy跟pyspider的杂谈

    最近有一个私人项目要搞,可能最近的博客都会变成爬虫跟数据分析类的了.既然是爬虫,第一反应想到的就是鼎鼎大名的scrapy了,其次想到的pyspider,最后想到的就是自己写. scrapy是封装了tw ...

  6. Java IO流之打印流与标准流

    一.打印流 1.1打印流特点与构造方法 1)PrintStream和PrintWriter类都提供了一系列重载的print和println方法来输出各种类型的数据. 2)PrintStream和Pri ...

  7. canvas——随机生成迷宫

    先上图. 效果 代码 随机生成迷宫要求任意两点都能够找到相同的路径,也就是说,迷宫是一个连通图.随机生成迷宫可以使用普里姆算法.广度优先算法.深度优先算法等实现.这里将使用普里姆算法通过生成最小数的方 ...

  8. Vulkan Tutorial 11 Shader modules

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 与之前的图像API不同,Vulkan中的着色器代码必须以二进制字节码的格式使用,而不 ...

  9. 在firefox的flashgot中配置各种下载器

    一.在firefox中安装flashgot下载管理器 flashgot是firefox的一个扩展,在联网的情况下,可以在firefox中的附加组件中搜索flashgot,然后安装. 二.在flashg ...

  10. Windows定时关机

    用shutdown命令.开始菜单>运行,输入shutdown -s -t 7200 (两个小时之后关机)at 12:00 shutdown -s (12:00关机) 其他设置:shutdown ...