我正在尝试设置按钮的样式,同时保持向后兼容性。
我有两个文件如下:
values/styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="colorPrimary">@color/primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">@color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">@color/accent</item>
<item name="android:textColorPrimary">@color/primary_text</item>
<item name="android:textColorSecondary">@color/secondary_text</item>
<item name="android:buttonStyle">@style/RoundedButton</item>
</style>
<style name="RoundedButton" parent="@style/Widget.AppCompat.Button">
</style>
<style name="AppName.NoActionBar" parent="AppTheme" >
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="DemoButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:minWidth">40dp</item>
</style>
</resources>
values-v21/styles.xml:
<resources>
<style name="RoundedButton" parent="Widget.AppCompat.Button">
<item name="android:background">@drawable/btn_rounded</item>
</style>
</resources>
但即便如此,我的按钮也会丢失所有属性,即按照android-studio的“XML预览”显示文本。我只想让按钮在pre-lolipop设备上默认为通常的Holo主题。它在Lolipop设备上按预期工作。 我做错了什么?
答案 0 :(得分:0)
在values / styles.xml中
<style name="RoundedButton" parent="@style/Widget.AppCompat.Button">
</style>
将其更改为关注: -
<style name="RoundedButton" parent="android:Widget.Holo">
</style>
答案 1 :(得分:0)
我无法找出导致这种情况发生的原因,因此我从<item name="android:buttonStyle">@style/RoundedButton</item>
文件中删除了行values/styles.xml
,并将其添加到我的values-v21/styles.xml
。
我不喜欢这个解决方案,因为有很多代码重复。关于它的唯一好处是它有效。
现在我的文件看起来像:
values/styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="colorPrimary">@color/primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">@color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">@color/accent</item>
<item name="android:textColorPrimary">@color/primary_text</item>
<item name="android:textColorSecondary">@color/secondary_text</item>
</style>
<style name="AppName.NoActionBar" parent="AppTheme" >
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="DemoButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:minWidth">40dp</item>
</style>
</resources>
v21/styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="colorPrimary">@color/primary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">@color/primary_dark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">@color/accent</item>
<item name="android:textColorPrimary">@color/primary_text</item>
<item name="android:textColorSecondary">@color/secondary_text</item>
<item name="android:buttonStyle">@style/RoundedButton</item>
</style>
<style name="RoundedButton" parent="Widget.AppCompat.Button">
<item name="android:background">@drawable/btn_rounded</item>
</style>
<style name="AppName.NoActionBar" parent="AppTheme" >
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="DemoButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:minWidth">40dp</item>
</style>
</resources>