How do I make a TwoLineListItem?
I want to make a seting开发者_如何学运维s menu like this:
Is there a definitive guide on how to make TwoLineListItem
's anywhere?
You will need to use a custom listrow with a custom adapter.
Here is an example: http://geekswithblogs.net/bosuch/archive/2011/01/31/android---create-a-custom-multi-line-listview-bound-to-an.aspx
In your case, extend PreferenceActivity and in the onCreate method:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
getPreferences();
}
Where you can inflate your view using a preferences.xml file - add a folder called xml to your res folder and add an xml file with stuff like:
<CheckBoxPreference
android:title="@string/pref_sub_notify"
android:defaultValue="true"
android:summary="@string/pref_sub_notify_summary"
android:key="subNotifyPref" />
<ListPreference
android:title="@string/pref_sub_expiry_warn"
android:summary="@string/pref_sub_expiry_summary"
android:key="subExpiryDelayPref"
android:defaultValue="7"
android:entries="@array/sub_expiry_delay"
android:entryValues="@array/sub_expiry_delay_values" />
In this case, the title is your first line and the summary is the second line.
You need a custom View
and a ListActivity. This tutorial may help. If you are working on a Settings Activity, why not extend PreferenceActivity?
There is a pretty good tutorial here on how to create custom list items. The basic idea is that you define the layout of the list item just as you define the layout for an Activity
. You then subclass ArrayAdapter
(or BaseAdapter
), and override the getView(...)
method to return your custom list item View
.
I had a similar problem, but my desired list's item should have an image followed by two strings, one above the other (just as you want). I found the solution here. It worked easily for me and I think it will work well for you too (you just need to remove the ImageView
part appropriately).
精彩评论