在Android Lollipop中扩展首选项类=丢失动画 [英] Extending Preference classes in Android Lollipop = losing animation

查看:123
本文介绍了在Android Lollipop中扩展首选项类=丢失动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅用于扩展 CheckBoxPreference

Just for extending CheckBoxPreference or SwitchPreference on Android Lollipop, the widget (the checkbox or the switch) won't have animation anymore.

我想将SwitchPreference扩展为强制api< 21使用SwitchCompat而不是他们正在使用的默认开关(这显然是错误的).

I'd like to extend SwitchPreference to force api < 21 to use SwitchCompat instead of the default one they are using (which is obviously wrong).

我正在使用新的 AppCompatPreferenceActivity ,但这似乎并不影响开关.

I am using the new AppCompatPreferenceActivity with appcompat-v7:22.1.1 but that doesn't seem to affect the switches.

问题是,只需扩展这些类,而无需添加任何自定义布局或小部件资源布局,动画就消失了.

The thing is that with just extending those classes, without adding any custom layout or widget resource layout, the animation is gone.

我知道我可以编写我的preference.xml的两个实例(在values-v21内部),并且可以工作...但是我想知道为什么会这样,并且如果有人知道解决方案而没有两个偏好.xml.

I know I can write two instances of my preference.xml (on inside values-v21) and it will work... But I'd like to know why is this happening and if somebody knows a solution without having two preference.xml.

代码示例:

public class SwitchPreference extends android.preference.SwitchPreference {

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

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public SwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

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

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

与CheckBoxPreference相同或相同,然后使用:

This or the same for CheckBoxPreference and then using:

<com.my.package.SwitchPreference />

将使Lollipop设备中的动画消失.

Will make the animation in a Lollipop device to be gone.

-

我尝试使用SwitchPreference的另一件事(我可以使用CheckBoxPreference)是给布局提供默认ID,但@android:id/switchWidget

Another thing I tried for the SwitchPreference (that I can with CheckBoxPreference) is to give a layout with the default id but @android:id/switchWidgetis not public while @android:id/checkbox is. I also know I can use a <CheckBoxPreference /> and give a widget layout that is in fact a SwitchCompat, but I'd like to avoid that (confusing the names).

推荐答案

我设法解决了这个问题,动画在没有动画的情况下就可以正常工作了.

i manage to fix it like this and animations is working before it was going to the state directly without animation:

FIX:

CustomSwitchCompat.class

public class CustomSwitchCompat extends SwitchCompat {

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

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

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

    @Override
    public boolean isShown() {
        return getVisibility() == VISIBLE;
    }

}

在您的布局中执行以下操作:preference_switch_layout.xml

<com.example.CustomSwitchCompat
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@android:id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@null"
    android:clickable="false"
    android:focusable="false"
    app:switchMinWidth="55dp"/>

,然后在您的preference.xml中执行以下操作:

<CheckBoxPreference
   android:defaultValue="false"
   android:key=""
   android:widgetLayout="@layout/preference_switch_layout"
   android:summary=""
   android:title="" />

这篇关于在Android Lollipop中扩展首选项类=丢失动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