---
title: "设置属性页面"
author: "canofy"
date: "2011-03-30T18:37:32Z"
tags: [".net", "Android", "OS", "XML"]
categories: ["移动开发"]
slug: "untitled"
draft: false
lastmod: "2011-03-30T18:37:32Z"
id: 980895
url: "https://canofy.iteye.com/blog/980895"
---

设置属性页面有两种方式 1、用xml完全的配置方式 2、完全使用代码的方式 xml配置方式： \[code="xml"] \[/code] 下面是代码的方式: \[code="java"] package com.example.android.apis.app; import android.content.Intent; import android.content.res.TypedArray; import android.net.Uri; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.PreferenceActivity; import android.preference.PreferenceCategory; import android.preference.PreferenceScreen; import com.example.android.apis.R; public class PreferencesFromCode extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setPreferenceScreen(createPreferenceHierarchy()); } private PreferenceScreen createPreferenceHierarchy() { // Root PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this); // Inline preferences PreferenceCategory inlinePrefCat = new PreferenceCategory(this); inlinePrefCat.setTitle(R.string.inline\_preferences); root.addPreference(inlinePrefCat); // Toggle preference CheckBoxPreference togglePref = new CheckBoxPreference(this); togglePref.setKey("toggle\_preference"); togglePref.setTitle(R.string.title\_toggle\_preference); togglePref.setSummary(R.string.summary\_toggle\_preference); inlinePrefCat.addPreference(togglePref); // Dialog based preferences PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this); dialogBasedPrefCat.setTitle(R.string.dialog\_based\_preferences); root.addPreference(dialogBasedPrefCat); // Edit text preference EditTextPreference editTextPref = new EditTextPreference(this); editTextPref.setDialogTitle(R.string.dialog\_title\_edittext\_preference); editTextPref.setKey("edittext\_preference"); editTextPref.setTitle(R.string.title\_edittext\_preference); editTextPref.setSummary(R.string.summary\_edittext\_preference); dialogBasedPrefCat.addPreference(editTextPref); // List preference ListPreference listPref = new ListPreference(this); listPref.setEntries(R.array.entries\_list\_preference); listPref.setEntryValues(R.array.entryvalues\_list\_preference); listPref.setDialogTitle(R.string.dialog\_title\_list\_preference); listPref.setKey("list\_preference"); listPref.setTitle(R.string.title\_list\_preference); listPref.setSummary(R.string.summary\_list\_preference); dialogBasedPrefCat.addPreference(listPref); // Launch preferences PreferenceCategory launchPrefCat = new PreferenceCategory(this); launchPrefCat.setTitle(R.string.launch\_preferences); root.addPreference(launchPrefCat); /* * The Preferences screenPref serves as a screen break (similar to page * break in word processing). Like for other preference types, we assign * a key here so that it is able to save and restore its instance state. \*/ // Screen preference PreferenceScreen screenPref = getPreferenceManager().createPreferenceScreen(this); screenPref.setKey("screen\_preference"); screenPref.setTitle(R.string.title\_screen\_preference); screenPref.setSummary(R.string.summary\_screen\_preference); launchPrefCat.addPreference(screenPref); /* * You can add more preferences to screenPref that will be shown on the * next screen. \*/ // Example of next screen toggle preference CheckBoxPreference nextScreenCheckBoxPref = new CheckBoxPreference(this); nextScreenCheckBoxPref.setKey("next\_screen\_toggle\_preference"); nextScreenCheckBoxPref.setTitle(R.string.title\_next\_screen\_toggle\_preference); nextScreenCheckBoxPref.setSummary(R.string.summary\_next\_screen\_toggle\_preference); screenPref.addPreference(nextScreenCheckBoxPref); // Intent preference PreferenceScreen intentPref = getPreferenceManager().createPreferenceScreen(this); intentPref.setIntent(new Intent().setAction(Intent.ACTION\_VIEW) .setData(Uri.parse("http://www.android.com"))); intentPref.setTitle(R.string.title\_intent\_preference); intentPref.setSummary(R.string.summary\_intent\_preference); launchPrefCat.addPreference(intentPref); // Preference attributes PreferenceCategory prefAttrsCat = new PreferenceCategory(this); prefAttrsCat.setTitle(R.string.preference\_attributes); root.addPreference(prefAttrsCat); // Visual parent toggle preference CheckBoxPreference parentCheckBoxPref = new CheckBoxPreference(this); parentCheckBoxPref.setTitle(R.string.title\_parent\_preference); parentCheckBoxPref.setSummary(R.string.summary\_parent\_preference); prefAttrsCat.addPreference(parentCheckBoxPref); // Visual child toggle preference // See res/values/attrs.xml for the that defines // TogglePrefAttrs. TypedArray a = obtainStyledAttributes(R.styleable.TogglePrefAttrs); CheckBoxPreference childCheckBoxPref = new CheckBoxPreference(this); childCheckBoxPref.setTitle(R.string.title\_child\_preference); childCheckBoxPref.setSummary(R.string.summary\_child\_preference); childCheckBoxPref.setLayoutResource( a.getResourceId(R.styleable.TogglePrefAttrs\_android\_preferenceLayoutChild, 0)); prefAttrsCat.addPreference(childCheckBoxPref); a.recycle(); return root; } } \[/code]