しかし、これまでデフォルトの文字サイズ(android:textSize や setTextSize() してない widget の文字サイズ)でアプリを作成していたが、 sp 単位のことを考慮していなかったためにレイアウトが崩れるなどの現象がおこり、どうしてもデフォルトの文字単位を一括で dp にしたいこともあるでしょう。
デフォルトの文字サイズについては Android UI Cookbook for 4. 0 の 1.2.2 タイポグラフィに書いたので、詳しくはそっちを参照してください。
例えば、TextView のテーマを見てみると、
- 437 <style name="Widget.TextView">
- 438 <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
- ...
- 448 </style>
http://tools.oesf.biz/android-4.0.1_r1.0/xref/frameworks/base/core/res/res/values/styles.xml#437
のように、textAppearanceSmall という attr を参照しています。
- 42 <style name="Theme">
- 85 <item name="textAppearanceSmall">@android:style/TextAppearance.Small</item>
- 371 </style>
http://tools.oesf.biz/android-4.0.1_r1.0/xref/frameworks/base/core/res/res/values/themes.xml#83
デフォルトのテーマでは textAppearanceSmall には @android:style/TextAppearance.Small が指定されており、 これは次のように 14sp になっています。
- 792 <style name="TextAppearance.Small">
- 793 <item name="android:textSize">14sp</item>
- 794 <item name="android:textColor">?textColorSecondary</item>
- 795 </style>
よって、オリジナルテーマで textAppearanceSmall を書き換えれば TextView のデフォルトの文字サイズを sp でなくすことができます。もちろん、textAppearanceSmall を参照している他の widget も影響を受けます。
- <style name="MyTheme" parent="@android:style/Theme">
- <item name="textAppearanceSmall">@style/MyTextAppearanceSmall</item>
- </style>
- <style name="MyTextAppearanceSmall" parent="@android:style/TextAppearance.Small">
- <item name="android:textSize">14dp</item>
- </style>
デフォルトテーマで TextAppearance として定義されているのは small だけではありません。かなりいろいろありますが、 http://tools.oesf.biz/android-4.0.1_r1.0/xref/frameworks/base/core/res/res/values/themes.xml#83
- textAppearanceLarge
- textAppearanceMedium
- textAppearanceSmall
- textAppearanceLargeInverse
- textAppearanceMediumInverse
- textAppearanceSmallInverse
あたりをカバーすればだいたいの widget は影響範囲に入ります。
もちろん ActionBar のタブの文字サイズなど、影響範囲に入らないものは個別にスタイルを指定することになります。
0 件のコメント:
コメントを投稿