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

Android StateListDrawable按下状态总是显示上次添加,第1张

概述我有一个自定义按钮,并通过编程方式更改其按下和默认颜色. public class CustomApplicationButton extends Button {public CustomApplicationButton(Context context) { this(context, 0, 0, 0);}public CustomApplicationButton(Cont 我有一个自定义按钮,并通过编程方式更改其按下和默认颜色.

public class CustomApplicationbutton extends button {public CustomApplicationbutton(Context context) {    this(context,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);    }    // 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;@OverrIDeprotected 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 StateListDrawable按下状态总是显示上次添加全部内容,希望文章能够帮你解决Android StateListDrawable按下状态总是显示上次添加所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1130070.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存