自定义开关preference Android中自定义、preference、Android

2023-09-05 23:31:08 作者:☀ 彼此安好互不扰 @

如何设置自定义样式或其他背景选择绘制的开关preference 小部件的Andr​​oid?

How to set a custom style or other background selector drawable for the SwitchPreference widget in Android?

(注意:不是正规开关控件,我指的是非标准开关preference 部件所用在 preferenceActivity / preferenceFragment

(Note: not the regular Switch widget, I mean the standart SwitchPreference widget that used in PreferenceActivity / PreferenceFragment)

推荐答案

您必须创建一个自定义布局为交换机本身,您可以将它动态地喜欢。

You have to create a custom layout for the switch itself and you can apply it dynamically like.

preference.setWidgetLayoutResource(R.layout.custom_switch);

不过,我会去到细节,告诉你到底如何实现这一点。

But I'll go into details and show you exactly how to achieve this.

所以,你在这样一个XML文件中定义的preference的的 preferences.xml 的

So, you define your preference in an xml file like preferences.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>

然后读它在OnCreate()方法的preferenceActivty类中:

Then read it in the onCreate() method inside your PreferenceActivty class:

    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个选择器在的跟踪的和的拇指的属性。 可随着Android霍洛颜色生成,其建议是由 tasomaniac 生成的可绘制了这些选择。在这种情况下,你所要做的,就是复制生成的绘制文件夹的内容(仅适用于可绘制,华电国际,绘制,MDPI,绘制-xhdpi,绘制-xxhdpi)。但是,您可以创建自定义视图,你需要每一个状态。

And for the switch you'll have 2 selectors for the track and thumb properties. The drawables for these selectors can be generated with the Android Holo Color Generator, which was suggested by tasomaniac. In this case, all you have to do, is to copy the content of the generated drawable folders(only for the drawable-hdpi, drawable-mdpi, drawable-xhdpi, drawable-xxhdpi). But you can create custom views for each state you need.

下面是如何将这些选择会是这样的: switch_track:

Here is how these selectors will look like: 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>

这就是pretty的是它。该解决方案为我排忧解难。如果我忽略的东西,请让我知道,我会纠正这些问题。

And that's pretty much it. This solution helped me out. If I omitted something, please let me know and I'll correct the issues.