[英]Why is my button taking "colorPrimary" as the default background color?
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Explore"
android:id="@+id/btn_dialog"
android:gravity="center_vertical|center_horizontal"
android:layout_below="@+id/text_dialog"
android:layout_marginBottom="20dp"
android:layout_centerHorizontal="true"
android:textColor="#ffffff" />
这是我的 colors.xml 文件。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#00ccff</color>
<color name="colorPrimaryDark">#00ccff</color>
<color name="colorAccent">#00ccff</color>
<color name="darkPrimary">#212121</color>
<color name="darkPrimaryDark">#000000</color>
<color name="whitecolor">#FFFFFF</color>
<color name="switchcolor">#f0f0f0</color>
</resources>
样式文件如下:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
当我尝试像这样设置背景颜色时: android:background="#f0f0f0"
,背景颜色没有变化。 我正在尝试修复大约一个小时左右。 请任何帮助表示赞赏。
在布局 XML 中使用app:backgroundTint
。 它将从应用程序的原色更改背景颜色
在样式文件中更改它:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
至
<style name="AppTheme" parent="Theme.AppCompat">
我在网上搜索了这个问题的答案,但没有找到一个可以在我的 Android 项目中正常工作的解决方案。
几个小时后,我唯一想到的是: - 在“res/values”中创建一个新样式
- 在其中添加一个名为“primaryColor”的参数(重复 res/values/themes/themes.xml 指令中的默认颜色的名称)
- 在其中设置所需的颜色
- 将给定的样式设置为我们按钮的“主题”属性
我得到了什么:
my_button_style.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="My_button_style">
<item name="colorPrimary">@color/someColor</item>
</style>
</resources>
和我在 GroupView.xml 中的按钮:
<Button
style="@style/My_button_style"
android:theme="@style/My_button_style"
...
/>
它适用于 Android API 20 及更低版本,而其他解决方案则不适用。 PS我确信这个解决方案并不完全正确,但我不是唯一一个面临这个问题的人,我还没有在网上找到另一种解决方案。 我试图用选择器解决这个问题,但它没有用。 使用Java码解决问题的方法不适合我 谢谢关注!
更改主题.xml 文件
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
至
<style name="Theme.MyApplication" parent="Theme.AppCompat.DayNight.DarkActionBar">
由于您使用的是 Theme.MaterialComponents.*,因此您的 Button 在运行时会被 MaterialButton 替换。
目前 backgroundTint 仍然是默认的 MaterialButton 样式。 这意味着如果您使用自定义 android:background,您必须确保 null out backgroundTint 以避免自定义背景不会被主题中定义的 attr/colorPrimary 着色。
您必须添加 app:backgroundTint="@null":
应用程序:backgroundTint="@null" android:background="@drawable/background"
首先在您的styles.xml
或themes.xml
中创建btn_style
<style name="btn_style" parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">@null</item>
</style>
然后在您的布局中应用样式并使用您想要的color
或drawable
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:background="@color/Green"
style="@style/btn_style"
android:text="Button"/>
那是因为您将主题设置为Theme.MaterialComponents.*
检查此以获取更多详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.