我是Android的新手,我很感激你的帮助。这是我的第一个问题= D很抱歉只提供两个链接而且没有图片,我没有声名鹊起让我的问题更清楚
根据android API guides for settings,我正在使用PreferenceFragment,因为我正在为Android 3.0(API级别11)及更高版本开发。
我想从设置链接的android API指南中获得如图4和图5所示的效果。
在平板电脑模拟器上它看起来不错,但在手机模拟器(和真实设备)上我无法实现它。相反,我得到" Sound","显示","存储","电池" ...作为PreferenceCategory(如" Device& #34;似乎)后面应该是新屏幕(活动)中的所有选项。全部只有一个屏幕。
以下是我的文件,提前谢谢
Settings.java (注意:由我自己生成并完成)
package com.example;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.RingtonePreference;
import android.text.TextUtils;
import java.util.List;
import java.util.concurrent.FutureTask;
/**
* A {@link PreferenceActivity} that presents a set of application settings. On
* handset devices, settings are presented as a single list. On tablets,
* settings are split by category, with category headers shown to the left of
* the list of settings.
* <p>
* See <a href="http://developer.android.com/design/patterns/settings.html">
* Android Design: Settings</a> for design guidelines and the <a
* href="http://developer.android.com/guide/topics/ui/settings.html">Settings
* API Guide</a> for more information on developing a Settings UI.
*/
public class Settings extends PreferenceActivity {
// From stackoverflow to avoid error API 19
protected boolean isValidFragment(String fragmentName) {
if (ProfilePreferenceFragment.class.getName().equals(fragmentName)) {
return true;
} else if (ChatPreferenceFragment.class.getName().equals(fragmentName)) {
return true;
} else if (NotificationPreferenceFragment.class.getName().equals(
fragmentName)) {
return true;
} else if (ContactPreferenceFragment.class.getName().equals(
fragmentName)) {
return true;
} else if (PaymentPreferenceFragment.class.getName().equals(
fragmentName)) {
return true;
} else if (AccessibilityPreferenceFragment.class.getName().equals(
fragmentName)) {
return true;
} else {
return false;
}
}// From stackoverflow to avoid error API 19
/**
* Determines whether to always show the simplified settings UI, where
* settings are presented in a single list. When false, settings are shown
* as a master/detail two-pane view on tablets. When true, a single pane is
* shown on tablets.
*/
private static final boolean ALWAYS_SIMPLE_PREFS = false;
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
setupSimplePreferencesScreen();
}
/**
* Shows the simplified settings UI if the device configuration
* dictates that a simplified, single-pane UI should be
* shown.
*/
private void setupSimplePreferencesScreen() {
if (!isSimplePreferences(this)) {
return;
}
// In the simplified UI, fragments are not used at all and we instead
// use the older PreferenceActivity APIs.
// Add 'profile' preferences.
addPreferencesFromResource(R.xml.pref_profile);
// Add 'chats' preferences, and a corresponding header.
PreferenceCategory fakeHeader = new PreferenceCategory(this);
fakeHeader.setTitle(R.string.pref_header_chat);
getPreferenceScreen().addPreference(fakeHeader);
addPreferencesFromResource(R.xml.pref_chat);
// Add 'notification' preferences, and a corresponding header.
fakeHeader = new PreferenceCategory(this);
fakeHeader.setTitle(R.string.pref_header_notification);
getPreferenceScreen().addPreference(fakeHeader);
addPreferencesFromResource(R.xml.pref_notification);
// Add 'contact' preferences, and a corresponding header.
fakeHeader = new PreferenceCategory(this);
fakeHeader.setTitle(R.string.pref_header_contact);
getPreferenceScreen().addPreference(fakeHeader);
addPreferencesFromResource(R.xml.pref_contact);
// Add 'payment' preferences, and a corresponding header.
fakeHeader = new PreferenceCategory(this);
fakeHeader.setTitle(R.string.pref_header_payment);
getPreferenceScreen().addPreference(fakeHeader);
addPreferencesFromResource(R.xml.pref_payment);
// Add 'accessibility' preferences, and a corresponding header.
fakeHeader = new PreferenceCategory(this);
fakeHeader.setTitle(R.string.pref_header_accessibility);
getPreferenceScreen().addPreference(fakeHeader);
addPreferencesFromResource(R.xml.pref_accesibility);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences to
// their values. When their values change, their summaries are updated
// to reflect the new value, per the Android Design guidelines.
// bindPreferenceSummaryToValue(findPreference("example_text"));
// bindPreferenceSummaryToValue(findPreference("example_list"));
// bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
// bindPreferenceSummaryToValue(findPreference("sync_frequency"));
}
/** {@inheritDoc} */
@Override
public boolean onIsMultiPane() {
return isXLargeTablet(this) && !isSimplePreferences(this);
}
/**
* Helper method to determine if the device has an extra-large screen. For
* example, 10" tablets are extra-large.
*/
private static boolean isXLargeTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
}
/**
* Determines whether the simplified settings UI should be shown. This is
* true if this is forced via {@link #ALWAYS_SIMPLE_PREFS}, or the device
* doesn't have newer APIs like {@link PreferenceFragment}, or the device
* doesn't have an extra-large screen. In these cases, a single-pane
* "simplified" settings UI should be shown.
*/
private static boolean isSimplePreferences(Context context) {
return ALWAYS_SIMPLE_PREFS
|| Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB
|| !isXLargeTablet(context);
}
/** {@inheritDoc} */
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onBuildHeaders(List<Header> target) {
if (!isSimplePreferences(this)) {
loadHeadersFromResource(R.xml.pref_headers, target);
}
}
/**
* A preference value change listener that updates the preference's summary
* to reflect its new value.
*/
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference
.setSummary(index >= 0 ? listPreference.getEntries()[index]
: null);
} else if (preference instanceof RingtonePreference) {
// For ringtone preferences, look up the correct display value
// using RingtoneManager.
if (TextUtils.isEmpty(stringValue)) {
// Empty values correspond to 'silent' (no ringtone).
} else {
Ringtone ringtone = RingtoneManager.getRingtone(
preference.getContext(), Uri.parse(stringValue));
if (ringtone == null) {
// Clear the summary if there was a lookup error.
preference.setSummary(null);
} else {
// Set the summary to reflect the new ringtone display
// name.
String name = ringtone
.getTitle(preference.getContext());
preference.setSummary(name);
}
}
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
};
/**
* Binds a preference's summary to its value. More specifically, when the
* preference's value is changed, its summary (line of text below the
* preference title) is updated to reflect the value. The summary is also
* immediately updated upon calling this method. The exact display format is
* dependent on the type of preference.
*
* @see #sBindPreferenceSummaryToValueListener
*/
private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference
.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(
preference,
PreferenceManager.getDefaultSharedPreferences(
preference.getContext()).getString(preference.getKey(),
""));
}
/**
* This fragment shows profile preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class ProfilePreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_profile);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
// bindPreferenceSummaryToValue(findPreference(""));
// bindPreferenceSummaryToValue(findPreference(""));
}
}
/**
* This fragment shows chat preferences only. It is used when the activity
* is showing a two-pane settings UI.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class ChatPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_chat);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference("pref_chat_enter_send"));
bindPreferenceSummaryToValue(findPreference("pref_chat_download_message"));
bindPreferenceSummaryToValue(findPreference("pref_chat_download_multimedia"));
bindPreferenceSummaryToValue(findPreference("pref_chat_font_size"));
bindPreferenceSummaryToValue(findPreference("pref_chat_wallpaper"));
bindPreferenceSummaryToValue(findPreference("pref_chat_cloud"));
}
}
/**
* This fragment shows notification preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class NotificationPreferenceFragment extends
PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_notification);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference("pref_notification_new_contact"));
bindPreferenceSummaryToValue(findPreference("pref_notification_cat_individual_vibration"));
bindPreferenceSummaryToValue(findPreference("pref_notification_cat_individual_vibration_repeat"));
bindPreferenceSummaryToValue(findPreference("pref_notification_cat_individual_led"));
bindPreferenceSummaryToValue(findPreference("pref_notification_cat_individual_led_repeat"));
bindPreferenceSummaryToValue(findPreference("pref_notification_cat_individual_screen"));
bindPreferenceSummaryToValue(findPreference("pref_notification_cat_individual_screen_repeat"));
bindPreferenceSummaryToValue(findPreference("pref_notification_cat_group_vibration"));
bindPreferenceSummaryToValue(findPreference("pref_notification_cat_group_vibration_repeat"));
bindPreferenceSummaryToValue(findPreference("pref_notification_cat_group_led"));
bindPreferenceSummaryToValue(findPreference("pref_notification_cat_group_led_repeat"));
bindPreferenceSummaryToValue(findPreference("pref_notification_cat_group_screen"));
bindPreferenceSummaryToValue(findPreference("pref_notification_cat_group_screen_repeat"));
}
}
/**
* This fragment shows contact preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class ContactPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_contact);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
// bindPreferenceSummaryToValue(findPreference(""));
// bindPreferenceSummaryToValue(findPreference(""));
}
}
/**
* This fragment shows payment preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class PaymentPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_payment);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
// bindPreferenceSummaryToValue(findPreference(""));
// bindPreferenceSummaryToValue(findPreference(""));
}
}
}
pref_headers.xml
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- These settings headers are only used on tablets. -->
<header
android:fragment="com.example.Settings$ProfilePreferenceFragment"
android:title="@string/pref_header_profile" />
<header
android:fragment="com.example.Settings$ChatPreferenceFragment"
android:title="@string/pref_header_chat" />
<header
android:fragment="com.example.Settings$NotificationPreferenceFragment"
android:title="@string/pref_header_notification" />
<header
android:fragment="com.example.Settings$ContactPreferenceFragment"
android:title="@string/pref_header_contact" />
<header
android:fragment="com.example.Settings$PaymentPreferenceFragment"
android:title="@string/pref_header_payment" />
</preference-headers>
和一个部分的例子,其他的都是这样的。的 pref_notification.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<SwitchPreference
android:key="pref_notification_new_contact"
android:summary="@string/pref_notification_new_contact_summary"
android:title="@string/pref_notification_new_contact_title" />
<PreferenceCategory
android:key="pref_notification_cat_individual"
android:title="@string/pref_notification_cat_individual" >
<SwitchPreference
android:defaultValue="true"
android:key="pref_notification_cat_individual_vibration"
android:title="@string/pref_notification_cat_individual_vibration_title" />
<CheckBoxPreference
android:defaultValue="true"
android:dependency="pref_notification_cat_individual_vibration"
android:key="pref_notification_cat_individual_vibration_repeat"
android:summary="@string/pref_notification_cat_individual_vibration_repeat_summary"
android:title="@string/pref_notification_cat_individual_vibration_repeat" />
<SwitchPreference
android:defaultValue="true"
android:key="pref_notification_cat_individual_led"
android:title="@string/pref_notification_cat_individual_led_title" />
<CheckBoxPreference
android:defaultValue="true"
android:dependency="pref_notification_cat_individual_led"
android:key="pref_notification_cat_individual_led_repeat"
android:summary="@string/pref_notification_cat_individual_led_repeat_summary"
android:title="@string/pref_notification_cat_individual_led_repeat" />
<SwitchPreference
android:defaultValue="true"
android:key="pref_notification_cat_individual_screen"
android:title="@string/pref_notification_cat_individual_screen_title" />
<CheckBoxPreference
android:defaultValue="true"
android:dependency="pref_notification_cat_individual_screen"
android:key="pref_notification_cat_individual_screen_repeat"
android:summary="@string/pref_notification_cat_individual_screen_repeat_summary"
android:title="@string/pref_notification_cat_individual_screen_repeat" />
</PreferenceCategory>
<PreferenceCategory
android:key="pref_notification_cat_group"
android:title="@string/pref_notification_cat_group" >
<SwitchPreference
android:defaultValue="true"
android:key="pref_notification_cat_group_vibration"
android:title="@string/pref_notification_cat_group_vibration_title" />
<CheckBoxPreference
android:defaultValue="true"
android:dependency="pref_notification_cat_group_vibration"
android:key="pref_notification_cat_group_vibration_repeat"
android:summary="@string/pref_notification_cat_group_vibration_repeat_summary"
android:title="@string/pref_notification_cat_group_vibration_repeat" />
<SwitchPreference
android:defaultValue="true"
android:key="pref_notification_cat_group_led"
android:title="@string/pref_notification_cat_group_led_title" />
<CheckBoxPreference
android:defaultValue="true"
android:dependency="pref_notification_cat_group_led"
android:key="pref_notification_cat_group_led_repeat"
android:summary="@string/pref_notification_cat_group_led_repeat_summary"
android:title="@string/pref_notification_cat_group_led_repeat" />
<SwitchPreference
android:defaultValue="true"
android:key="pref_notification_cat_group_screen"
android:title="@string/pref_notification_cat_group_screen_title" />
<CheckBoxPreference
android:defaultValue="true"
android:dependency="pref_notification_cat_group_screen"
android:key="pref_notification_cat_group_screen_repeat"
android:summary="@string/pref_notification_cat_group_screen_repeat_summary"
android:title="@string/pref_notification_cat_group_screen_repeat" />
</PreferenceCategory>
</PreferenceScreen>