如何为Android中的SwitchPreference
窗口小部件绘制的自定义样式或其他背景选择器?
(注意:不是常规的Switch
小部件,我的意思是PreferenceActivity
/PreferenceFragment
中使用的Standart SwitchPreference
小部件)
您必须为开关本身创建一个自定义布局,并且可以动态地应用它。
preference.setWidgetLayoutResource(R.layout.custom_switch);
,但是我会详细介绍并确切地向您展示如何实现这一目标。
因此,您在XML文件中定义了您的偏好
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="YOUR_CATEGORY_TITLE" >
<SwitchPreference
android:key="SWITCH"
android:title="YOUR_TITLE_FOR_SWITCH" />
</PreferenceCategory>
</PreferenceScreen>
然后在您的peferenceActivty类中的onCreate()方法中读取它:
SwitchPreference pref = (SwitchPreference) findPreference(getString(R.string.SWITCH));
//pref.setChecked(true); // You can check it already if needed to true or false or a value you have stored persistently
pref.setWidgetLayoutResource(R.layout.custom_switch); // THIS IS THE KEY OF ALL THIS. HERE YOU SET A CUSTOM LAYOUT FOR THE WIDGET
pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// Here you can enable/disable whatever you need to
return true;
}
});
custom_switch 布局看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_switch_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="@android:color/white"
android:textIsSelectable="false"
android:textSize="18sp"
android:textStyle="bold"
android:track="@drawable/switch_track"
android:thumb="@drawable/switch_thumb"/>
对于开关,您将拥有2个选择器,用于 track 和 thumb properties。这些选择器的抽签物可以用Android Holo Color Generator生成,这是由Tasomaniac建议的。在这种情况下,您所要做的就是复制生成的可绘制文件夹的内容(仅适用于可绘制的hdpi,可绘制的mdpi,可绘制的-xhdpi,drawable-xxhdpi)。但是您可以为所需的每个州创建自定义视图。
这是这些选择器的外观: switch_track:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/switch_bg_focused" android:state_focused="true"/>
<item android:drawable="@drawable/switch_bg"/>
</selector>
switch_thumb:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/switch_thumb_disabled" android:state_enabled="false"/>
<item android:drawable="@drawable/switch_thumb_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/switch_thumb_activated" android:state_checked="true"/>
<item android:drawable="@drawable/switch_thumb"/>
</selector>
就是这样。这个解决方案帮助了我。如果我省略了什么,请告诉我,我会纠正问题。
您可以使用以下网站为您的开关生成样式。http://android-holo-colors.com/
然后,您可以使用以下库来自定义常规开关的实现。这些库还包括SwitchPreference替代方案。
https://github.com/bod/android-switch-backport
https://github.com/ankri/switchcompatlibrary
做到这一点的一种方法是子类别转换并覆盖OnBindView方法。这样,您仍需要在该方法中调用super.onbindview(view),然后在孩子视图中找到开关并适当地样式:
:package com.example;
import android.annotation.SuppressLint;
import android.content.Context;
import android.preference.SwitchPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import com.example.R;
public class CustomSwitchPreference extends SwitchPreference {
@SuppressLint("NewApi")
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSwitchPreference(Context context) {
super(context);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
Switch theSwitch = findSwitchInChildviews((ViewGroup) view);
if (theSwitch!=null) {
//do styling here
theSwitch.setThumbResource(R.drawable.new_thumb_resource);
}
}
private Switch findSwitchInChildviews(ViewGroup view) {
for (int i=0;i<view.getChildCount();i++) {
View thisChildview = view.getChildAt(i);
if (thisChildview instanceof Switch) {
return (Switch)thisChildview;
}
else if (thisChildview instanceof ViewGroup) {
Switch theSwitch = findSwitchInChildviews((ViewGroup) thisChildview);
if (theSwitch!=null) return theSwitch;
}
}
return null;
}
}
在您的style.xml文件中创建一个样式,然后给予widget.appcompat.compoundbutton.switch parent。
<style name="theme_switch_compat" parent="Widget.AppCompat.CompoundButton.Switch">
<item name="colorAccent">@color/YourColorAccent</item>
</style>
然后,您可以使用下面的链接来完成您的主题
如何更改switchcompat的轨道颜色