コードからボタンの状態を変更する方法。サーバーから画像を取得しているため、xml ファイルを使用できません。
使用:
Drawable d = new BitmapDrawable(getResources(),bitmap);
ドローアブルを取得しましたが、さまざまな状態を割り当てて、ボタンに対応する画像を添付するにはどうすればよいですか?
コードからボタンの状態を変更する方法。サーバーから画像を取得しているため、xml ファイルを使用できません。
使用:
Drawable d = new BitmapDrawable(getResources(),bitmap);
ドローアブルを取得しましたが、さまざまな状態を割り当てて、ボタンに対応する画像を添付するにはどうすればよいですか?
xml では、<selector>
要素を使用してこれを行います。コードでは、StateListDrawableを使用します。
StateListDrawable content = new StateListDrawable();
Drawable pressedDrawable = new BitmapDrawable(getResources(),bitmap);
//Your other drawables (focused, .....)
content.addState(new int[] { android.R.attr.state_pressed }, pressedDrawable);
//Add the other drawables the same way
Button button = (Button) view.findViewById(R.id.my_button);
button.setBackground(content);