我想稍微更改标准Android按钮的颜色,以便更好地匹配客户的品牌.
到目前为止,我发现这样做的最好方法是将Button'drawable'改为drawable,位于res/drawable/red_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/red_button_pressed" />
<item android:state_focused="true" android:drawable="@drawable/red_button_focus" />
<item android:drawable="@drawable/red_button_rest" />
</selector>
Run Code Online (Sandbox Code Playgroud)
但这样做需要我居然为每一个按钮,我想自定义三种不同的可绘制(一个用于按钮在休息,一个集中的时候,和一个按下时).这似乎比我需要的更复杂,更干燥.
我真正想做的就是对按钮应用某种颜色转换.是否有更简单的方法来改变按钮的颜色而不是我正在做的事情?
<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" />
Run Code Online (Sandbox Code Playgroud)
这是我的 color.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>
Run Code Online (Sandbox Code Playgroud)
样式文件如下:
<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>
Run Code Online (Sandbox Code Playgroud)
当我尝试像这样设置背景颜色时:android:background="#f0f0f0",背景颜色没有变化。我正在尝试修复大约一个小时左右。请提供任何帮助。