2014-01-23 92 views
1

在我的setings應用程序中,我使用EditTextPreference,並且在android API uder 11 edittext字段上有黑色背景和黑色文本顏色。 我該如何改變EditTextPreferences背景顏色?Android EditTextPreference風格

我想: 在主題:

<item name="android:editTextPreferenceStyle">@style/EditTextAppTheme</item> 

風格:

<style name="EditTextAppTheme" parent="android:Widget.EditText"> 
     <item name="android:background">@drawable/edit_text_holo_light</item> 
     <item name="android:textColor">#000000</item> 
    </style> 

如果我設置風格:

<style name="EditTextPreferences" parent="android:Preference.DeviceDefault.DialogPreference.EditTextPreference"> 
     <item name="android:background">@drawable/edit_text_holo_light</item> 
     <item name="android:textColor">#FF0000</item> 
    </style> 

而且主題:

<item name="android:editTextPreferenceStyle">@style/EditTextPreferences</item> 

我得到錯誤:

error: Error retrieving parent for item: No resource found that matches the given name 'android:Preference.DeviceDefault.DialogPreference.EditTextPreference'. 

回答

2

我一直在試圖弄清楚這一點,以及在過去的幾天。我發現所有的EditTextPrefence延伸是AlertDialogEditText。因此,如果您可以在主題中對這兩種風格進行設計,您將在EditTextPreference中找到您要查找的結果。這裏就是我的工作:

<?xml version="1.0" encoding="utf-8"?> 
<resources xmlns:android="http://schemas.android.com/apk/res/android"> 

<style name="LightTheme" parent="android:Theme.Light"> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:background">@color/greyscale2</item> 
    <item name="android:textColor">@color/greyscale16</item> 
    <item name="android:textViewStyle">@style/LightTextView</item> 
    <item name="android:buttonStyle">@style/LightButton</item> 
    <item name="android:editTextStyle">@style/LightEditText</item> 
    <item name="android:alertDialogTheme">@style/LightDialog</item> 
    <item name="android:dialogPreferenceStyle">@style/LightDialog</item> 
    <item name="android:colorBackground">@color/greyscale2</item> 
    <item name="android:colorBackgroundCacheHint">@color/greyscale1</item> 
</style> 

<!-- TextView --> 
<style name="LightTextView" parent="android:Widget.TextView"> 
    <item name="android:textColor">@color/greyscale16</item> 
    <item name="android:background">@android:color/transparent</item> 
</style> 

<!-- Button --> 
<style name="LightButton" parent="android:Widget.Button"> 
    <item name="android:textColor">@color/greyscale16</item> 
    <item name="android:background">@drawable/light_button_background</item> 
    <item name="android:gravity">center_vertical|center_horizontal</item> 
</style> 

<style name="LightCheckBox" parent="android:Widget.CompoundButton.CheckBox"> 
    <item name="android:background">@color/greyscale2</item> 
</style> 

<style name="LightEditText" parent="android:style/Widget.EditText"> 
    <item name="android:background">@color/greyscale1</item> 
    <item name="android:editTextBackground">@color/greyscale2</item> 
    <item name="android:textColor">@color/greyscale16</item> 
    <item name="android:button">@style/DarkButton</item> 
    <item name="android:inputType">number</item> 
</style> 

<style name="LightDialog" parent="@android:style/Theme.Dialog"> 
    <item name="android:background">@color/greyscale2</item> 
    <item name="android:textColor">@color/greyscale16</item> 
    <item name="android:textViewStyle">@style/LightTextView</item> 
    <item name="android:buttonStyle">@style/LightButton</item> 
    <item name="android:divider">@color/greyscale14</item> 
</style> 

通知的 <item name="android:dialogPreferenceStyle">@style/LightDialog</item>屬性在我的基本主題。我發現,設置樣式資源here,更具體alertDiaolg here(注:這些也可以在你的SDK目錄中找到您的計算機上)

我希望這至少負責你在正確的方向,我一直在努力想出解決(我的應用程序中的第一個主題)。快樂編碼!

0

http://i.imgur.com/zr0VUzY.jpg

我想你已經觀察到的效應是薑餅模擬器中的錯誤。 更改活動的主題並重新啓動模擬器後,EditTextPreference如上所示。

0

首先延長EditTextPreference和覆蓋onPrepareDialogBuilder,反轉背景色低於蜂窩版本:

public class CustomEditTextPreference extends EditTextPreference { 

    /** 
    * Prepares the dialog builder to be shown when the preference is clicked. 
    * Use this to set custom properties on the dialog. 
    * <p> 
    * Do not {@link AlertDialog.Builder#create()} or 
    * {@link AlertDialog.Builder#show()}. 
    */ 
    @Override 
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { 
     builder.setInverseBackgroundForced(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB); 
    } 
} 

然後在你的preferences.xml所有

<EditTextPreference ... /> 

<your.package.CustomEditTextPreference ... /> 
0

我有這個替換確切的問題,當我切換到API v21 AppCom帕特(我認爲 - 它以前用於至少工作得很好)。 我的解決辦法是定義爲Android的風格:明確主題我editTextStyle - 這裏是我的完整styles.xml:

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 

    <!-- 
     Base application theme, dependent on API level. This theme is replaced 
     by AppBaseTheme from res/values-vXX/styles.xml on newer devices. 
    --> 
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light"> 

     <!-- 
      Theme customizations available in newer API levels can go in 
      res/values-vXX/styles.xml, while customizations related to 
      backward-compatibility can go here. 
     --> 
     <item name="android:editTextStyle">@android:style/Widget.EditText</item> 
    </style> 

    <!-- Application theme. --> 
    <style name="AppTheme" parent="AppBaseTheme"> 
     <!-- All customizations that are NOT specific to a particular API-level can go here. --> 
    </style> 

</resources> 

結果是EDITTEXT看起來一樣@Jesse韋伯的屏幕截圖。