【问题标题】:How to set semi transparent background color for Android CardView?如何为 Android CardView 设置半透明背景颜色?
【发布时间】:2020-07-21 12:26:24
【问题描述】:

我想将卡片视图的背景颜色设置为 argb 颜色。但是结果是这样的:

<androidx.cardview.widget.CardView
        android:id="@+id/infoCard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="8dp"
        app:cardBackgroundColor="#59448AFF"
        app:cardCornerRadius="8dp">

        <com.google.android.material.textview.MaterialTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/space_16dp"
            android:layout_marginTop="@dimen/space_8dp"
            android:layout_marginEnd="@dimen/space_16dp"
            android:layout_marginBottom="@dimen/space_8dp"
            android:lineSpacingExtra="@dimen/space_4dp"
            android:text="This is a sample text"
            android:textColor="@color/secondaryText"
            android:textSize="16sp" />

    </androidx.cardview.widget.CardView>

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: android android-cardview cardview


    【解决方案1】:

    color.xml内定义

    <color name="semiTransparentColor">#20ffffff</color>
    

    现在这样设置卡片视图。

    card_view:cardBackgroundColor="@color/semiTransparentColor"
    

    这里是透明的十六进制代码。 link你可以根据需要设置。

    【讨论】:

      【解决方案2】:

      只需将此设置为卡片视图

      app:cardElevation="0dp"
      

      或尝试删除 com.google.android.material.textview.MaterialTextView 的背景

      android:background="@null"
      

      【讨论】:

      • 这个答案可以解释设置高程与设置背景颜色的关系。
      猜你喜欢
      • 2017-06-02
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      相关资源
      最近更新 更多