以下は 4.0.3 のコードですが、この部分は 3.0 で Theme.Holo が追加されて以降特に変わっていません。
http://tools.oesf.biz/android-4.0.3_r1.0/xref/frameworks/base/core/res/res/values/themes.xml
- <style name="Theme">
- ...
- <item name="checkboxStyle">@android:style/Widget.CompoundButton.CheckBox</item>
- ...
- </style>
- ...
- <style name="Theme.Holo">
- ...
- <item name="checkboxStyle">@android:style/Widget.Holo.CompoundButton.CheckBox</item>
- ...
- </style>
- ...
- <style name="Theme.Holo">
- ...
- <item name="checkboxStyle">@android:style/Widget.Holo.CompoundButton.CheckBox</item>
- ...
- </style>
- ...
- <style name="Theme.Holo.Light" parent="Theme.Light">
- ...
- <item name="checkboxStyle">@android:style/Widget.Holo.Light.CompoundButton.CheckBox</item>
- ...
- </style>
Widget.Holo.CompoundButton.CheckBox と Widget.Holo.Light.CompoundButton.CheckBox を見ると、Widget.CompoundButton.CheckBox をそのまま継承しているだけです。
http://tools.oesf.biz/android-4.0.3_r1.0/xref/frameworks/base/core/res/res/values/styles.xml#1003
- <style name="Widget.Holo.CompoundButton.CheckBox" parent="Widget.CompoundButton.CheckBox">
- </style>
- <style name="Widget.Holo.Light.CompoundButton.CheckBox" parent="Widget.CompoundButton.CheckBox">
- </style>
このスタイルですが、4.1 と 4.2 で微妙に変わります。
4.1まで
http://tools.oesf.biz/android-4.1.2_r1.0/xref/frameworks/base/core/res/res/values/styles.xml#345
- <style name="Widget.CompoundButton.CheckBox">
- <item name="android:background">@android:drawable/btn_check_label_background</item>
- <item name="android:button">?android:attr/listChoiceIndicatorMultiple</item>
- </style>
4.2以降
http://tools.oesf.biz/android-4.2.0_r1.0/xref/frameworks/base/core/res/res/values/styles.xml#345
- <style name="Widget.CompoundButton.CheckBox">
- <item name="android:button">?android:attr/listChoiceIndicatorMultiple</item>
- </style>
実は 4.2 で paddingLeft の使い方が変わりました。Rtl に対応するためだと思われます。
4.1 までは左端からテキストの間が paddingLeft でした。そのため、チェックボックス画像分の paddingLeft を指定するために、背景画像が使われていました。
一方、4.2 からはチェックボックス画像とテキストの間が paddingLeft になりました。

