我开发了一个带圆角的app小部件。我基本上设置了一个可绘制到app小部件布局根目录的背景。
现在我试图改变它的背景不透明度而不会丢失圆角。
我知道RemoteViews
非常有限。
现在我正在使用这个代码:views.setInt(R.id.root_layout_widget, "setBackgroundColor", ColorUtils.setAlphaComponent(Color.WHITE, opacity))
但是这样我失去了圆角。如果我没有使用RemoteViews
,我会得到元素的背景并设置它的alpha。
我的根元素是LinearLayout,我的背景可绘制如下:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@android:color/white" />
<corners android:radius="2dp" />
</shape>
你知道有什么办法吗?谢谢
您可以尝试此代码
int transparency = 192;(the amount of opacity level you want to give)
views.setInt(R.id.root_layout_widget, "setBackgroundColor", transparency);
代替
views.setInt(R.id.root_layout_widget, "setBackgroundColor", ColorUtils.setAlphaComponent(Color.WHITE, opacity))
当您设置背景颜色时,您将替换现有的drawable,有点烦人。我建议您在背景视图中设置色调颜色,您可能会觉得这个答案很有用:How to set tint for an image view programmatically in android?