为什么从切换到关闭&时SwitchPreference不显示动画?反之亦然? [英] Why SwitchPreference is not showing animation when switching from on to off & vice versa?
问题描述
我为应用程序的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
对象缺少动画:
-
,如果
SwitchPreference
是设置活动中的第一个Preference
.
if the
SwitchPreference
is the very firstPreference
in the settings activity.
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
对象的Activity
或Fragment
中.
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.
这篇关于为什么从切换到关闭&时SwitchPreference不显示动画?反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!