Nic*_*kel 2 android android-preferences preferencefragment
我正在尝试在我的顶部添加一些空间,该空间SwitchPreferenceCompat位于PreferenceFragmentCompat. 基本上我只需要在它和顶部之间留出一些空间Toolbar,无论是通过扩大它的高度还是使用填充间隙,而不添加会干扰Toolbar. 我相信我可以通过向 中添加自定义样式来实现这一点SwitchPreferenceCompat,但是我无法使其正常工作。
这是我尝试过的:
在我的styles.xml——
<style name="SwitchPreferenceNew" parent="Preference.SwitchPreferenceCompat.Material">
<item name="android:paddingTop">20dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)
在我的app_preferences.xml——
<android.support.v7.preference.SwitchPreferenceCompat
style="@style/SwitchPreferenceNew"
android:defaultValue="false"
android:icon="@drawable/ic_power_settings_new_black_48dp"
android:key="prefsActivate"
android:summary=""
android:title="Activate reminders" />
Run Code Online (Sandbox Code Playgroud)
我想我只是没有正确覆盖样式,但是我无法找到如何使用SwitchPreferenceCompat. 先感谢您!
我知道这已经晚了,但我从以下代码中得到了结果:
我的应用主题:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="preferenceTheme">@style/my_preference_theme</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我喜欢的主题:
<style name="my_preference_theme" parent="@style/PreferenceThemeOverlay">
<item name="switchPreferenceCompatStyle">@style/preference_switch_compat</item>
</style>
Run Code Online (Sandbox Code Playgroud)
在我的 PreferenceSwitchCompat 我使用了 layout 属性,你应该为你的视图创建新的布局
<style name="preference_switch_compat">
<item name="android:layout">@layout/switch_layout</item>
</style>
Run Code Online (Sandbox Code Playgroud)
在这里,您必须自定义您的视图:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SwitchCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/switchWidget"
android:background="#f00"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)
希望对某人有所帮助。