这是一个最小的完整示例,用于向扩展的类添加自定义波纹效果ListPreference
。我刚刚使用 API 21 (5.0) 制作并测试了它。
设置活动(启动活动)
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>
数组.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 中指定的:
我根据您的代码以及 android SDK 示例中的示例 SettingsActivity 的代码构建了此示例。
Edit:
经过一段时间的聊天和尝试各种方法后,我们得出的结论是,问题是由 OP 的手机(三星 S5)或其设置引起的。当OP在模拟器中尝试代码时,一切正常。
作为参考 - 这是它在 OP 手机中的样子: