如何更改 ListView 的可绘制复选框?
How to change the checkbox drawable of a ListView?
我是 Android 编程新手,如有任何帮助,我们将不胜感激!我有一个 ListActivity,如图所示:
public class BasicViews5Activity extends ListActivity {
String[] names;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setTextFilterEnabled(true);
names = getResources().getStringArray(R.array.names_array);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,names));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(this,"You have selected " + names[position], Toast.LENGTH_SHORT).show();
}
}
如何更改 ListView 中 CheckBox 的可绘制对象?
我的 Xml 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Show selected items"
android:onClick="onClick"/>
<ListView
android:id="@+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/checked"/>
</LinearLayout>
您的问题已有回复here:
I didn´t like the appearance of the checkbox default image when the background layout was dark, so I changed it using a xml selector in order to have a white background in the button.
The selector is needed in android:button="@drawable/selector_check"
如果有其他新手遇到类似问题:
您有一个 class 扩展了 ListActivity 或 ListFragment。在
onActivityCreated() 方法你需要设置列表 Adapter:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.rowlayout, R.id.label, values);
setListAdapter(adapter);
在您的 rowlayout.xml 中,您需要一个 ID 为 'label' 的 TextView,这将是
用 'values' 数组中的字符串填充。
在 rowlayout.xml 中,您需要一个带有按钮属性集的复选框:
android:button="@drawable/custom_checkbox_design"
在 custom_checkbox_design.xml 内,您需要以下内容:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checked" android:state_checked="true"/>
<item android:drawable="@drawable/unchecked" android:state_checked="false"/>
</selector>
我是 Android 编程新手,如有任何帮助,我们将不胜感激!我有一个 ListActivity,如图所示:
public class BasicViews5Activity extends ListActivity {
String[] names;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setTextFilterEnabled(true);
names = getResources().getStringArray(R.array.names_array);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,names));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(this,"You have selected " + names[position], Toast.LENGTH_SHORT).show();
}
}
如何更改 ListView 中 CheckBox 的可绘制对象?
我的 Xml 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Show selected items"
android:onClick="onClick"/>
<ListView
android:id="@+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/checked"/>
</LinearLayout>
您的问题已有回复here:
I didn´t like the appearance of the checkbox default image when the background layout was dark, so I changed it using a xml selector in order to have a white background in the button.
The selector is needed in android:button="@drawable/selector_check"
如果有其他新手遇到类似问题:
您有一个 class 扩展了 ListActivity 或 ListFragment。在 onActivityCreated() 方法你需要设置列表 Adapter:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.rowlayout, R.id.label, values);
setListAdapter(adapter);
在您的 rowlayout.xml 中,您需要一个 ID 为 'label' 的 TextView,这将是 用 'values' 数组中的字符串填充。
在 rowlayout.xml 中,您需要一个带有按钮属性集的复选框:
android:button="@drawable/custom_checkbox_design"
在 custom_checkbox_design.xml 内,您需要以下内容:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checked" android:state_checked="true"/>
<item android:drawable="@drawable/unchecked" android:state_checked="false"/>
</selector>