XML for Checked List Item in a ListView

江枫思渺然 提交于 2020-01-26 01:45:41

问题


I've been searching all over for an example of the XML needed to setup a ListView to use a checkbox. While I've been able to find the Java code needed, the XML for the list items remains surprisingly elusive. Are there any good resources to learn how to style these listitems appropriately?

When I tried modifying the base XML file I have, I got the following error:

java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

So I'm not sure how to create it on my own using more elements than just a TextView. So currently, I am using the integer value found here as the resource ID for the ArrayAdapter: http://developer.android.com/reference/android/R.layout.html#simple_list_item_checked

which looks like what I want, but I need to be able to find the XML for that resource and style it for my own needs. Any help would be greatly appreciated!


回答1:


Found it!

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/textCheckMark"
android:paddingLeft="6dip"
android:paddingRight="6dip"
/>



回答2:


You can always create a layout of your own and inflate the same. But, for this you need to write a customized code for Adapter.

Here is one good example to start.



来源:https://stackoverflow.com/questions/7990234/xml-for-checked-list-item-in-a-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!