为什么 android:buttonStyle 没有样式按钮?
Why android:buttonStyle does not style buttons?
所以,我有一个奇怪的问题:我的应用程序非常简单,主 activity 只有一个按钮和清单中 activity 设置的自定义主题。
我可以确认主题有效并被选中,因为我可以更改 activity 背景或字体颜色,例如
但是当我尝试为 activity 上的所有按钮设置样式时,它不起作用!
这是我使用的 styles.xml 代码:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:background">#0f0</item>
<item name="android:buttonStyle">@style/btxs</item>
</style>
<style name="btxs" parent="@android:style/Widget.Button">
<item name="android:textColor">#f00</item>
</style>
</resources>
activity 的背景是绿色的(所以主题有效),但按钮仍然是默认的。
在主题中设置按钮样式的方法是否android:buttonStyle
正确?
为了参考,我也粘贴了我的 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:layout_marginTop="25dp" />
</RelativeLayout>
AppCompatActivity
将使用 buttonStyle
。我还假设您可能想要扩展 Base.Widget.AppCompat.Button
.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:background">#0f0</item>
<item name="buttonStyle">@style/btxs</item>
</style>
<style name="btxs" parent="Base.Widget.AppCompat.Button">
<item name="android:textColor">#f00</item>
</style>
</resources>
所以,我有一个奇怪的问题:我的应用程序非常简单,主 activity 只有一个按钮和清单中 activity 设置的自定义主题。
我可以确认主题有效并被选中,因为我可以更改 activity 背景或字体颜色,例如
但是当我尝试为 activity 上的所有按钮设置样式时,它不起作用!
这是我使用的 styles.xml 代码:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:background">#0f0</item>
<item name="android:buttonStyle">@style/btxs</item>
</style>
<style name="btxs" parent="@android:style/Widget.Button">
<item name="android:textColor">#f00</item>
</style>
</resources>
activity 的背景是绿色的(所以主题有效),但按钮仍然是默认的。
在主题中设置按钮样式的方法是否android:buttonStyle
正确?
为了参考,我也粘贴了我的 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:layout_marginTop="25dp" />
</RelativeLayout>
AppCompatActivity
将使用 buttonStyle
。我还假设您可能想要扩展 Base.Widget.AppCompat.Button
.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:background">#0f0</item>
<item name="buttonStyle">@style/btxs</item>
</style>
<style name="btxs" parent="Base.Widget.AppCompat.Button">
<item name="android:textColor">#f00</item>
</style>
</resources>