문제

I am creating a dynamically Imageview.

ImageView btnSend = new ImageView (this);

I need to put a layout that contains the states of the images, this is the xml:

<? xml version = "1.0" encoding = "utf-8"?>
<selector
      xmlns: android = "http://schemas.android.com/apk/res/android">
      <item
           android: state_focused = "true"
           android: state_pressed = "false"
           android: drawable = "@ drawable/button_state3" />
      <item
           android: state_focused = "true"
           android: state_pressed = "true"
           android: drawable = "@ drawable/button_state2" />
      <item
           android: state_focused = "false"
           android: state_pressed = "true"
           android: drawable = "@ drawable/button_state2" />
      <item
           android: drawable = "@ drawable/button_state1" />
</ selector>

I tested with setBackgroundResource property, but does not work.

how do I assign this layout to Imageview?

Thanks in advance.

도움이 되었습니까?

해결책

What you have created in this XML (BTW remove the spaces after @) is a StateListDrawable not a Layout as you mentioned. Consequently, use ImageView.setImageResource(R.drawable.mydrawable) to set it.

다른 팁

This is the code that works...

ImageView btnEliminar = new ImageView (this);

StateListDrawable drawable = new StateListDrawable();

Drawable normal = getResources().getDrawable(R.drawable.image_normal);
Drawable selected = getResources().getDrawable(R.drawable.image_selected);
Drawable pressed = getResources().getDrawable(R.drawable.image_pressed);

drawable.addState(new int[] { android.R.attr.state_pressed}, pressed);
drawable.addState(new int[] { android.R.attr.state_focused}, selected);
drawable.addState(new int[] { android.R.attr.state_enabled}, normal);

btnEliminar.setImageDrawable(drawable);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top