Android動態產生按鈕樣式

來源:互聯網
上載者:User

標籤:Android   GradientDrawable   ColorStateList   動態設定樣式   

動態產生按鈕樣式使用:
    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;    }}

Android動態產生按鈕樣式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.