ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android StateListDrawable按下状态总是显示上次添加

2019-06-12 09:18:10  阅读:190  来源: 互联网

标签:android stroke statelistdrawable layerdrawable


我有一个自定义按钮,并通过编程方式更改其按下和默认颜色.

public class CustomApplicationButton extends Button {

public CustomApplicationButton(Context context) {
    this(context, 0, 0, 0);
}

public CustomApplicationButton(Context context, int topDrawableResId, int outlineDefaultColorId, int outlinePressedColorId) {
    super(context);

    // set width and height
    LinearLayout.LayoutParams params = new LayoutParams(
            context.getResources().getDimensionPixelSize(R.dimen.sr_application_button_width), 
            context.getResources().getDimensionPixelSize(R.dimen.sr_application_button_height));
    setLayoutParams(params);

    // set drawable top icon
    if (topDrawableResId != 0) {
        setCompoundDrawablesWithIntrinsicBounds(0, topDrawableResId, 0, 0);
    }

    // set background and outline color

    int strokeWidth = context.getResources().getDimensionPixelSize(R.dimen.sr_launcher_button_stroke_size);

    // unpressed state drawable
    LayerDrawable defaultLayers = (LayerDrawable) context.getResources().getDrawable(
            R.drawable.btn_launcher_shape_default);
    GradientDrawable defaultShapeOutline = (GradientDrawable) defaultLayers.findDrawableByLayerId(R.id.outline_default);
    defaultShapeOutline.setStroke(strokeWidth, context.getResources().getColor(outlineDefaultColorId));

    // pressed state drawable
    LayerDrawable pressedLayers = (LayerDrawable) context.getResources().getDrawable(
            R.drawable.btn_launcher_shape_pressed);
    GradientDrawable pressedShapeOutline = (GradientDrawable) pressedLayers.findDrawableByLayerId(R.id.outline_pressed);
    pressedShapeOutline.setStroke(strokeWidth, context.getResources().getColor(outlinePressedColorId));

    // set states
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] {android.R.attr.state_pressed}, pressedLayers);
    states.addState(new int[] { }, defaultLayers);

    // set background
    this.setBackground(states);
}

}

然后,将此按钮添加到我的活动中,里面有一个Linearlayout.

public class MainActivity extends Activity {

private LinearLayout applicationPanel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    applicationPanel = (LinearLayout) findViewById(R.id.application_panel);
    ssPanel = (LinearLayout) findViewById(R.id.ss_panel);

    CustomApplicationButton btnLauncherApp = new CustomApplicationButton(this, R.drawable.ic_application, 
            R.color.application_green_default_color, R.color.application_green_pressed_color);
    btnLauncherApp.setText("Button 1");
    applicationPanel.addView(btnLauncherApp);

    btnLauncherApp = new CustomApplicationButton(this, R.drawable.ic_camera, 
            R.color.camera_blue_default_color, R.color.camera_blue_pressed_color);
    btnLauncherApp.setText("Button 2");
    applicationPanel.addView(btnLauncherApp);

    btnLauncherApp = new CustomApplicationButton(this, R.drawable.ic_browser, 
            R.color.browser_gray_default_color, R.color.browser_gray_pressed_color);
    btnLauncherApp.setText("Button 3");
    applicationPanel.addView(btnLauncherApp);

}

}

我的问题是所有3个按钮都有不同的默认笔触颜色,但按下的笔划颜色总是最后添加按钮颜色.

总结一下;

两张图片千言万语:)

解决方法:

正如文档中明确提到的:

"Note: changing this property will affect all instances of a drawable loaded from a resource. It is recommended to invoke mutate() before changing this property."

因此,如果您在每个setStroke()之前调用mutate(),您的问题将得到解决.

defaultShapeOutline.mutate();
defaultShapeOutline.setStroke(strokeWidth, context.getResources().getColor(outlineDefaultColorId));

标签:android,stroke,statelistdrawable,layerdrawable
来源: https://codeday.me/bug/20190612/1224588.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有