XML:
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
爪哇:
import org.holoeverywhere.app.ListFragment;
import org.holoeverywhere.widget.ListView;
....
....
private void listViewInit() {
getListView().setOnItemClickListener(this);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView().setMultiChoiceModeListener(getMultiChoiceModeListener());
getListView().setSelector(R.drawable.activated_background); // Error there
setListAdapter(adapter);
}
激活的背景是
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@color/row_activated"/>
<item android:drawable="@android:color/transparent"/>
</selector>
在模拟器(android 2.3.3)中,当 ListView 附近出现在屏幕上时,我有下一个异常:
E/AndroidRuntime: FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: File res/drawable/activated_background.xml from drawable resource ID #0x7f020131
at org.holoeverywhere.drawable.DrawableCompat.loadDrawable(DrawableCompat.java:138)
at org.holoeverywhere.drawable.DrawableCompat.getDrawable(DrawableCompat.java:107)
at org.holoeverywhere.widget.ListView.setSelector(ListView.java:954)
at org.kidinov.unixadmin.FileManagerFragment.init(FileManagerFragment.java:211)
at org.kidinov.unixadmin.FileManagerFragment.go(FileManagerFragment.java:183)
at org.kidinov.unixadmin.FileManagerFragment.onResume(FileManagerFragment.java:97)
at android.support.v4.app.Fragment.performResume(Fragment.java:1503)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:948)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1089)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1445)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1c/d=0x0 a=2 r=0x7f090011}
at org.holoeverywhere.drawable.DrawableCompat.loadDrawable(DrawableCompat.java:128)
at org.holoeverywhere.drawable.DrawableCompat.getDrawable(DrawableCompat.java:107)
at org.holoeverywhere.drawable.StateListDrawable.inflate(StateListDrawable.java:194)
at org.holoeverywhere.drawable.DrawableCompat.createFromXmlInner(DrawableCompat.java:87)
at org.holoeverywhere.drawable.DrawableCompat.createFromXml(DrawableCompat.java:63)
at org.holoeverywhere.drawable.DrawableCompat.loadDrawable(DrawableCompat.java:135)
如果我从代码中删除getListView().setSelector(R.drawable.activated_background);
,并将其作为我列表自定义行的背景移动到 XML 中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/activated_background">
....
</RelativeLayour>
它适用于 4+,但在 2.3 选定的行中没有任何例外地突出显示。
欢迎任何帮助。