我正在尝试自定义我的警报对话框。但我无法更改按钮的背景颜色。 我尝试了可绘制按钮设计、样式、在 xml 中设置直接颜色以及在 Activity 中通过 java 代码设置颜色,但我无法更改它。 按钮的形状发生变化,但颜色只能是默认颜色或透明颜色。
按钮本身的xml编码:
<Button
android:id="@+id/btnDenied"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="35dp"
android:layout_marginBottom="15dp"
android:background="@drawable/button_selected" //// or @drawable/button_costume
android:text="@string/button_no" />
选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed = "true">
<shape>
<solid android:color="#FFEB3B"/>
</shape>
</item>
<item android:state_pressed = "false">
<shape>
<solid android:color="#FFC107"/>
</shape>
</item>
</selector>
和另一个可绘制文件:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="50dp"/>
<solid
android:color="@color/colorHeader"/>
</shape>
我也尝试过这种风格:
<style name="Button_Style"
parent="@android:style/Widget.Button">
<item name="android:textSize">14sp</item>
<item name="android:background">@color/colorHeader</item>
<item name="android:textColor">#1B1B22</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_margin">2dp</item>
</style>
但他们都不能改变按钮的颜色。
最佳答案
我编写的代码是 100% 正确的,但有时 Android Studio 会执行一些错误的任务,因此可以通过再次运行程序或执行以下步骤来解决这些问题:
清理项目,从构建菜单中。
重建项目。
And I got this problem with me not to show errors in xml file, I hope this thing helped you.
关于java - 我的应用程序忽略按钮背景颜色的任何更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61838548/