我想稍微改变标准安卓按钮的 colored颜色 ,以便更好地匹配客户的品牌.

到目前为止,我找到的最好的方法是将Button的可抽屉改为位于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>

但要做到这一点,我实际上需要为我想要定制的每个按钮创建三个不同的可绘制按钮(一个用于静止的按钮,一个用于聚焦,一个用于按下).这似乎比我需要的更复杂、更不干燥.

我真正想做的就是对按钮进行某种 colored颜色 变换.有没有比我更容易改变按钮 colored颜色 的方法?

推荐答案

我发现这一切都可以在一个文件中轻松完成.在名为custom_button.xml的文件中输入以下代码,然后在按钮视图中设置background="@drawable/custom_button":

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" >
        <shape>
            <gradient
                android:startColor="@color/yellow1"
                android:endColor="@color/yellow2"
                android:angle="270" />
            <stroke
                android:width="3dp"
                android:color="@color/grey05" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item android:state_focused="true" >
        <shape>
            <gradient
                android:endColor="@color/orange4"
                android:startColor="@color/orange5"
                android:angle="270" />
            <stroke
                android:width="3dp"
                android:color="@color/grey05" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item>        
        <shape>
            <gradient
                android:endColor="@color/blue2"
                android:startColor="@color/blue25"
                android:angle="270" />
            <stroke
                android:width="3dp"
                android:color="@color/grey05" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

Android相关问答推荐

更新Jetpack Compose打破了动态色彩

错误:无法解析Symbol@style/Theme. Androidstudio in AndroidManifest.html for Kotlin Android Development''

无法安装后重新编译android代码'

房间DB:UPSERT返回什么?

不能有意地从一个活动的可组合功能转移到另一个活动

Jetpack Compose:根据盒子中的最大视图固定宽度和高度

(已解决)从最近的应用程序打开应用程序时出错

在本地通知中设置自定义声音

Android Compose 为什么 Checkbox 在父对象可点击时需要 onCheckChanged?

Android Studio XML 文本在 ConstraintLayout 中不居中

为什么我要使用 $version 而不是2.7.0?

如何用jetpack compose实现垂直李克特量表

在 theme.xml 中使用 android:autoCompleteTextViewStyle

Jetpack Compose 惰性列在单个项目更新时重新组合所有项目

找不到(包名称).在以下位置搜索:

Unity:Android 上随机接近零的 FPS 下降(提供了很多线索)

如何在包含 Jetpack Compose 内容的布局中使用权重

在 jetpack compose 中使用 viewmodel 的最佳实践

使用 Room 在 SQLite 中保存复杂的 JSON 响应

如何在 Kotlin 的片段中制作图像列表?