![](/img/trans.png)
[英]How to set Random GradientDrawable Color programmatically - Android
[英]How to get colors from gradientDrawable on android Marshmallow
我的应用程序使用GradientDrawable
。 我必须从GradientDrawable
获取 colors 并将其设置为背景。
private int[] getGradientColors(GradientDrawable drawable) {
// So I have to add this check, cause getColors() method requires min API 24 or higher
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
return drawable;
return Objects.requireNonNull(drawable).getColors();
}
有没有其他方法可以在 API 23 中获得这个 colors?
据我了解,您想将渐变设置为布局背景。
将此行添加到父布局会将背景设置为渐变
android:background="@drawable/gradient"
或者
通过代码
linearLayout.setBackgroundResource(R.drawable.gradient);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.