2015年6月23日火曜日

AppCompat を継承したテーマで EditText のデフォルトスタイルを上書きするときは android:editTextStyle ではなく editTextStyle を使う

注意: appcompat-v7:22.2.0 での話です。将来 fix される可能性もあります。

AppCompat を継承したテーマで EditText のデフォルトスタイルを上書きしようとして android:editTextStyle を使うと、5系以降しか適用されないという落とし穴があります。

結論

android: をつけずに editTextStyle で指定すると4系にも適用されます。ただし、parent が Widget.AppCompat.EditText の場合、5系でカーソルが白になります(解説参照のこと)。
  1. <resources>  
  2.   
  3.     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
  4.         <item name="editTextStyle">@style/EditTextStyle</item>  
  5.     </style>  
  6.   
  7.     <style name="EditTextStyle" parent="Widget.AppCompat.EditText">  
  8.         <item name="android:background">#ccccff</item>  
  9.     </style>  
  10.   
  11. </resources>  

背景を変えたいだけなら android:editTextBackground と editTextBackground 両方を指定するほうが、カーソルの色を維持できます。
  1. <resources>  
  2.   
  3.     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
  4.         <item name="android:editTextBackground">@drawable/blue</item>  
  5.         <item name="editTextBackground">@drawable/blue</item>  
  6.     </style>  
  7.   
  8.     <drawable name="blue">#ccccff</drawable>  
  9.   
  10. </resources>  



解説

1. デフォルトの状態
  1. <resources>  
  2.   
  3.     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
  4.     </style>  
  5.   
  6. </resources>  



2. android:editTextStyle を指定した状態
  1. <resources>  
  2.   
  3.     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
  4.         <item name="android:editTextStyle">@style/EditTextStyle</item>  
  5.     </style>  
  6.   
  7.     <style name="EditTextStyle" parent="android:Widget.EditText">  
  8.         <item name="android:background">#ffffff</item>  
  9.     </style>  
  10.   
  11. </resources>  



3. editTextStyle を指定した状態
  1. <resources>  
  2.   
  3.     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
  4.         <item name="editTextStyle">@style/EditTextStyle</item>  
  5.     </style>  
  6.   
  7.     <style name="EditTextStyle" parent="android:Widget.EditText">  
  8.         <item name="android:background">#ffcccc</item>  
  9.     </style>  
  10.   
  11. </resources>  


EditTextStyle の parent が android:Widget.EditText なので、4.4.2 のカーソルの色が黒になってしまっています。


4. parent が Widget.AppCompat.EditText なスタイルを editTextStyle を指定した状態
  1. <resources>  
  2.   
  3.     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
  4.         <item name="editTextStyle">@style/EditTextStyle</item>  
  5.     </style>  
  6.   
  7.     <style name="EditTextStyle" parent="Widget.AppCompat.EditText">  
  8.         <item name="android:background">#ccccff</item>  
  9.     </style>  
  10.   
  11. </resources>  


今度は 5.0.0 のカーソルの色が白になった...

この白が何かというと、v12/values-v12.xml で android:textCursorDrawable に指定されている @drawable/abc_text_cursor_mtrl_alpha です。 これに accentColor で tint するのが適用されず白くなっているようです。たぶん。
  1. <style name="Base.V12.Widget.AppCompat.EditText" parent="Base.V7.Widget.AppCompat.EditText">  
  2.     <item name="android:textCursorDrawable">@drawable/abc_text_cursor_mtrl_alpha</item>  
  3. </style>  
そこで、android:editTextBackground です。


android:editTextBackground

背景を変えるだけなら android:editTextBackground を指定するという方法もあります。 ただ、こちらも落とし穴があり、
- android:editTextBackground // 5系にしか適用されない
- editTextBackground // 4系にしか適用されない
という状態なので、両方指定する必要があります。


5. android:editTextBackground を指定した状態
  1. <resources>  
  2.   
  3.     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
  4.         <item name="android:editTextBackground">@drawable/yellow</item>  
  5.     </style>  
  6.   
  7.     <drawable name="yellow">#ffff00</drawable>  
  8.   
  9. </resources>  



6. editTextBackground を指定した状態
  1. <resources>  
  2.   
  3.     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
  4.         <item name="editTextBackground">@drawable/cyan</item>  
  5.     </style>  
  6.   
  7.     <drawable name="cyan">#00ffff</drawable>  
  8.   
  9. </resources>  



7. android:editTextBackground と editTextBackground を両方指定した状態
  1. <resources>  
  2.   
  3.     <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  
  4.         <item name="android:editTextBackground">@drawable/blue</item>  
  5.         <item name="editTextBackground">@drawable/blue</item>  
  6.     </style>  
  7.   
  8.     <drawable name="blue">#ccccff</drawable>  
  9.   
  10. </resources>  



0 件のコメント:

コメントを投稿