2011年10月4日火曜日

PreferenceActivity の CheckBox マークを変える

PreferenceActivity の各UI は Android のフレームワークの theme.xml に定義されています。

/frameworks/base/core/res/res/values/themes.xml
  1. 194         <!-- Preference styles -->  
  2. 195         <item name="preferenceScreenStyle">@android:style/Preference.PreferenceScreen</item>  
  3. 196         <item name="preferenceCategoryStyle">@android:style/Preference.Category</item>  
  4. 197         <item name="preferenceStyle">@android:style/Preference</item>  
  5. 198         <item name="preferenceInformationStyle">@android:style/Preference.Information</item>  
  6. 199         <item name="checkBoxPreferenceStyle">@android:style/Preference.CheckBoxPreference</item>  
  7. 200         <item name="yesNoPreferenceStyle">@android:style/Preference.DialogPreference.YesNoPreference</item>  
  8. 201         <item name="dialogPreferenceStyle">@android:style/Preference.DialogPreference</item>  
  9. 202         <item name="editTextPreferenceStyle">@android:style/Preference.DialogPreference.EditTextPreference</item>  
  10. 203         <item name="ringtonePreferenceStyle">@android:style/Preference.RingtonePreference</item>  
  11. 204         <item name="preferenceLayoutChild">@android:layout/preference_child</item>  

ここをそれぞれオリジナルテーマに置き換えればOK

もともとはこうなってる

/frameworks/base/core/res/res/values/styles.xml
  1. 769     <!-- Preference Styles -->  
  2. 770   
  3. 771     <style name="Preference">  
  4. 772         <item name="android:layout">@android:layout/preference</item>  
  5. 773     </style>  
  6. 774       
  7. 775     <style name="Preference.Information">  
  8. 776         <item name="android:layout">@android:layout/preference_information</item>  
  9. 777         <item name="android:enabled">false</item>  
  10. 778         <item name="android:shouldDisableView">false</item>  
  11. 779     </style>  
  12. 780       
  13. 781     <style name="Preference.Category">  
  14. 782         <item name="android:layout">@android:layout/preference_category</item>  
  15. 783         <!-- The title should not dim if the category is disabled, instead only the preference children should dim. -->  
  16. 784         <item name="android:shouldDisableView">false</item>  
  17. 785         <item name="android:selectable">false</item>  
  18. 786     </style>  
  19. 787       
  20. 788     <style name="Preference.CheckBoxPreference">  
  21. 789         <item name="android:widgetLayout">@android:layout/preference_widget_checkbox</item>  
  22. 790     </style>  
  23. 791       
  24. 792     <style name="Preference.PreferenceScreen">  
  25. 793     </style>  
  26. 794   
  27. 795     <style name="Preference.DialogPreference">  
  28. 796         <item name="android:positiveButtonText">@android:string/ok</item>  
  29. 797         <item name="android:negativeButtonText">@android:string/cancel</item>  
  30. 798         <item name="android:widgetLayout">@android:layout/preference_dialog</item>  
  31. 799     </style>  
  32. 800       
  33. 801     <style name="Preference.DialogPreference.YesNoPreference">  
  34. 802         <item name="android:positiveButtonText">@android:string/yes</item>  
  35. 803         <item name="android:negativeButtonText">@android:string/no</item>  
  36. 804     </style>  
  37. 805       
  38. 806     <style name="Preference.DialogPreference.EditTextPreference">  
  39. 807         <item name="android:dialogLayout">@android:layout/preference_dialog_edittext</item>  
  40. 808     </style>  
  41. 809       
  42. 810     <style name="Preference.RingtonePreference">  
  43. 811         <item name="android:ringtoneType">ringtone</item>  
  44. 812         <item name="android:showSilent">true</item>  
  45. 813         <item name="android:showDefault">true</item>  
  46. 814         <item name="android:widgetLayout">@android:layout/preference_dialog</item>  
  47. 815     </style>  

これを置き換え。

android:widgetLayout というのが左側に配置される部品(チェックボックスとか、○に下三角のやつ)

結論。CheckBoxPreference のチェックボックスと EditTextPreference の下三角は変更できる。
ただし、EditTextPreference のダイアログのレイアウトは変更できたけど、レイアウトファイル内の @+android:id/edittext_container が public ではないため、入力ボックスがでなくなっちゃうのでダメだった。



PreferenceDialog を継承したクラスを作ったほうが早そう。

