Android 如何在安卓系统中为偏好添加连锁反应?

Android 如何在安卓系统中为偏好添加连锁反应?,android,android-preferences,listpreference,rippledrawable,Android,Android Preferences,Listpreference,Rippledrawable,我正在努力增加涟漪效应时,偏好是触摸(选定)。我通过扩展列表首选项自定义了我的首选项。我曾尝试使用RippleDrawable以编程方式设置涟漪效果,但没有看到动画 这是我定制的偏好类 public class CustomListPreference extends ListPreference { public CustomListPreference(Context context, AttributeSet attrs) { super(cont

我正在努力增加涟漪效应时,偏好是触摸(选定)。我通过扩展
列表首选项
自定义了我的首选项。我曾尝试使用
RippleDrawable
以编程方式设置涟漪效果,但没有看到动画

这是我定制的偏好类

public class CustomListPreference extends ListPreference {

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

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

        @Override
        protected void onBindView(View view) {
            super.onBindView(view);
            setCustomStyle(view);
        }

        private void setCustomStyle(View view) {
            TextView titleView = (TextView) view.findViewById(android.R.id.title);
            titleView.setTypeface(InitActivity.TYPEFACE_REGULAR);
            TextView summary = (TextView) view.findViewById(android.R.id.summary);
            summary.setTypeface(InitActivity.TYPEFACE_REGULAR);

            //Setting the drawable here, but it doesn't work.        
            RippleDrawable drawable = (RippleDrawable) getContext().getResources().getDrawable(R.drawable.my_ripple_background);
            view.setBackGround(drawable);
        }

} 
我的首选项布局

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- opens a subscreen of settings -->
    <com.abc.app.CustomListPreference
            android:defaultValue="1"
            android:entries="@array/sampleEntries"
            android:entryValues="@array/SampleEntryValues"
            android:key="some_preference"
            android:title="@string/some_preferences" />

    <com.abc.app.CustomCheckboxPreference
           android... />


</PreferenceScreen>

我的ripple xml

<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/light_black_overlay"> <!--#22000000-->
    <item>
        <shape
            android:shape="rectangle">
            <solid android:color="@android:color/background_light" />
        </shape>
    </item>
</ripple>


我是否为正确的视图设置动画?欢迎提出任何意见。谢谢。

这是一个向扩展了
列表首选项的类添加自定义连锁反应的最小完整示例。我刚刚用API21(5.0)制作并测试了它

设置活动(启动活动)

pref_general.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <CheckBoxPreference
        android:defaultValue="true"
        android:key="example_checkbox"
        android:summary="a checkbox"
        android:title="Checkbox test" />

    <!-- replace with com.abc.app.CustomListPreference in your case-->
    <com.timcastelijns.rippletest.CustomListPreference
        android:defaultValue="1"
        android:entries="@array/sampleEntries"
        android:entryValues="@array/SampleEntryValues"
        android:key="some_preference"
        android:title="test" />

</PreferenceScreen>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="sampleEntries">
        <item>1</item>
        <item>2</item>
        <item>3</item>
    </string-array>

    <string-array name="SampleEntryValues">
        <item>4</item>
        <item>5</item>
        <item>6</item>
    </string-array>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@android:color/holo_blue_light">
    <item android:id="@android:id/mask">
        <color android:color="@android:color/white" />
    </item>
</ripple>
my\u ripple\u background.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <CheckBoxPreference
        android:defaultValue="true"
        android:key="example_checkbox"
        android:summary="a checkbox"
        android:title="Checkbox test" />

    <!-- replace with com.abc.app.CustomListPreference in your case-->
    <com.timcastelijns.rippletest.CustomListPreference
        android:defaultValue="1"
        android:entries="@array/sampleEntries"
        android:entryValues="@array/SampleEntryValues"
        android:key="some_preference"
        android:title="test" />

</PreferenceScreen>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="sampleEntries">
        <item>1</item>
        <item>2</item>
        <item>3</item>
    </string-array>

    <string-array name="SampleEntryValues">
        <item>4</item>
        <item>5</item>
        <item>6</item>
    </string-array>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@android:color/holo_blue_light">
    <item android:id="@android:id/mask">
        <color android:color="@android:color/white" />
    </item>
</ripple>

按下时,它会显示浅蓝色涟漪效果,如xml中所指定:


我根据您的代码以及android SDK示例中示例SettingsActivity中的代码构建了这个示例


编辑:
经过一段时间的聊天和尝试各种事情后,我们得出结论,问题是由OP的手机(三星S5)或其设置引起的。当OP在模拟器中尝试代码时,所有代码都正常工作

供参考-这是它在OPs phone中的外观:


我不小心在代码中添加了.xml文件扩展名。我想我执行的代码没有文件扩展名。不过我会尝试其他代码片段。尝试了你的代码,我可以在触摸屏上看到蓝色的背景,但它不像涟漪。涟漪应该从触摸的角度扩展到侧面。@prudhvi但对我来说确实如此。您使用的是什么sdk?你能再加上一些代码让我检查一下吗?特别是你的ripple xml我在其他活动(不是首选项)中使用了我的ripple xml,它运行良好,所以我认为它没有问题。我使用的是MinsdkVersion18和TargetsdkVersion21。我只在drawable-v21文件夹中包含ripple xml。@prudhvi和您的手机兼容sdk 21是吗?您能在使用ripple的地方包含活动代码吗?