1

私はPreferenceFragmentを使用して、単一のSwitchPreferenceでxmlファイルを拡張しています。 SwitchPreferenceのタイトルを含め、その環境設定の背景色を下の画像と一致させるにはどうしたらいいですか?私は背景を設定しようとしましたが、スイッチアイコンの背景色のみを設定できます。Android SwitchPreferenceに背景色を追加するにはどうすればよいですか?

enter image description here

+0

https://stackoverflow.com/questions/21235829/custom-switchpreference-in-android/21854548 –

+0

私はテキスト全体の背景色 –

答えて

0

私はレイアウトにStyle.xml

<style name="SwitchTheme" parent="Theme.AppCompat.Light"> 
    <item name="colorControlActivated">@color/yourcolor</item> 
</style> 

<android.support.v7.widget.SwitchCompat 
    android:id="@+id/switch_on_off" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentRight="true" 
    android:layout_gravity="center" 
    android:checked="false" 
    android:gravity="center" 
    android:paddingLeft="@dimen/padding" 
    android:paddingRight="@dimen/padding" 
    app:switchMinWidth="@dimen/switch_size" 
    app:theme="@style/SwitchTheme" /> 
0

のstyles.xmlをスタイル

を使用してこれを行っている(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> 

your_layout_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"/> 

このソースは私のために働いています。これで試してみてください。

関連する問題