在MaterialCardView的中心设置自定义图标

时间:2019-10-31 08:59:50

标签: android material-design android-cardview material-components material-components-android

我无法在MaterialCardView的中心设置自定义图标。这是我的xml布局

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/cardPaymentContainer"
    android:layout_width="0dp"
    android:layout_height="200dp"
    android:layout_margin="@dimen/default_margin"
    android:layout_marginStart="@dimen/button_margin"
    android:layout_marginEnd="@dimen/button_margin"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolBarContainer">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/cardPaymentCardView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:checkable="true"
        android:clickable="true"
        android:focusable="true"
        app:cardCornerRadius="@dimen/card_view_cornder_radius"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    </com.google.android.material.card.MaterialCardView>

</androidx.constraintlayout.widget.ConstraintLayout>

处理点击活动:

dataBinding =
            DataBindingUtil.setContentView<PaymentActivityBinding>(this, R.layout.payment_activity)
        dataBinding.setHandler(this)
        dataBinding.cardPaymentCardView.setOnClickListener({
            Debug.d(TAG, "cardPaymentCardView: onClick")
            dataBinding.cardPaymentCardView.isChecked = !dataBinding.cardPaymentCardView.isChecked
        })

结果:

未点击:

enter image description here

然后单击:

enter image description here

但是我需要将自定义图标放在CardView的中心。 像这样的SMT:

enter image description here

我通过Android Studio将SVG转换为xml

我尝试了此操作,但是在cardView的中心没有显示图标,也没有显示

 <com.google.android.material.card.MaterialCardView
                android:id="@+id/cardPaymentCardView"
                style="@style/cardViewStyle"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:checkable="true"
                android:clickable="true"
                android:focusable="true"
                app:cardCornerRadius="@dimen/card_view_cornder_radius"
                app:checkedIcon="@drawable/ic_credit_card_outline_select"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

            </com.google.android.material.card.MaterialCardView>

3 个答案:

答案 0 :(得分:1)

简而言之,您无法使用组件提供的标准checkedIcon来实现它。您必须在Card内添加一个drawable / view。

当前(版本1.1.0-beta01和1.2.0-alpha01)您可以

  • 使用app:checkedIcon属性定义自定义图标
  • 使用app:checkedIconTint属性为图标定义颜色选择器

(但我建议您避免使用这种代码)您可以:

  • 覆盖选中的图标在dimens.xml中添加的边距:

      <!-- Margin between the checked icon and the card -->
      <dimen name="mtrl_card_checked_icon_margin">xxdp</dimen>
    
  • 覆盖在dimens.xml中添加的选中图标所使用的大小:

      <!-- Size of the icon to be placed when the card is checked -->
      <dimen name="mtrl_card_checked_icon_size">xxdp</dimen>
    

wildcard syntax

答案 1 :(得分:0)

尝试使用<selector>与此类似:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_checked_gray_24dp" android:state_selected="true"/>
    <item android:drawable="@drawable/ic_checked_gray_24dp" android:state_pressed="true"/>
    <item android:drawable="@drawable/ic_checked_gray_24dp" android:state_checked="true"/>
    <item android:drawable="@drawable/ic_checked_white_32dp"/>
</selector>

答案 2 :(得分:0)

尝试app:checkedIconMargin

<com.google.android.material.card.MaterialCardView
    android:id="@+id/cardPaymentCardView"
    style="@style/cardViewStyle"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:checkable="true"
    android:clickable="true"
    android:focusable="true"
    app:cardCornerRadius="@dimen/card_view_cornder_radius"
    app:checkedIcon="@drawable/ic_credit_card_outline_select"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"

    app:checkedIconMargin="10dp"

    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

</com.google.android.material.card.MaterialCardView>