如何通过首次单击更改CardView的边框颜色,然后在第二次单击Android时再次更改为先前的颜色?
如何使用选择器而不是更改背景?
if(isSelected) {
binding.cvStainMaster.setBcackgroundResource(R.drawable.boarder_blue_corner);
binding.btnStart.setBackgroundResource(R.drawable.bg_button_primary);
isSelected = false;
}else{
binding.cvStainMaster.setBackgroundColor(Color.WHITE);
binding.btnStart.setBackgroundResource(R.drawable.button_background_with_corner);
isSelected = true;
}
我通过不断变化的背景来完成。但是我希望它使用选择器
How to do it using selector instead of changing background?
if(isSelected) {
binding.cvStainMaster.setBcackgroundResource(R.drawable.boarder_blue_corner);
binding.btnStart.setBackgroundResource(R.drawable.bg_button_primary);
isSelected = false;
}else{
binding.cvStainMaster.setBackgroundColor(Color.WHITE);
binding.btnStart.setBackgroundResource(R.drawable.button_background_with_corner);
isSelected = true;
}
I have done it by changing background. but I want it to do using selector
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
androidx.cardview.widget.cardview
没有任何属性可以更改边框/冲程颜色,但是您可以使用com.google.android.android.material.card.materialcard.materialcardview <<<<<<
com.google.google.google.android.android. /code>来自材料设计库是
androidx.cardview的子类。 widget.cardview
(材料cardview扩展了cardView
)。要在您的项目中添加
youteralCardView
首先,您必须将材料设计库的祖父添加到最新版本,例如:实现'com.google.android.android.material.material.material:材料:1.5。 0'
,您的应用主题必须从theme.materialcomponents。*
继承。现在,使用
材料cardview
您可以使用app:strokecolor
属性将stroke/border color指向颜色选择器设置为颜色选择器,以根据检查/未检查的状态更改边框颜色和app:strokeWidth
以设置中风宽度。要激活android:state_checked = true/false
note您还必须添加属性android:checkable =“ true”
and app:checkediconsize =“ 0dp” “ 您可以删除默认已检查的图标。以下是一个示例用法:其中
@color/card_border_selector
在res/color/color/color/card_border_selector.xml
之类的类似于以下内容中定义: code>材料cardview 您必须使用
材料>材料cardview.setchecked()
函数更改状态,如下示例:android:state_checked = state_checked = false
:的
android:state_checked = true
:Using the
androidx.cardview.widget.CardView
there is no any property to change the border/stroke colour but you can achieve it using thecom.google.android.material.card.MaterialCardView
from Material Design Library which is a subclass ofandroidx.cardview.widget.CardView
(MaterialCardView extends CardView
).To add the
MaterialCardView
in your project first you have to add the grandle depedency of Material Design Library pointing to the latest version like:implementation 'com.google.android.material:material:1.5.0'
and your app Theme must inherit fromTheme.MaterialComponents.*
.Now with
MaterialCardView
you can use theapp:strokeColor
attribute to set the stroke/border color pointing to a color selector to change the border color based on the card checked/unchecked state and theapp:strokeWidth
to set the stroke width. To activate theandroid:state_checked=true/false
states you have to add also the attributeandroid:checkable="true"
and withapp:checkedIconSize="0dp"
you can remove the default checked-stated icon. Below is an example usage:where the
@color/card_border_selector
is defined in theres/color/card_border_selector.xml
like the below:And programmatically every time you click on the
MaterialCardView
you have to change the state using thematerialCardView.setChecked()
function like the below example:Result for
android:state_checked=false
:Result for
android:state_checked=true
: