如何通过首次单击更改CardView的边框颜色,然后在第二次单击Android时再次更改为先前的颜色?

发布于 2025-01-22 15:48:20 字数 550 浏览 0 评论 0原文

如何使用选择器而不是更改背景?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁杀 2025-01-29 15:48:20

使用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” “ 您可以删除默认已检查的图标。以下是一个示例用法:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/materialCardView"
        android:layout_width="80dp"
        android:layout_height="100dp"
        android:checkable="true"
        app:cardBackgroundColor="@android:color/holo_green_light"
        app:cardForegroundColor="@android:color/transparent"
        app:checkedIconSize="0dp"
        app:cardCornerRadius="10dp"
        app:cardElevation="2dp"
        app:strokeWidth="4dp"
        app:strokeColor="@color/card_border_selector"/>

</RelativeLayout>

其中@color/card_border_selectorres/color/color/color/card_border_selector.xml之类的类似于以下内容中定义

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/holo_orange_light" android:state_checked="true"/>
    <item android:color="@android:color/holo_blue_light" android:state_checked="false"/>
</selector>

: code>材料cardview 您必须使用材料>材料cardview.setchecked()函数更改状态,如下示例:

MaterialCardView materialCardView = findViewById(R.id.materialCardView);
materialCardView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        materialCardView.setChecked(!materialCardView.isChecked());
    }
});

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 the com.google.android.material.card.MaterialCardView from Material Design Library which is a subclass of androidx.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 from Theme.MaterialComponents.*.

Now with MaterialCardView you can use the app: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 the app:strokeWidth to set the stroke width. To activate the android:state_checked=true/false states you have to add also the attribute android:checkable="true" and with app:checkedIconSize="0dp" you can remove the default checked-stated icon. Below is an example usage:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/materialCardView"
        android:layout_width="80dp"
        android:layout_height="100dp"
        android:checkable="true"
        app:cardBackgroundColor="@android:color/holo_green_light"
        app:cardForegroundColor="@android:color/transparent"
        app:checkedIconSize="0dp"
        app:cardCornerRadius="10dp"
        app:cardElevation="2dp"
        app:strokeWidth="4dp"
        app:strokeColor="@color/card_border_selector"/>

</RelativeLayout>

where the @color/card_border_selector is defined in the res/color/card_border_selector.xml like the below:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/holo_orange_light" android:state_checked="true"/>
    <item android:color="@android:color/holo_blue_light" android:state_checked="false"/>
</selector>

And programmatically every time you click on the MaterialCardView you have to change the state using the materialCardView.setChecked() function like the below example:

MaterialCardView materialCardView = findViewById(R.id.materialCardView);
materialCardView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        materialCardView.setChecked(!materialCardView.isChecked());
    }
});

Result for android:state_checked=false:

unselected_state

Result for android:state_checked=true:

selected_state

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文