gpt4 book ai didi

android - 为什么安卓:buttonStyle does not style buttons?

转载 作者:太空狗 更新时间:2023-10-29 16:14:25 25 4
gpt4 key购买 nike

所以,我遇到了这个奇怪的问题:我的应用非常简单,主要 Activity 只有一个按钮, list 中为 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>

关于android - 为什么安卓:buttonStyle does not style buttons?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33748406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com