简体   繁体   English

Android-以编程方式使StateListDrawable覆盖先前的一个?

[英]Android - Programmatically making a StateListDrawable, overwriting previous one?

I need to make a StateListDrawable programmatically over and over again (when ever the user selects a button). 我需要一遍又一遍地(当用户选择一个按钮时)以编程方式创建一个StateListDrawable。 However, I'm afraid of generating new XML files each time this happens. 但是,我担心每次发生时都会生成新的XML文件。 How can I make sure the previous XML file is getting destroyed and/or replaced? 如何确保先前的XML文件被破坏和/或替换?

My code, as of now: 我的代码,到目前为止:

StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed}, getResources().getDrawable(R.drawable.item_tap_state));
states.addState(new int[]{}, new BitmapDrawable(getResources(), newItem));
item.setImageDrawable(states);

Don't worry, if you create a StateListDreawable object programmatically, it won't generate xml files. 不用担心,如果您以编程方式创建StateListDreawable对象,则不会生成xml文件。 Your code is perfect if you want just adding states to your state list drawable. 如果只想将状态添加到可绘制的状态列表中,则代码是完美的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM