Android AppCompat v21提供的SwitchCompat不提供SwitchCompatPerefrence

时间:2014-10-23 20:07:14

标签: android android-appcompat switchcompat switchpreference

似乎AppCompat v21提供的SwitchCompat不提供SwitchCompatPerefrence。

我知道我可以使用SwitchPreference,但它在视觉上并不相同。在Android 4.x上;当我在活动界面上使用v21的SwitchCompact时,它看起来像是材质切换按钮,但是,因为没有SwitchCompactPreference,我必须在我的pereference视图中使用SwitchPreference,显然它有Android 4.0外观。

看起来AppCompact v21已完成一半。

我错过了什么吗?

7 个答案:

答案 0 :(得分:10)

根据当前接受的答案和cgollner的要点,如果你只从那里采取xml布局: https://gist.github.com/cgollner/3c7fe2f9d34aee38bd0c

这样做:

<CheckBoxPreference
            android:widgetLayout="@layout/preference_switch_layout"
            android:defaultValue="off"
            android:key="key1"
            android:title="@string/title1" />

而不是这个(使用setWidgetLayoutResource从源添加布局):

<com.cgollner.unclouded.preferences.SwitchCompatPreference
            android:defaultValue="off"
            android:key="key1"
            android:title="@string/title1" />

然后动画也可以在棒棒糖和下面使用相同的xml。

答案 1 :(得分:6)

我为自己建立了一些东西,SwitchCompatPreference.java。扩展SwitchPreference是最简单的构建方式。遗憾的是,SwitchCompat并未继承Switch,因此原始SwitchPreference需要稍加修改。首选项使用如下:

<me.barrasso.android.volume.ui.SwitchCompatPreference
        android:icon="@drawable/icon"
        android:key="key"
        android:defaultValue="false"
        android:widgetLayout="@layout/pref_switch"
        android:title="@string/title"
        android:summary="@string/summary" />

布局非常简单,可根据需要进行调整。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SwitchCompat
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/toggle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:textIsSelectable="false"
    android:textStyle="bold" />

答案 2 :(得分:6)

答案 3 :(得分:3)

你不会喜欢这个答案,但我能想到的最好的方法是从SwitchCompat对象创建你自己的偏好:

http://developer.android.com/guide/topics/ui/settings.html#Custom

我知道这个答案并不是最好的,而且还没有代码示例。我将在周末尝试解决这个问题,并用我发现的内容更新这个答案。

答案 4 :(得分:1)

缺点:可能无法在每台设备上运行。

AppCompatDelegate中使用PreferenceActivity(您可以从AppCompatPreferenceActivity开始),并在创建SwitchCompat视图时覆盖onCreateView以返回Switch

即,

public abstract class AppCompatPreferenceActivity extends PreferenceActivity {
    private AppCompatDelegate mDelegate;

    //... other methods omitted for clarity

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        // Allow super to try and create a view first
        final View result = super.onCreateView(name, context, attrs);
        if (result != null) {
            return result;
        }

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            switch (name) {
                // ... can add other views here
                case "Switch":
                    return new SwitchCompat(this, attrs);
            }
        }
        return null;
    }
}

优点是,通过这种方式,您可以在不改变布局的情况下添加支持现有应用程序,并且只需最少的工作量。

答案 5 :(得分:1)

这是关于SwitchCompat小部件在接受的答案中的动画。

我发现问题是由Preference类中的一个标志引起的,在Android 4.0-4.3中,标志是mHasSpecifiedLayout,在Android 4.4中,标志是mCanRecycleLayout。

使用setWidgetLayoutResource设置小部件时,它将更改此标志。

如果使用不同的包名创建新的自定义首选项(android.preference或com.android除外),它也会更改此标志。

当mHasSpecifiedLayout为false或CanRecycleLayout为true时,动画将起作用,否则动画不起作用。

因此,您可以使用反射而不是setWidgetLayoutResource()方法设置窗口小部件布局,然后动画将不会被破坏。

这是一个片段:

        CheckBoxPreference switchPref = new CheckBoxPreference(getActivity());
        try {
            Field field = Preference.class.getDeclaredField("mWidgetLayoutResId");
            field.setAccessible(true);
            field.setInt(switchPref, R.layout.preference_switch_layout);
        } catch (Exception e) {
            switchPref.setWidgetLayoutResource(R.layout.preference_switch_layout);
        }
        switchPref.setKey(key);
        switchPref.setTitle(titleRes);
        switchPref.setSummary(summaryRes);
        switchPref.setDefaultValue(defaultValue);

答案 6 :(得分:-2)

来自official Android blog

  

如何将AppCompat与偏好设置一起使用?
  在API v11 +设备上运行时,您可以在ActionBarActivity中继续使用PreferenceFragment。对于之前的设备,您需要提供一个非物质样式的普通PreferenceActivity。