[英]Setting a button background and color
我想为同一个按钮设置背景和颜色。 我使用setbackgroundResource
和setBackgroundColor
来这样做......但是它只改变背景或颜色而不是两者。
有没有办法以不同的方式更改按钮背景和背景颜色?
如果您想更改背景图像的颜色,即您使用setBackgroundResource
设置然后使用
setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.black)));
假设您想将可绘制对象设置为前景并将颜色设置为背景,您将需要一个 ImageView。 您可以直接从 xml 设置它:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/black"
android:src="@drawable/my_awesome_drawable" />
或者像这样以编程方式:
imageView.background = resources.getDrawable(R.color.black)
imageView.setImageResource(R.drawable.my_awesome_drawable)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.