为什么从切换到关闭&时SwitchPreference不显示动画?反之亦然? [英] Why SwitchPreference is not showing animation when switching from on to off & vice versa?

查看:253
本文介绍了为什么从切换到关闭&时SwitchPreference不显示动画?反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为应用程序的preferences设置了SwitchPreference.

I've made a SwitchPreference for my app's preferences.

问题是当我在&上切换时,SwitchPreference没有显示动画.而是突然跳动了.

The problem is that the SwitchPreference is not showing animation when I'm switching between on & off, rather, it is switching with a sudden jerk.

这是preferences.xml文件的代码:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <SwitchPreference
        android:id="@+id/notification"
        android:key="notification"
        android:title="@string/notification"
        android:defaultValue="true"/>
</PreferenceScreen>

推荐答案

原因

我注意到一些不同的情况可能会导致我的SwitchPreference对象缺少动画:

  1. ,如果SwitchPreference是设置活动中的第一个Preference.

  1. if the SwitchPreference is the very first Preference in the settings activity.

如果我扩展SwitchPreference并使用它(

if I extend the SwitchPreference and use that instead (post describing a similar problem).

修复

为避免第一个问题,我创建了一个DummyPreference类,该类用作PreferenceScreen中的第一个Preference.下面的示例.

Fixes

To avoid the first problem, I created a DummyPreference class which I used as the first Preference in the PreferenceScreen instead. Examples below.

DummyPreference.java

public class DummyPreference extends Preference
{
    public DummyPreference(Context context,AttributeSet attrs)
    {
        super(context,attrs);
    }

    @Override
    public View getView(View convertView,ViewGroup parent)
    {
        View v = new View(getContext());
        v.setVisibility(View.GONE);
        return v;
    }
}

pref_whatever.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <com.exaple.DummyPreference/>
    <!-- other preference controls here -->
</PreferenceScreen>

为避免第二个问题,我不得不诉诸于在XML中使用android普通的Preference类,并且将所需的任何额外逻辑移至包含Preference对象的ActivityFragment中.

To avoid the second problem, I had to just resort to using android's plain old Preference classes in the XML, and I moved any extra logic needed into the Activity or Fragment containing the Preference objects.

我知道这是旧帖子.我只是希望它将来可以对其他人有所帮助.

I know this is an old post. I'm just hoping that it may help someone else in the future.

这篇关于为什么从切换到关闭&amp;时SwitchPreference不显示动画?反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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