14

如何为SwitchPreferenceAndroid 中的小部件设置自定义样式或其他可绘制的背景选择器?

(注意:不是常规Switch小部件,我指的是/SwitchPreference中使用的标准小部件)PreferenceActivityPreferenceFragment

4

4 回答 4

18

您必须为开关本身创建自定义布局,并且可以动态应用它。

preference.setWidgetLayoutResource(R.layout.custom_switch);

但我将详细介绍并向您展示如何实现这一目标。

因此,您可以在 xml 文件中定义您的偏好,例如options.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>

然后在 PreferenceActivty 类中的 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 个用于轨道拇指属性的选择器。这些选择器的可绘制对象可以使用tasomaniac建议的 Android Holo 颜色生成器生成。在这种情况下,您所要做的就是复制生成的可绘制文件夹的内容(仅适用于drawable-hdpi、drawable-mdpi、drawable-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>

开关拇指:

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

差不多就是这样。这个解决方案帮助了我。如果我遗漏了什么,请告诉我,我会纠正这些问题。

于 2014-02-18T12:56:57.607 回答
3

您可以使用以下网站为您的 Switch 生成样式。 http://android-holo-colors.com/

然后您可以使用以下库来自定义常规 Switch 的实现。这些库还包括 SwitchPreference 替代方案。

https://github.com/BoD/android-switch-backport

https://github.com/ankri/SwitchCompatLibrary

于 2014-01-20T15:55:51.567 回答
2

一种方法是继承 SwitchPreference 并覆盖 onBindView 方法。这样做时,您仍然希望在该方法中调用 super.onBindView(view),然后在子视图中找到 Switch 并根据需要设置其样式:

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;
    }
}
于 2015-05-21T19:54:09.417 回答
0

在您的 style.xml 文件中创建一个样式并将其指定为 Widget.AppCompat.CompoundButton.Switch 父级。

<style name="theme_switch_compat" parent="Widget.AppCompat.CompoundButton.Switch">

    <item name="colorAccent">@color/YourColorAccent</item>

</style>

然后您可以使用下面的链接来完成您的主题

如何更改 SwitchCompat 的轨道颜色

于 2021-04-22T12:35:59.877 回答