首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓系统中的自定义SwitchPreference

安卓系统中的自定义SwitchPreference
EN

Stack Overflow用户
提问于 2014-01-20 21:52:23
回答 4查看 18.4K关注 0票数 14

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

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

EN

回答 4

Stack Overflow用户

发布于 2014-02-18 20:56:58

您必须为交换机本身创建自定义布局,并且可以像这样动态应用它。

代码语言:javascript
复制
preference.setWidgetLayoutResource(R.layout.custom_switch);

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

因此,您可以在像preferences.xml这样的xml文件中定义您的首选项

代码语言:javascript
复制
<?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()方法中读取它:

代码语言:javascript
复制
    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布局如下所示:

代码语言:javascript
复制
<?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"/>

对于开关,您将有两个选择器,分别用于trackthumb属性。这些选择器的绘图可以使用tasomaniac建议的Android全息颜色生成器生成。在这种情况下,您需要做的就是复制生成的可绘制文件夹的内容(仅适用于drawable-hdpi、drawable-mdpi、drawable-xhdpi和drawable-xxhdpi)。但您可以为所需的每个状态创建自定义视图。

下面是这些选择器的外观:switch_track:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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>

差不多就是这样了。这个解决方案帮了我大忙。如果我遗漏了什么,请让我知道,我会纠正问题。

票数 18
EN

Stack Overflow用户

发布于 2014-01-20 23:55:52

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

然后,您可以使用下面的库来自定义常规开关的实现。这些库还包括SwitchPreference alternative。

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

https://github.com/ankri/SwitchCompatLibrary

票数 3
EN

Stack Overflow用户

发布于 2015-05-22 03:54:09

实现这一点的一种方法是将SwitchPreference子类化并覆盖onBindView方法。在执行此操作时,您仍希望在该方法中调用super.onBindView(视图),但随后在子视图中找到该开关并根据需要设置其样式:

代码语言:javascript
复制
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;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21235829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档