动态生成按钮样式

使用:

    int borderColor = Color.parseColor("#2E3135");    int bgColor = Color.parseColor("#00FF00");    // 设置View背景样式,有边框宽度、边框颜色、圆角度数、背景颜色。    GradientDrawable shape = DrawableUtils.createShape(1, 4, borderColor, bgColor);    btn1.setBackground(shape);    int textNormalColor = Color.parseColor("#2E3135");    int textPressColor = Color.parseColor("#FF0000");    int textFocusColor = Color.parseColor("#00FF00");    int textEnableColor = Color.parseColor("#0000FF");    // 设置文本点击样式切换    ColorStateList colorStateList = DrawableUtils.createColorStateList(textNormalColor, textPressColor, textFocusColor, textEnableColor);    btn2.setTextColor(colorStateList);    /* android:focusableInTouchMode="true" 控件默认获取焦点 */    int color1 = Color.parseColor("#FF0000");    int color2 = Color.parseColor("#00FF00");    // 设置View点击样式切换    GradientDrawable unSelected = DrawableUtils.createShape(1, 4, color1, color2);    GradientDrawable selected = DrawableUtils.createShape(1, 4, color2, color1);    StateListDrawable stateListDrawable = DrawableUtils.createStateListDrawable(unSelected, selected);    btn3.setBackground(stateListDrawable);

工具类:

public class DrawableUtils {    /**     * 创建一个shape     *     * @param strokeWidth 边框宽度(px)     * @param roundRadius 圆角半径(px)     * @param strokeColor 边框颜色     * @param fillColor   内部填充颜色     * @return GradientDrawable     */    public static GradientDrawable createShape(int strokeWidth, int roundRadius, int strokeColor, int fillColor) {        GradientDrawable gd = new GradientDrawable();        gd.setColor(fillColor);        gd.setCornerRadius(roundRadius);        gd.setStroke(strokeWidth, strokeColor);        return gd;    }    /**     * 创建按钮文字点击样式     *     * @param normal  正常样式     * @param pressed 按下样式     * @param focused 焦点样式     * @param unable  不可用样式     * @return ColorStateList     */    public static ColorStateList createColorStateList(int normal, int pressed, int focused, int unable) {        int[] colors = new int[]{pressed, focused, normal, focused, unable, normal};        int[][] states = new int[6][];        states[0] = new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled};        states[1] = new int[]{android.R.attr.state_enabled, android.R.attr.state_focused};        states[2] = new int[]{android.R.attr.state_enabled};        states[3] = new int[]{android.R.attr.state_focused};        states[4] = new int[]{android.R.attr.state_window_focused};        states[5] = new int[]{};        return new ColorStateList(states, colors);    }    /**     * 创建按钮点击样式     *     * @param unSelected 未点击样式     * @param selected 点击样式     * @return StateListDrawable     */    public static StateListDrawable createStateListDrawable(GradientDrawable unSelected, GradientDrawable selected) {        StateListDrawable drawable = new StateListDrawable();        drawable.addState(new int[]{android.R.attr.state_pressed}, selected);        drawable.addState(new int[]{-android.R.attr.state_pressed}, unSelected);        return drawable;    }}

更多相关文章

  1. 关于Android(安卓)ListView组件中android:drawSelectorOnTop含义
  2. android GridView 去掉自带点击边框效果和禁止上下滑动
  3. Android常用动画alpha和rotate同时使用
  4. Android中自定义水平的ProgressBar
  5. 【转】android shape指定那几个边有边框
  6. 给按钮设置点击变色
  7. Android--ListView滚动条样式
  8. 在低版本Android上使用Material Design——AppCompat v21
  9. SJ64 拿起工具(Android(安卓)Studio)开始造轮子(贺卡App)

随机推荐

  1. 基于mysql中delete的语法别名问题
  2. MySQL 用 limit 为什么会影响性能
  3. 一次MySQL启动导致的事故实战记录
  4. MySQL中几种插入和批量语句实例详解
  5. MySQL 如何限制一张表的记录数
  6. MySQL into_Mysql中replace与replace int
  7. MYSQL 的10大经典优化案例场景实战
  8. MySQL 数据库定时备份的几种方式(全面)
  9. Mysql 字符集不一致导致连表异常的解决
  10. 五分钟带你搞懂MySQL索引下推