问题
After updating the appcompat library to the latest version (v21) to apply the material design, my custom theme isn't used anymore on Android 4.0.X devices unless I explicitly declare it in the XML.
I have tested the theme on Android 2.3.X and 5.0.0 devices and it works on those devices. But it's not working on my HTC One S with Android 4.0.3. What do I need to do to fix this?
My themes.xml
<style name="Theme.Custom" parent="@style/Theme.AppCompat.Light">
<item name="android:editTextStyle">@style/Custom.Widget.EditText</item>
<item name="android:buttonStyle">@style/Custom.Widget.Button</item>
</style>
My styles.xml
<style name="Custom.Widget.EditText" parent="android:Widget.EditText">
<item name="android:background">@drawable/edit_text_holo_light</item>
<item name="android:textColor">@color/text_primary</item>
<item name="android:textColorHint">@color/text_hint_color</item>
</style>
<style name="Custom.Widget.Button" parent="android:Widget.Button">
<item name="android:background">@drawable/btn_default_holo_light</item>
<item name="android:minHeight">48dip</item>
<item name="android:minWidth">64dip</item>
<item name="android:textColor">@color/button_text_primary</item>
</style>
My layout file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<!-- other elements -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="@+id/view_username_edit_username_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions|textCapSentences"
android:hint="@string/UsernameScreen_usernameHint"
android:imeOptions="actionDone"
android:singleLine="true"
android:maxLength="@integer/username_max_length"
android:textSize="@dimen/text_size_medium" />
<Button
android:id="@+id/view_username_save_Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/view_username_edit_username_editText"
android:clickable="true"
android:enabled="false"
android:orientation="horizontal"
android:text="@string/common_save" />
</RelativeLayout>
</RelativeLayout>
If I add the following to lines in the layout files it works. So there seems to be going something wrong with the themes.xml.
style="@style/Custom.Widget.EditText"
style="@style/Custom.Widget.Button"
Result:
Expected:
Edit
I tested the same code on some other devices.
- Samsung S2 (4.0.4): OK
- HTC One (4.4.3): OK
- HTC One X (4.0.3): NOK
- HTC One S (4.1.1): NOK
- HTC Desire C (4.0.3): NOK
I don't see why it wouldn't work on some of these devices.
回答1:
I after add buttonStyle without the "android:" prefix and it solved the problem. (name="android:buttonStyle" -> name="buttonStyle")
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="buttonStyle">@style/SkinedButton</item>
</style>
回答2:
Finally I found a solution to my problem. I was using @style/Theme.AppCompat
as a parent to one of my styles. After changing it to @style/Theme.AppCompat.Light.DarkActionBar
it works on all devices. I still find it very strange why this would screw up my styling.
来源:https://stackoverflow.com/questions/27603189/theme-not-applied-using-appcompat-library-on-some-android-4-x-devices