以编程方式将选择器 xml 资源设置为可绘制到 ImageView 不起作用
Programmatically setting selector xml resourse as drawable to ImageView is not working
我需要用图标及其标题填充 ListView
。我在资源中为菜单图标及其标题创建了数组,如:
对于标题:
<array name="menu_title_array">
<item>@string/menu_name_text</item>
</array>
对于图标:
<array name="menu_icons_array">
<item>@drawable/menu_icon_name</item>
</array>
我使用 xml select 或像这样的可绘制对象为每个图标创建了一个可绘制对象:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/icon_name_disabled" android:state_enabled="false"/>
<item android:drawable="@drawable/icon_name_press" android:state_pressed="true"/>
<item android:drawable="@drawable/icon_name_press" android:state_activated="true"/>
<item android:drawable="@drawable/icon_name"/>
</selector>
现在我正在创建一个包含 title
和 drawable
的 MenuItems 列表,来自我创建的 menu_title_array
和 menu_icons_array
数组:
TypedArray titles = getResources().obtainTypedArray(R.array.menu_title_array);
TypedArray icons = getResources().obtainTypedArray(R.array.menu_icons_array);
for (int index = 0; index < menuRowText.length(); mainMenuIndex++) {
menuRowItemList.add(new MenuRowItem(icons.getDrawable(index), titles.getString(index ));
}
现在我在适配器 getView()
中设置标题及其图标,方法如下:
title.setText(menuItem.getTitle());
icon.setImageDrawable(menuItem.getDrawable());
现在,如果我 select 一个菜单项,那么图标的状态应该根据我为它创建的 <selector>
而改变。 但它并没有改变它的状态。
如果我为每个菜单项创建一个 StateListDrawable
,例如:
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
mContext.getResources().getDrawable(R.drawable.icon_name_press));
states.addState(new int[] {android.R.attr.state_activated},
mContext.getResources().getDrawable(R.drawable.icon_name_press));
states.addState(new int[] { },
mContext.getResources().getDrawable(R.drawable.icon_name));
并将其应用于列表图标,如:
icon.setImageDrawable(states);
然后它正常工作,因为它正在改变它的状态激活,按下。
请指出我哪里出错了,我不想为每个 MenuItem 使用 StateListDrawable
创建可绘制图标,因为应用程序中会有很多样板代码。
ListView 正在使用 List<MenuRowItem>
进行填充。这是以下 pojo 的结构:
class MenuRowItem {
private Drawable drawable;
private String title;
MenuRowItem(Drawable drawable, String title) {
this.drawable = drawable;
this.title = title;
}
public Drawable getDrawable() {
return drawable;
}
public String getText() {
return mText;
}
如果需要更多详细信息,请告诉我。
通过将 drawable
的 id
保留在 MenuRowItem
中而不是保留 Drawable
对象解决了问题。
现在是 MenuRowItem
的 POJO:
class MenuRowItem {
private int drawableResourceId;
private String title;
MenuRowItem(int resourceId, String title) {
this.drawableResourceId = resourceId;
this.title = title;
}
public int getResourceId() {
return drawableResourceId;
}
public String getText() {
return mText;
}
}
我相信 Drawable(enable, activated, normal)
丢失状态,同时将可绘制对象保留为对象。
因此,我在 POJO 中保留了 drawable 的 resourceId,并使用以下方法在适配器中将 drawable 设置为 ImageView:
icon.setImageDrawable(mContext.getResources().getDrawable(item.getResourceId()));
希望它能对某人有所帮助。如果需要更多信息,请告诉我
我需要用图标及其标题填充 ListView
。我在资源中为菜单图标及其标题创建了数组,如:
对于标题:
<array name="menu_title_array">
<item>@string/menu_name_text</item>
</array>
对于图标:
<array name="menu_icons_array">
<item>@drawable/menu_icon_name</item>
</array>
我使用 xml select 或像这样的可绘制对象为每个图标创建了一个可绘制对象:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/icon_name_disabled" android:state_enabled="false"/>
<item android:drawable="@drawable/icon_name_press" android:state_pressed="true"/>
<item android:drawable="@drawable/icon_name_press" android:state_activated="true"/>
<item android:drawable="@drawable/icon_name"/>
</selector>
现在我正在创建一个包含 title
和 drawable
的 MenuItems 列表,来自我创建的 menu_title_array
和 menu_icons_array
数组:
TypedArray titles = getResources().obtainTypedArray(R.array.menu_title_array);
TypedArray icons = getResources().obtainTypedArray(R.array.menu_icons_array);
for (int index = 0; index < menuRowText.length(); mainMenuIndex++) {
menuRowItemList.add(new MenuRowItem(icons.getDrawable(index), titles.getString(index ));
}
现在我在适配器 getView()
中设置标题及其图标,方法如下:
title.setText(menuItem.getTitle());
icon.setImageDrawable(menuItem.getDrawable());
现在,如果我 select 一个菜单项,那么图标的状态应该根据我为它创建的 <selector>
而改变。 但它并没有改变它的状态。
如果我为每个菜单项创建一个 StateListDrawable
,例如:
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
mContext.getResources().getDrawable(R.drawable.icon_name_press));
states.addState(new int[] {android.R.attr.state_activated},
mContext.getResources().getDrawable(R.drawable.icon_name_press));
states.addState(new int[] { },
mContext.getResources().getDrawable(R.drawable.icon_name));
并将其应用于列表图标,如:
icon.setImageDrawable(states);
然后它正常工作,因为它正在改变它的状态激活,按下。
请指出我哪里出错了,我不想为每个 MenuItem 使用 StateListDrawable
创建可绘制图标,因为应用程序中会有很多样板代码。
ListView 正在使用 List<MenuRowItem>
进行填充。这是以下 pojo 的结构:
class MenuRowItem {
private Drawable drawable;
private String title;
MenuRowItem(Drawable drawable, String title) {
this.drawable = drawable;
this.title = title;
}
public Drawable getDrawable() {
return drawable;
}
public String getText() {
return mText;
}
如果需要更多详细信息,请告诉我。
通过将 drawable
的 id
保留在 MenuRowItem
中而不是保留 Drawable
对象解决了问题。
现在是 MenuRowItem
的 POJO:
class MenuRowItem {
private int drawableResourceId;
private String title;
MenuRowItem(int resourceId, String title) {
this.drawableResourceId = resourceId;
this.title = title;
}
public int getResourceId() {
return drawableResourceId;
}
public String getText() {
return mText;
}
}
我相信 Drawable(enable, activated, normal)
丢失状态,同时将可绘制对象保留为对象。
因此,我在 POJO 中保留了 drawable 的 resourceId,并使用以下方法在适配器中将 drawable 设置为 ImageView:
icon.setImageDrawable(mContext.getResources().getDrawable(item.getResourceId()));
希望它能对某人有所帮助。如果需要更多信息,请告诉我