如何在安卓的偏好设置中添加涟漪效果?

11

我正在尝试在偏好设置被触摸(选定)时添加涟漪效果。我通过扩展ListPreference来自定义我的偏好设置。我已经尝试使用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>

我是否为正确的视图设置了动画?感谢任何想法。谢谢。

1个回答

7
这是一个最小完整示例,用于将自定义波纹效果添加到扩展了ListPreference类的类中。我使用API 21(5.0)创建并测试了它。 SettingsActivity(启动活动)
public class SettingsActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.pref_general);
    }
}

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>

arrays.xml

<?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>

自定义列表偏好设置

public class CustomListPreference extends ListPreference {

    private Context ctx;

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

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

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

    private void setCustomStyle(View view) {
        RippleDrawable drawable = (RippleDrawable) ctx.getDrawable(R.drawable.my_ripple_background);
        view.setBackground(drawable);
    }
}

my_ripple_background.xml

<?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中所指定的。

enter image description here


我基于您的代码和来自Android SDK示例中的SettingsActivity构建了这个示例。
编辑:
经过一段时间的交流和尝试各种方法,我们得出结论,问题是由OP的手机(三星S5)或其设置引起的。当OP在模拟器中尝试代码时,一切都正常工作。

供参考-这是OP手机上的样子:

enter image description here


@prudhvi,但是对我来说它确实能够做到。你使用的是哪个SDK?你能否包含更多的代码,以便我可以检查一下?特别是你的ripple xml。 - Tim
是的,我的手机支持sdk 21。在涟漪效果起作用的那个活动中,我没有动态设置它。我只是在活动的xml中使用“_android:background="@drawable/ripple_background"_”。 - Prudhvi
@prudhvi 你是否有两个ripple文件?ripple_backgroundmy_ripple_background - Tim
@prudhvi,你能否建立一个新项目来测试并仅使用我提供的代码,看看是否有效?这里已经很晚了,我会在明天早上进行更多的研究 :-) - Tim
当然,我很感激你的帮助。 - Prudhvi
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接