Switch Preference Compat not animating(支持偏好v7)

时间:2016-10-09 09:53:27

标签: android

背景 我正在使用支持首选项v7库的SwitchPreferenceCompat,如下所示,以便为Switch标题创建多行:

public class MoreLinesSwitchPreference extends SwitchPreferenceCompat {
    public MoreLinesSwitchPreference(Context context) {
        super(context);
    }

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

    public MoreLinesSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);

        TextView textView = (TextView) holder.itemView.findViewById(android.R.id.title);
        if (textView != null) {
            textView.setSingleLine(false);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        }
    }
}

我的Preferences.xml:

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

    <com.sed.al_mabadi_ul_mufeedah.MyCustomListPreference
        android:defaultValue="20"
        android:entries="@array/fontSize"
        android:entryValues="@array/fontSize"
        android:icon="@drawable/ic_format_size_24dp"
        android:key="fontSize"
        android:summary="20"
        android:title="@string/settings_font_size" />

    <com.sed.al_mabadi_ul_mufeedah.MyCustomListPreference
        android:defaultValue="DroidNaskh-Regular"
        android:entries="@array/font_face"
        android:entryValues="@array/font_face"
        android:icon="@drawable/ic_font_24dp"
        android:key="fontFace"
        android:summary="Choose Font Name"
        android:title="@string/settings_font_name" />

    <com.sed.al_mabadi_ul_mufeedah.MyCustomListPreference
        android:defaultValue="en"
        android:entries="@array/language"
        android:entryValues="@array/language_short"
        android:icon="@drawable/ic_language_24dp"
        android:key="language"
        android:summary="@string/app_restart"
        android:title="@string/app_language_title" />

    <com.sed.al_mabadi_ul_mufeedah.MoreLinesSwitchPreference
        android:defaultValue="true"
        android:icon="@drawable/ic_harakath_8b64ce_24dp"
        android:key="diacriticsOn"
        android:summary="@string/app_restart"
        android:title="@string/show_hide_diacritics" />

    <com.sed.al_mabadi_ul_mufeedah.MoreLinesSwitchPreference
        android:defaultValue="false"
        android:icon="@drawable/ic_night_24dp"
        android:key="nightModeOn"
        android:summary="@string/app_restart"
        android:title="@string/settings_night_mode" />

    <com.sed.al_mabadi_ul_mufeedah.MoreLinesSwitchPreference
        android:defaultValue="false"
        android:icon="@drawable/ic_screen_on"
        android:key="alwaysOn"
        android:summary="@string/sleep_on"
        android:title="@string/settings_screen_on" />

</android.support.v7.preference.PreferenceScreen>

来自我的编译依赖项:

compile 'com.android.support:preference-v7:24.2.0'

MyStyle.xml:

<style name="AppTheme.Base" parent="Theme.AppCompat.DayNight.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorControlActivated">@color/colorCursor</item>
    <item name="colorControlHighlight">@color/colorFocus</item>
    <item name="android:textColorPrimary">@color/colorPrimaryText</item>
    <item name="android:textColorSecondary">@color/colorSecondaryText</item>
    <item name="android:colorBackground">@color/background</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>

问题:

SwitchPreferenceCompat不会在ON和OFF值之间设置动画。

我研究过:

Why SwitchPreference is not showing animation when switching from on to off & vice versa?

Extending Preference classes in Android Lollipop = losing animation

https://code.google.com/p/android/issues/detail?id=185323

为什么以上内容无法回答我的问题:

与SwitchPreference相关的上述帖子和其他帖子并未谈及 SwitchPreferenceCompat 而非简单的Switch或SwitchPreference

需要帮助设置ON和OFF值之间的切换动画。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

The problem is fixed now. I updated the compile dependencies from

compile 'com.android.support:preference-v7:24.2.0'

to

compile 'com.android.support:preference-v7:25.3.0'

and now the same switch preference is animating. This could probably be a bug in the older versions of support preference.

相关问题