Android中的自定义开关首选项
如何为Android中的Android中的自定义开关首选项,android,custom-widgets,Android,Custom Widgets,如何为Android中的SwitchPreference小部件设置自定义样式或其他可绘制的背景选择器 (注意:不是常规的Switch小部件,我指的是在PreferenceActivity/PreferenceFragment中使用的standartSwitchPreference小部件)您可以使用以下网站为您的交换机生成样式。 然后,您可以使用以下库来定制常规开关的实现。这些库还包括SwitchPreference选项 您必须为交换机本身创建一个自定义布局,并且可以像这样动态应用它 pref
SwitchPreference
小部件设置自定义样式或其他可绘制的背景选择器
(注意:不是常规的
Switch
小部件,我指的是在PreferenceActivity
/PreferenceFragment
中使用的standartSwitchPreference
小部件)您可以使用以下网站为您的交换机生成样式。
然后,您可以使用以下库来定制常规开关的实现。这些库还包括SwitchPreference选项
您必须为交换机本身创建一个自定义布局,并且可以像这样动态应用它
preference.setWidgetLayoutResource(R.layout.custom_switch);
但我将详细介绍并向您展示如何实现这一点
因此,您可以在xml文件中定义您的首选项,如preferences.xml
自定义_开关的布局如下所示:
<?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"/>
对于交换机,您将有两个选择器用于曲目和拇指属性。
这些选择器的绘图功能可以使用Android Holo Color Generator生成,这是由的建议。在这种情况下,您所要做的就是复制生成的可绘制文件夹的内容(仅适用于可绘制hdpi、可绘制mdpi、可绘制xhdpi、可绘制xxhdpi)。但是,您可以为需要的每个状态创建自定义视图
以下是这些选择器的外观:
切换轨道:
<?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>
<?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>
切换\u拇指:
<?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>
<?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>
差不多就是这样。这个解决方案帮助了我。如果我遗漏了什么,请告诉我,我会纠正这些问题。一种方法是将SwitchPreference子类化,并重写onBindView方法。这样做时,您仍然希望在该方法中调用super.onBindView(视图),但随后在子视图中找到开关,并根据需要设置其样式:
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;
}
}
package.com.example;
导入android.annotation.SuppressLint;
导入android.content.Context;
导入android.preference.SwitchPreference;
导入android.util.AttributeSet;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Switch;
导入com.example.R;
公共类CustomSwitchPreference扩展了SwitchPreference{
@SuppressLint(“新API”)
公共CustomSwitchPreference(上下文上下文、属性集属性、int-defStyleAttr、int-defStyleRes){
super(context、attrs、defStyleAttr、defStyleRes);
}
公共CustomSwitchPreference(上下文上下文、属性集属性、int-defStyleAttr){
super(上下文、attrs、defStyleAttr);
}
公共CustomSwitchPreference(上下文、属性集属性){
超级(上下文,attrs);
}
公共CustomSwitchPreference(上下文){
超级(上下文);
}
@凌驾
受保护的void onBindView(视图){
super.onBindView(视图);
切换开关=FindSwitchInChildView((视图组)视图);
如果(开关!=null){
//你在这里做造型吗
开关.setThumbResource(R.drawable.new\u thumb\u资源);
}
}
专用交换机FindSwitchInChildView(视图组视图){
对于(int i=0;i在style.xml文件中创建一个样式,并将其指定为Widget.AppCompat.CompoundButton.Switch父项
<style name="theme_switch_compat" parent="Widget.AppCompat.CompoundButton.Switch">
<item name="colorAccent">@color/YourColorAccent</item>
</style>
@颜色/你的颜色口音
然后你可以使用下面的链接来完成你的主题
解决方案的问题是,小部件似乎不代表SharedReference值的状态,即它永远不会根据sp进行检查/取消检查,setCheck似乎也不会更改UI。我认为这是我使用的Android版本(4.4.2)的问题我无法处理任何切换事件preference@siyb-共享首选项不允许通过setChecked等进行更改:)-如果要手动更改sp小部件,需要通过首选项管理器设置首选项,然后重新绘制标题或重新加载视图{通过invalidate或getPreferenceScreen().removeAll();addPreferencesFromResource(@IdRes int);}此解决方案不完整,因为它在SwitchPreference.java中搜索id为“com.android.internal.R.id.switchWidget”的小部件,该小部件未被SDK公开。因此,您需要复制原始的SwitchPreference.java并将该id更改为您在布局中提供的id。此解决方案对我有效。但仅适用于API21+: