gpt4 book ai didi

android - Android 按钮的圆角 + 背景颜色动态设置?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:19:51 25 4
gpt4 key购买 nike

我想在我的应用程序中为按钮提供以下设置。您可以将其视为按钮的一些主题更改。

样式 圆形或普通

颜色 红色或黄色或蓝色或任何颜色代码

我知道使用 XML 中定义的形状,我可以实现圆角。使用 setBackgroundColor,我可以将任何颜色设置为背景。

问题

setBackgroundColorsetBackground 都根据我调用它们的顺序相互覆盖。所以我无法在同一个按钮上实现这两种效果。我怎样才能同时达到这两种效果。我怎样才能从单个 Button 类中获取多个按钮。提前致谢。

enter image description here

最佳答案

好吧,我是用 GradientDrawable 做的

    int color = Color.rgb(255,0,0); //red for example
int radius = 5; //radius will be 5px
int strokeWidth = 2;

GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(color);
gradientDrawable.setCornerRadius(radius);
gradientDrawable.setStroke(strokeWidth, color);
button.setBackground(gradientDrawable);

关于android - Android 按钮的圆角 + 背景颜色动态设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26037990/

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