更改 Radius CardView 背景颜色
Change Raduis CardView Background Color
我有一个带有半径和笔划的 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)
}
您需要使用 android:backgroundTint
要更改 CardView
的背景颜色,您必须使用以下方法:
cardView.setCardBackgroundColor(ContextCompat.getColor(this,R.color.xxx));
我有一个带有半径和笔划的 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)
}
您需要使用 android:backgroundTint
要更改 CardView
的背景颜色,您必须使用以下方法:
cardView.setCardBackgroundColor(ContextCompat.getColor(this,R.color.xxx));