本文来自:点击打开链接
图1中上面的listtitle是一个listPreference,当你点击后会出现图2的效果,然后在图2中选择ABC其中一个,这个dialog会消失,并将选择的文本显示在图1中
而下面的部分是在PreferenceScreen中嵌套一个PreferenceScreen,在内部的PreferenceScreen中使用android:@layout/your_layout将你自定义的layout加入到内部的PreferenceScreen中,贴代码:
1、PreferenceScreen :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="using_categories_in_root_screen"
android:summary="Using Preference Categories"
android:title="Categories" >
<ListPreference
android:defaultValue="@string/manual_check"
android:dialogTitle="@string/make_choice"
android:entries="@array/frequency_choice"
android:entryValues="@array/frequency_choice"
android:key="list"
android:title="@string/check_frequency" />
<PreferenceCategory >
<PreferenceScreen
android:title="layut"
android:key="layout"
android:layout="@layout/your_activity_main" >
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
2、your_layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="100dp"
android:paddingLeft="50dp"
android:paddingBottom="100dp" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/recently_check_time"
android:textSize="24sp" />
<TextView
android:id="@+id/lastupdatetime_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/nothing" />
</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/btn_manualcheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="30dp"
android:layout_marginRight="20dp"
android:text="@string/now_check" />
</RelativeLayout>
</LinearLayout>
3、java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.frequency_check_time);
b_manualCheck = (Button) findViewById(R.id.btn_manualcheck);
b_manualCheck.setOnClickListener(new OnClickListenerImpl());
Preference listPreference = findPreference("list");
listPreference.setOnPreferenceChangeListener(this);
listPreference.setOnPreferenceClickListener(this);
String summary = PreferenceManager.getDefaultSharedPreferences(this)
.getString(listPreference.getKey(), "@string/manual_check");
listPreference.setSummary(summary);
}
private class OnClickListenerImpl implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
@Override
public boolean onPreferenceClick(Preference preference) {
return true;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
preference.setSummary((String) newValue);
return true;
}