チェックボックスをカスタマイズする場合
この違いのため、チェックボックスをカスタマイズする場合は注意が必要です。例えばチェックボックス画像を 32 x 32 dp で作成し、画像とテキストの間を 4dp あけたいとしたら、次のように values/dimens.xml と values-v17/dimens.xml を用意する必要があります。
values/dimens.xml
- <resources>
- <!-- 32 + 4 = 36 -->
- <dimen name="check_button_padding_left">36dp</dimen>
- </resources>
- <resources>
- <dimen name="check_button_padding_left">4dp</dimen>
- </resources>
チェックボックスの画像を変えるには、checkboxStyle に指定するスタイルで android:button に drawable を指定するか、テーマで android:listChoiceIndicatorMultiple に drawable を指定します。
values/styles.xml
- <resources>
- <style name="AppTheme" parent="Theme.Holo.Light">
- <item name="android:checkboxStyle">@style/CheckBoxStyle</item>
- </style>
- <style name="CheckBoxStyle" parent="android:Widget.CompoundButton.CheckBox">
- <item name="android:button">@drawable/my_custom_checkbox</item>
- <item name="android:paddingLeft">@dimen/check_button_padding_left</item>
- </style>
- </resources>
- <resources>
- <style name="AppTheme" parent="Theme.Holo.Light">
- <item name="android:listChoiceIndicatorMultiple">@drawable/my_custom_checkbox</item>
- <item name="android:checkboxStyle">@style/CheckBoxStyle</item>
- </style>
- <style name="CheckBoxStyle" parent="android:Widget.CompoundButton.CheckBox">
- <item name="android:paddingLeft">@dimen/check_button_padding_left</item>
- </style>
- </resources>
おまけ : CompoundButton.java の変更について
onDraw() については、Rtl のとき右端に描画されるようになっているだけで、paddingLeft の扱いは変わっていません。4.1.2
http://tools.oesf.biz/android-4.1.2_r1.0/xref/frameworks/base/core/java/android/widget/CompoundButton.java#228
- 227 @Override
- 228 protected void onDraw(Canvas canvas) {
- 229 super.onDraw(canvas);
- 230
- 231 final Drawable buttonDrawable = mButtonDrawable;
- 232 if (buttonDrawable != null) {
- 233 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
- 234 final int height = buttonDrawable.getIntrinsicHeight();
- 235
- 236 int y = 0;
- 237
- 238 switch (verticalGravity) {
- 239 case Gravity.BOTTOM:
- 240 y = getHeight() - height;
- 241 break;
- 242 case Gravity.CENTER_VERTICAL:
- 243 y = (getHeight() - height) / 2;
- 244 break;
- 245 }
- 246
- 247 buttonDrawable.setBounds(0, y, buttonDrawable.getIntrinsicWidth(), y + height);
- 248 buttonDrawable.draw(canvas);
- 249 }
- 250 }
4.2.0
http://tools.oesf.biz/android-4.2.0_r1.0/xref/frameworks/base/core/java/android/widget/CompoundButton.java#252
- 251 @Override
- 252 protected void onDraw(Canvas canvas) {
- 253 super.onDraw(canvas);
- 254
- 255 final Drawable buttonDrawable = mButtonDrawable;
- 256 if (buttonDrawable != null) {
- 257 final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
- 258 final int drawableHeight = buttonDrawable.getIntrinsicHeight();
- 259 final int drawableWidth = buttonDrawable.getIntrinsicWidth();
- 260
- 261 int top = 0;
- 262 switch (verticalGravity) {
- 263 case Gravity.BOTTOM:
- 264 top = getHeight() - drawableHeight;
- 265 break;
- 266 case Gravity.CENTER_VERTICAL:
- 267 top = (getHeight() - drawableHeight) / 2;
- 268 break;
- 269 }
- 270 int bottom = top + drawableHeight;
- 271 int left = isLayoutRtl() ? getWidth() - drawableWidth : 0;
- 272 int right = isLayoutRtl() ? getWidth() : drawableWidth;
- 273
- 274 buttonDrawable.setBounds(left, top, right, bottom);
- 275 buttonDrawable.draw(canvas);
- 276 }
- 277 }
- 227 @Override
- 228 public int getCompoundPaddingLeft() {
- 229 int padding = super.getCompoundPaddingLeft();
- 230 if (!isLayoutRtl()) {
- 231 final Drawable buttonDrawable = mButtonDrawable;
- 232 if (buttonDrawable != null) {
- 233 padding += buttonDrawable.getIntrinsicWidth();
- 234 }
- 235 }
- 236 return padding;
- 237 }
- 238
- 239 @Override
- 240 public int getCompoundPaddingRight() {
- 241 int padding = super.getCompoundPaddingRight();
- 242 if (isLayoutRtl()) {
- 243 final Drawable buttonDrawable = mButtonDrawable;
- 244 if (buttonDrawable != null) {
- 245 padding += buttonDrawable.getIntrinsicWidth();
- 246 }
- 247 }
- 248 return padding;
- 249 }
0 件のコメント:
コメントを投稿