android switchpreference 怎么切换,Android中的自定义SwitchPreference

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

preference.setWidgetLayoutResource(R.layout.custom_switch);

但我会详细介绍如何实现这一目标。

因此,您可以在prefers.xml等xml文件中定义首选项

然后在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布局如下所示:

对于开关,您将有2个选择器用于轨道和拇指属性。 这些选择器的drawable可以使用Android Holo Color Generator生成,这是由tasomaniac建议的。 在这种情况下,您所要做的就是复制生成的可绘制文件夹的内容(仅适用于drawable-hdpi,drawable-mdpi,drawable-xhdpi,drawable-xxhdpi)。 但您可以为所需的每个州创建自定义视图。

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

switch_thumb:

这就是它。 这个解决方案帮助了我。 如果我省略了什么,请告诉我,我会纠正这些问题。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在XML布局文件,可以使用PreferenceScreen标签来创建一个PreferenceScreen。然后在PreferenceScreen添加SwitchPreference,使用android:widgetLayout属性来设置自定义布局。 下面是一个示例代码: ```xml <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <SwitchPreference android:key="example_switch" android:title="Example Switch" android:summary="This is an example switch preference" android:widgetLayout="@layout/custom_switch_preference" /> </PreferenceScreen> ``` 在这个示例,我们创建了一个PreferenceScreen,其包含一个SwitchPreference。我们使用了android:widgetLayout属性来设置SwitchPreference的自定义布局为@layout/custom_switch_preference。 接下来,在res/layout文件夹下创建一个名为custom_switch_preference.xml的布局文件,用于定义SwitchPreference的自定义布局: ```xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@android:id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginStart="16dp" android:contentDescription="@null" android:src="@drawable/ic_launcher"/> <TextView android:id="@android:id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="72dp" android:layout_marginTop="16dp" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="?android:attr/textColorPrimary" android:text="Example Switch"/> <Switch android:id="@android:id/switch_widget" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_centerVertical="true" android:layout_marginEnd="16dp"/> </RelativeLayout> ``` 在这个布局文件,我们使用了一个相对布局,其包含了一个ImageView、一个TextView和一个Switch。我们使用android:id属性来设置它们的id,以便在Java代码引用它们。 最后,在Java代码,我们可以使用SharedPreferences类来读取和写入SwitchPreference的值。例如,以下代码演示了如何读取SwitchPreference的值: ```java SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); boolean isChecked = sharedPreferences.getBoolean("example_switch", false); ``` 其,我们使用getDefaultSharedPreferences()方法获取默认的SharedPreferences实例,并使用getBoolean()方法来获取SwitchPreference的值。第二个参数是默认值,如果SwitchPreference的值不存在,则使用该默认值。 如果要写入SwitchPreference的值,可以使用SharedPreferences.Editor类。例如,以下代码演示了如何将SwitchPreference的值写入SharedPreferences: ```java SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean("example_switch", true); editor.apply(); ``` 其,我们使用edit()方法获取SharedPreferences.Editor实例,并使用putBoolean()方法将SwitchPreference的值写入SharedPreferences。最后,我们使用apply()方法提交更改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值