如何在Android中的首选项中添加涟漪效应? [英] How to add ripple effect to preferences in android?
问题描述
我正在尝试在触摸(选择)偏好设置时添加波纹效果.我通过扩展 ListPreference
自定义了我的偏好.我尝试使用 RippleDrawable
以编程方式设置涟漪效果,但看不到动画.
I am working on adding ripple effect when the preference is touched (selected). I have customized my preference by extending the ListPreference
. I have tried to set the ripple effect programmatically by using RippleDrawable
but I don't see the animation.
这是我自定义的偏好设置类
Here is my customized preference class
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>
我的涟漪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>
我是否将动画设置为正确的视图?任何想法表示赞赏.谢谢.
Am I setting the animation for the correct view? Any ideas are appreciated. Thanks.
推荐答案
这是向扩展 ListPreference
的类中添加自定义波纹效果的最小完整示例.我刚刚使用API 21(5.0)进行了测试.
This is a minimal complete example for adding a custom ripple effect to a class that extends ListPreference
. I just made and tested this with 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>
CustomListPreference
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中所指定:
When pressed, it shows a light blue ripple effect, as specified in the xml:
我根据您的代码以及android SDK示例中的示例SettingsActivity的代码构建了这个示例.
I built this example based on your code, and code from the example SettingsActivity in the android SDK samples.
经过一段时间的聊天并尝试了各种方法之后,我们得出的结论是问题是由OP的手机(三星S5)或其设置引起的.当OP在仿真器中尝试代码时,它们都可以正常工作.
After some time in chat and trying various things, we have come to a conclusion that the problem is caused by OP's phone (Samsung S5) or it's settings. When OP tried the code in the emulator, it all worked properly.
供参考-这是OP手机中的外观:
For reference - this is how it looked in OPs phone:
这篇关于如何在Android中的首选项中添加涟漪效应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!