res/xml/prefs.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <PreferenceCategory android:title="Setting">  
  4.         <CheckBoxPreference   
  5.             android:key="checkbox_value"  
  6.             android:title="Setting1"   
  7.             android:summary="sample of checkbox setting" />  
  8.         <EditTextPreference   
  9.             android:key="edittext_value"  
  10.             android:title="Setting2"   
  11.             android:summary="sample of edittext setting"  
  12.             android:dialogTitle="Setting edittext"  
  13.             android:dialogMessage="please set values" />  
  14.     </PreferenceCategory>  
  15. </PreferenceScreen>  
res/values/styles.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="CustomTheme" parent="@android:style/Theme.Light">  
  4.         <item name="android:windowBackground">@color/window_bg</item>  
  5.         <item name="android:colorBackground">@color/window_bg</item>  
  6.   
  7.         <item name="android:checkBoxPreferenceStyle">@style/CustomCheckBoxPreference</item>  
  8.         <item name="android:editTextPreferenceStyle">@style/CustomDialogPreference.CustomEditTextPreference  
  9.         </item>  
  10.     </style>  
  11.   
  12.     <style name="CustomCheckBoxPreference">  
  13.         <item name="android:widgetLayout">@layout/preference_widget_checkbox</item>  
  14.     </style>  
  15.   
  16.     <style name="CustomDialogPreference.CustomEditTextPreference">  
  17.         <item name="android:dialogLayout">@layout/preference_dialog_edittext</item>  
  18.     </style>  
  19.   
  20.     <style name="CustomDialogPreference">  
  21.         <item name="android:positiveButtonText">@android:string/ok</item>  
  22.         <item name="android:negativeButtonText">@android:string/cancel</item>  
  23.         <item name="android:widgetLayout">@layout/preference_dialog</item>  
  24.     </style>  
  25.   
  26.     <style name="CustomDialogPreference.YesNoPreference">  
  27.         <item name="android:positiveButtonText">@android:string/yes</item>  
  28.         <item name="android:negativeButtonText">@android:string/no</item>  
  29.     </style>  
  30. </resources>  
res/layout/preference_widget_checkbox.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <CheckBox xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+android:id/checkbox"   
  4.     android:layout_width="wrap_content"  
  5.     android:layout_height="wrap_content"  
  6.     android:layout_marginRight="4dip"  
  7.     android:layout_gravity="center_vertical"  
  8.     android:button="@drawable/btn_check"  
  9.     android:focusable="false"  
  10.     android:clickable="false" />  
res/layoutpreference_dialog.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ImageView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.      android:layout_width="wrap_content"  
  4.      android:layout_height="wrap_content"  
  5.      android:layout_marginRight="6dip"  
  6.      android:layout_marginTop="@dimen/margin_check"  
  7.      android:layout_marginLeft="@dimen/margin_check"  
  8.      android:layout_marginBottom="@dimen/margin_check"  
  9.      android:layout_gravity="center_vertical"  
  10.      android:background="@drawable/btn_circle"  
  11.      android:src="@drawable/ic_btn_round_more"  
  12.      android:scaleType="center"  
  13.       />  
res/layoutpreference_dialog_edittext.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.   
  6.     <LinearLayout  
  7.         android:id="@*android:id/edittext_container"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:padding="5dip"  
  11.         android:orientation="vertical">  
  12.   
  13.         <TextView   
  14.             android:id="@android:id/message"  
  15.             style="?android:attr/textAppearanceMedium"  
  16.             android:layout_width="fill_parent"  
  17.             android:layout_height="wrap_content"  
  18.             android:textColor="@color/color2" />  
  19.               
  20.     </LinearLayout>  
  21.   
  22. </ScrollView>  
res/values/dimens.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <dimen name="size_check">32dip</dimen>  
  4.     <dimen name="size_more">32dip</dimen>  
  5.     <dimen name="size_radio">32dip</dimen>  
  6.     <dimen name="margin_check">3dip</dimen>  
  7.     <dimen name="size_circle">14dip</dimen>  
  8. </resources>  
res/values/colors.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <color name="color1">#FDF3DE</color>  
  4.     <color name="color2">#9C0C0A</color>  
  5.     <color name="color3">#E88833</color>  
  6.     <color name="color4">#8F6031</color>  
  7.     <color name="color5">#3B3013</color>  
  8.     <color name="color6">#690806</color>  
  9.     <color name="color7">#dfd3d0</color>  
  10.   
  11.     <color name="window_bg">@color/color1</color>  
  12.   
  13.     <color name="component_frame_normal">@color/color3</color>  
  14.     <color name="component_frame_pressed">@color/color2</color>  
  15.     <color name="component_frame_pressed_top">@color/color2</color>  
  16.     <color name="component_frame_pressed_bottom">@color/color6</color>  
  17.     <color name="component_frame_disabled">@color/color7</color>  
  18.     <color name="component_text_normal">@color/color6</color>  
  19.     <color name="component_text_pressed">@color/color1</color>  
  20.     <color name="component_button_top">#fff</color>  
  21.     <color name="component_button_bottom">@color/color7</color>  
  22. </resources>  
