Android AppCompat v21提供的SwitchCompat不提供SwitchCompatPerefrence

nLL*_*nLL 14 android android-appcompat switchcompat switchpreference

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

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

它看起来像AppCompact v21半完成.

我错过了什么吗?

小智 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" />
Run Code Online (Sandbox Code Playgroud)

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

<com.cgollner.unclouded.preferences.SwitchCompatPreference
            android:defaultValue="off"
            android:key="key1"
            android:title="@string/title1" />
Run Code Online (Sandbox Code Playgroud)

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


Tom*_*Tom 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" />
Run Code Online (Sandbox Code Playgroud)

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

<?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" />
Run Code Online (Sandbox Code Playgroud)