今天项目用到PreferenceActivity时,也是今天才刚接触,于是自己稍微研究了下,它主要用作设置界面,用于一个界面同时保存多个设置的数据,其底层也是用sharedProperty来实现的。
使用步骤:
1.自己写一个继承PreferenceActivity的类 如
public class SettingsActivity extends PreferenceActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
//这是把res/xml下的PreferenceScreen加入到PreferenceActivity 中
addPreferencesFromResource(R.xml.preferences);
setContentView(R.layout.settings);
}
}
2.必须在res/xml文件下建立一个preferences.xml文件
preferences.xml内容
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/app_name"
android:summary="Application settings">
<EditTextPreference android:key="user_name"
android:defaultValue="@null"
android:title="User Name"
android:summary="Personalize your Jamendo client" />
<ListPreference
android:key="download_format"
android:title="Download format"
android:summary="Select your preferred codec"
android:entries="@array/stream_codecs"
android:entryValues="@array/stream_codecs_values" />
<CheckBoxPreference android:key="wifi_only"
android:defaultValue="false"
android:title="Wifi only mode"
android:summary="Listen only in wifi network area" />
<CheckBoxPreference android:key="roaming_protection"3.
android:defaultValue="true"
android:title="Roaming Protection"
android:summary="Disable listening abroad" />
</PreferenceCategory>
<PreferenceCategory android:title="3rd Party Applications"
android:summary="Interaction with outside applications">
<CheckBoxPreference android:key="lastfm_scrobble"
android:defaultValue="false"
android:title="Last.fm Scrobbling"
android:summary="Requires official last.fm client" />
</PreferenceCategory>
</PreferenceScreen>
3.建立PreferenceActivity显示的布局文件settings.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#000000">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="@drawable/gradient_dark_purple"
android:orientation="horizontal" android:gravity="center"
android:minHeight="75dip">
<LinearLayout android:layout_height="wrap_content"
android:minHeight="75dip" android:layout_width="fill_parent"
android:orientation="horizontal" android:gravity="center_vertical"
android:paddingLeft="13dip" android:paddingRight="13dip">
<ImageView android:layout_width="48dip"
android:layout_height="48dip" android:src="@drawable/settings"></ImageView>
<LinearLayout android:layout_height="wrap_content"
android:paddingLeft="13dip" android:orientation="vertical"
android:layout_width="fill_parent">
<TextView android:layout_width="wrap_content"
android:singleLine="true" android:layout_height="wrap_content"
android:textSize="18dip" android:textColor="#ffffff" android:text="@string/settings"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="12dip"
android:textColor="#ffffff"></TextView>
<TextView android:id="@+id/ItemsCountTextView"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="right" android:textSize="12dip"
android:textColor="#ffffff" android:text=" "></TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<!-- 这个用于显示PreferenceActivity中preferenceScreen的-->
<ListView android:layout_width="fill_parent" android:id="@android:id/list"
android:layout_weight="1" android:layout_height="fill_parent">
</ListView>
</LinearLayout>
就只这么简单 完了,也许有些朋友会奇怪 为什么在settting.xml中能显示preference.xml中preferencescreen呢
进入PreferenceActivity中才知道
因为PreferenceActivity 是继承ListActivity的
它其中有个 private void bindPreferences() {
final PreferenceScreen preferenceScreen = getPreferenceScreen();
if (preferenceScreen != null) {
//这就是为什么在settting.xml中能显示preference.xml中preferencescreen的原因
preferenceScreen.bind(getListView());
if (mSavedInstanceState != null) {
super.onRestoreInstanceState(mSavedInstanceState);
mSavedInstanceState = null;
}
}
}