首页 > 解决方案 > 获取背景按钮并将此颜色应用于另一个

问题描述

我在 android studio 上有一个项目,我需要获取按钮的背景颜色才能将颜色应用于另一个。

我试过这个:

ColorDrawable BgColor = (ColorDrawable) btn_next.getBackground();
btn_filRouge.setBackgroundColor(BgColor);

我想将 ColorDrawable 转换为 int。或者直接把颜色变成int。

标签: javaandroidandroid-studiocolorsbackground

解决方案


我想将 ColorDrawable 转换为 int。或者直接颜色变成int 。

您不能将 ColorDrawable 转换为 int,但您可以遵循第二种解决方案,答案在您的问题中,您可以使用getColor()

ColorDrawable bgColor = (ColorDrawable) btn_next.getBackground();
int color = bgColor.getColor();

有关更多详细信息,请查看ColorDrawable的文档


推荐阅读