更改 Radis CardView 背景颜色

时间:2021-04-26 06:28:06

标签: android xml android-layout android-cardview cardview

我有一个带有半径和笔画的 CardView,但是当我以编程方式更改 CardView 背景时,半径和笔画都丢失了,我希望新颜色保留在笔画内。

这是我的cardview xml

<com.google.android.material.card.MaterialCardView
      android:id="@+id/answerOneTransport"
      android:layout_width="148dp"
      android:layout_height="60dp"
      android:layout_marginStart="32dp"
      android:layout_marginTop="32dp"
      app:cardBackgroundColor="#F7F7F7"
      app:cardCornerRadius="16dp"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/quizzTransportViewPager"
      app:strokeColor="#9890FF"
      app:strokeWidth="1dp" >
      <TextView
          android:id="@+id/textAnswwerOneTransport"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Answer One"
          android:layout_gravity="center"
          android:fontFamily="@font/nunito"
          android:textSize="20sp"
          />
  </com.google.android.material.card.MaterialCardView> 

这里是我改变颜色的地方:

answerOneTransport.setOnClickListener {
    answerOneTransport.setBackgroundColor(Color.GREEN)
}

2 个答案:

答案 0 :(得分:2)

要更改 CardView 的背景颜色,您必须使用以下方法:

cardView.setCardBackgroundColor(ContextCompat.getColor(this,R.color.xxx));

enter image description here

答案 1 :(得分:0)

您需要使用 (landmark: Landmark) -> some View in