res/drawable/btn_check_bg.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <padding android:left="38dip" android:right="0dip" />  
  4.     <solid android:color="#0000" />  
  5. </shape>  
res/drawable/btn_bg.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_checked="true" android:state_pressed="true"  
  4.         android:state_enabled="true">  
  5.         <layer-list>  
  6.             <item android:top="@dimen/margin_check" android:bottom="@dimen/margin_check"  
  7.                 android:left="@dimen/margin_check" android:right="@dimen/margin_check">  
  8.                 <shape android:shape="rectangle">  
  9.                     <corners android:radius="5dip" />  
  10.                     <gradient android:angle="270"  
  11.                         android:startColor="@color/component_frame_pressed_top"  
  12.                         android:endColor="@color/component_frame_pressed_bottom" />  
  13.                     <size android:width="@dimen/size_check"  
  14.                         android:height="@dimen/size_check" />  
  15.                     <stroke android:width="2dip"  
  16.                         android:color="@color/component_frame_pressed" />  
  17.                 </shape>  
  18.             </item>  
  19.             <item android:top="10dip" android:bottom="10dip"  
  20.                 android:left="8dip" android:right="8dip"  
  21.                 android:drawable="@drawable/check_on" />  
  22.         </layer-list>  
  23.     </item>  
  24.   
  25.     <item android:state_checked="false" android:state_pressed="true"  
  26.         android:state_enabled="true">  
  27.         <layer-list>  
  28.             <item android:top="@dimen/margin_check" android:bottom="@dimen/margin_check"  
  29.                 android:left="@dimen/margin_check" android:right="@dimen/margin_check">  
  30.                 <shape android:shape="rectangle">  
  31.                     <corners android:radius="5dip" />  
  32.                     <gradient android:angle="270"  
  33.                         android:startColor="@color/component_frame_pressed_top"  
  34.                         android:endColor="@color/component_frame_pressed_bottom" />  
  35.                     <size android:width="@dimen/size_check"  
  36.                         android:height="@dimen/size_check" />  
  37.                     <stroke android:width="2dip"  
  38.                         android:color="@color/component_frame_pressed" />  
  39.                 </shape>  
  40.             </item>  
  41.             <item android:top="10dip" android:bottom="10dip"  
  42.                 android:left="8dip" android:right="8dip"  
  43.                 android:drawable="@drawable/check_off" />  
  44.         </layer-list>  
  45.     </item>  
  46.   
  47.     <item android:state_checked="true" android:state_focused="true"  
  48.         android:state_enabled="true">  
  49.         <layer-list>  
  50.             <item android:top="@dimen/margin_check" android:bottom="@dimen/margin_check"  
  51.                 android:left="@dimen/margin_check" android:right="@dimen/margin_check">  
  52.                 <shape android:shape="rectangle">  
  53.                     <corners android:radius="5dip" />  
  54.                     <gradient android:angle="90"  
  55.                         android:startColor="#ffffff" android:endColor="#fefefe" />  
  56.                     <size android:width="@dimen/size_check"  
  57.                         android:height="@dimen/size_check" />  
  58.                     <stroke android:width="2dip"  
  59.                         android:color="@color/component_frame_pressed" />  
  60.                 </shape>  
  61.             </item>  
  62.             <item android:top="10dip" android:bottom="10dip"  
  63.                 android:left="8dip" android:right="8dip"  
  64.                 android:drawable="@drawable/check_on" />  
  65.   
  66.         </layer-list>  
  67.     </item>  
  68.   
  69.     <item android:state_checked="false" android:state_focused="true"  
  70.         android:state_enabled="true">  
  71.         <layer-list>  
  72.             <item android:top="@dimen/margin_check" android:bottom="@dimen/margin_check"  
  73.                 android:left="@dimen/margin_check" android:right="@dimen/margin_check">  
  74.                 <shape android:shape="rectangle">  
  75.                     <corners android:radius="5dip" />  
  76.                     <gradient android:angle="90"  
  77.                         android:startColor="#ffffff" android:endColor="#fefefe" />  
  78.                     <size android:width="@dimen/size_check"  
  79.                         android:height="@dimen/size_check" />  
  80.                     <stroke android:width="2dip"  
  81.                         android:color="@color/component_frame_pressed" />  
  82.                 </shape>  
  83.             </item>  
  84.             <item android:top="10dip" android:bottom="10dip"  
  85.                 android:left="8dip" android:right="8dip"  
  86.                 android:drawable="@drawable/check_off" />  
  87.         </layer-list>  
  88.     </item>  
  89.   
  90.     <item android:state_checked="false" android:state_enabled="true">  
  91.         <layer-list>  
  92.             <item android:top="@dimen/margin_check" android:bottom="@dimen/margin_check"  
  93.                 android:left="@dimen/margin_check" android:right="@dimen/margin_check">  
  94.                 <shape android:shape="rectangle">  
  95.                     <corners android:radius="5dip" />  
  96.                     <gradient android:angle="90"  
  97.                         android:startColor="#ffffff" android:endColor="#fefefe" />  
  98.                     <size android:width="@dimen/size_check"  
  99.                         android:height="@dimen/size_check" />  
  100.                     <stroke android:width="2dip"  
  101.                         android:color="@color/component_frame_normal" />  
  102.                 </shape>  
  103.   
  104.             </item>  
  105.             <item android:top="10dip" android:bottom="10dip"  
  106.                 android:left="8dip" android:right="8dip"  
  107.                 android:drawable="@drawable/check_off" />  
  108.         </layer-list>  
  109.     </item>  
  110.   
  111.     <item android:state_checked="true" android:state_enabled="true">  
  112.         <layer-list>  
  113.             <item android:top="@dimen/margin_check" android:bottom="@dimen/margin_check"  
  114.                 android:left="@dimen/margin_check" android:right="@dimen/margin_check">  
  115.                 <shape android:shape="rectangle">  
  116.                     <corners android:radius="5dip" />  
  117.                     <gradient android:angle="90"  
  118.                         android:startColor="#ffffff" android:endColor="#fefefe" />  
  119.                     <size android:width="@dimen/size_check"  
  120.                         android:height="@dimen/size_check" />  
  121.                     <stroke android:width="2dip"  
  122.                         android:color="@color/component_frame_normal" />  
  123.                 </shape>  
  124.             </item>  
  125.             <item android:top="10dip" android:bottom="10dip"  
  126.                 android:left="8dip" android:right="8dip"  
  127.                 android:drawable="@drawable/check_on" />  
  128.         </layer-list>  
  129.     </item>  
  130.   
  131.     <!-- Disabled states -->  
  132.     <item>  
  133.         <layer-list>  
  134.             <item android:top="@dimen/margin_check" android:bottom="@dimen/margin_check"  
  135.                 android:left="@dimen/margin_check" android:right="@dimen/margin_check">  
  136.                 <shape android:shape="rectangle">  
  137.                     <corners android:radius="5dip" />  
  138.                     <gradient android:angle="90"  
  139.                         android:startColor="#ffffff" android:endColor="#fefefe" />  
  140.                     <size android:width="@dimen/size_check"  
  141.                         android:height="@dimen/size_check" />  
  142.                     <stroke android:width="2dip"  
  143.                         android:color="@color/component_frame_disabled" />  
  144.                 </shape>  
  145.             </item>  
  146.             <item android:top="10dip" android:bottom="10dip"  
  147.                 android:left="8dip" android:right="8dip"  
  148.                 android:drawable="@drawable/check_off" />  
  149.         </layer-list>  
  150.     </item>  
  151. </selector>  
res/drawable/btn_circle.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_enabled="true">  
  4.         <shape android:shape="ring" android:useLevel="false"  
  5.             android:innerRadius="0dip" android:thickness="@dimen/size_circle">  
  6.             <size android:width="@dimen/size_check" android:height="@dimen/size_check" />  
  7.             <stroke android:width="2dip"  
  8.                 android:color="@color/component_frame_normal" />  
  9.             <solid android:color="#fff" />  
  10.         </shape>  
  11.     </item>  
  12.     <item>  
  13.         <shape android:shape="ring" android:useLevel="false"  
  14.             android:innerRadius="0dip" android:thickness="@dimen/size_circle">  
  15.             <size android:width="@dimen/size_check" android:height="@dimen/size_check" />  
  16.             <stroke android:width="2dip"  
  17.                 android:color="@color/component_frame_disabled" />  
  18.             <solid android:color="#fff" />  
  19.         </shape>  
  20.     </item>  
  21. </selector>  
ic_btn_round_more.xml は SDK プラットフォームのリソースのものを利用。

0 件のコメント:

コメントを投稿