效果:
新建的xml文件(根结点是PreferenceScreen):
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<Preference
android:key="here_is_key"
android:title="here_is_title"
android:summary="here_is_summary"
/>
</PreferenceScreen>
代码:
package com.sg31.preferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.view.Menu;
import android.view.MenuItem;
// 已过时,继承自:PreferenceActivity
public class MainActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
Preference pfPreference = findPreference("here_is_key");
pfPreference.setSummary("this is new summary");
}
}