Android ActionBar中的主开/关开关

Android ActionBar中的主开/关开关,android,android-actionbar,android-preferences,Android,Android Actionbar,Android Preferences,我已经使用Android的PreferenceAPI设计了一个设置 但我不知道如何创建“主开关”。这里提到了它,但没有关于如何实施它的文件 从技术上讲,它是一个位于操作栏中的开关首选项,当它关闭时,它将禁用所有子设置 有什么想法吗?您必须使用开关创建自定义操作栏,然后才能实现此目的,有关帮助,请参见此 及 您必须手动创建它。这是您应该实现的模式。您可以在操作栏中添加一个按钮,单击可以启用或禁用该功能,并相应地更改可绘制的图形。您可以使用开关标记,但这仅在api 14中介绍。例如 <?

我已经使用Android的PreferenceAPI设计了一个设置

但我不知道如何创建“主开关”。这里提到了它,但没有关于如何实施它的文件

从技术上讲,它是一个位于操作栏中的开关首选项,当它关闭时,它将禁用所有子设置


有什么想法吗?

您必须使用开关创建自定义操作栏,然后才能实现此目的,有关帮助,请参见此


您必须手动创建它。这是您应该实现的模式。您可以在操作栏中添加一个按钮,单击可以启用或禁用该功能,并相应地更改可绘制的图形。您可以使用开关标记,但这仅在api 14中介绍。例如

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Switch
        android:id="@+id/switchForActionBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</RelativeLayout>
以及preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="Test custom preferences" >

        <android.dumdum.TogglePreference />
    </PreferenceCategory>

</PreferenceScreen>


谢谢。可以使用吗?然后它会自动保存该值。这再次添加到api 14中。你的活动是首选项活动吗?不是,这是一个正常的活动,我从这里开始“设置片段扩展首选项片段”。我想知道android在自己的“设置”中是如何做的。谢谢第一个链接。这对我很有用,因为它是我想要的+1.
public class TogglePreference extends Preference {

    public TogglePreference(Context context) {
        super(context);
    }

    public TogglePreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TogglePreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public View getView(View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = new LinearLayout(getContext());
            ((LinearLayout) convertView)
                    .setOrientation(LinearLayout.HORIZONTAL);

            TextView txtInfo = new TextView(getContext());

            txtInfo.setText("Test");
            ((LinearLayout) convertView).addView(txtInfo,
                    new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.FILL_PARENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT, 1));

            ToggleButton btn = new ToggleButton(getContext());
            ((LinearLayout) convertView).addView(btn);
        }

        return convertView;
    }
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="Test custom preferences" >

        <android.dumdum.TogglePreference />
    </PreferenceCategory>

</PreferenceScreen>