<分区>
我正在使用 PreferenceFragment 来扩充具有单个 SwitchPreference 的 xml 文件。如何使该首选项的背景颜色(包括 SwitchPreference 的标题)与下图匹配。我试过设置背景,但我只能设置开关图标的背景颜色。
<分区>
我正在使用 PreferenceFragment 来扩充具有单个 SwitchPreference 的 xml 文件。如何使该首选项的背景颜色(包括 SwitchPreference 的标题)与下图匹配。我试过设置背景,但我只能设置开关图标的背景颜色。
最佳答案
styles.xml(在values文件夹下的style.xml文件中制作这个样式)
<resources>
<style name="SwitchTheme" parent="Theme.AppCompat.Light">
<!-- switch on thumb & track color -->
<item name="colorControlActivated">#02c754</item>
<!-- switch off thumb color -->
<item name="colorSwitchThumbNormal">#f1f1f1</item>
<!-- switch off track color -->
<item name="android:colorForeground">#42221f1f</item>
</style>
</resources>
你的布局 Activity .xml
<android.support.v7.widget.SwitchCompat
android:id="@+id/switch_on_off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:checked="true"
android:gravity="center"
android:paddingLeft="30dp"
android:theme="@style/SwitchTheme"
app:switchMinWidth="55dp"/>
这个资源对我有用。试试这个你就会明白
关于java - 如何为 Android SwitchPreference 添加背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46586134/