[英]get Cardview Background Color
我在 Android 中为 CardView 设置了一个随机颜色。
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
holder.cardView.setBackgroundColor(color);
如何以编程方式在运行时获取 Cardview 背景颜色?
你可以简单地初始化一个 CardView 并设置一个 id:
作为您在 res/layout 中的 .xml 文件中的示例:
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="10dp"
app:cardCornerRadius="4dp"
app:cardBackgroundColor="@color/cardview_dark_background"
android:background="@color/cardview_dark_background">
然后在您的活动/片段中初始化它,以便:
CardView cardView = (CardView) findViewById(R.id.CardView);
cardView.getCardBackgroundColor();
请注意,此方法返回一个ColorStateList
而不是单个颜色值
所以要获得单个颜色值,只需调用:
int backgroundColor = cardView.getCardBackgroundColor().getDefaultColor();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.