【问题标题】:CardView background alpha colour not working correctlyCardView 背景 alpha 颜色无法正常工作
【发布时间】:2019-01-05 18:26:04
【问题描述】:

我正在尝试使CardView 具有高程,但问题是当我使用像"#ffffff" 这样的无alpha 颜色时它工作正常但是当我设置一些像#b0ffffff 这样的alpha 颜色时,它会显示另一个带有高程的内部视图 像这样

但是当我设置像"#ffffff" 这样的非 alpha 颜色时,它可以正常工作

这是我的布局

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="#b0ffffff"
    android:layout_centerInParent="true"
    card_view:cardBackgroundColor="#b0ffffff"
    card_view:cardElevation="5dp">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:src="@drawable/people" />

</android.support.v7.widget.CardView>

顺便说一句,我正在尝试制作这样的视图

【问题讨论】:

    标签: android android-layout android-cardview android-shape


    【解决方案1】:

    只需删除该行:

    card_view:cardBackgroundColor="#b0ffffff"
    

    它应该可以工作

    【讨论】:

    • 但我想默认分配 alpha 颜色,它显示为白色
    • 在卡片视图背景中什么都不做
    【解决方案2】:

    试试这个

               <android.support.v7.widget.CardView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    app:cardBackgroundColor="#B0FFFFFF"
                    app:cardCornerRadius="50dp"
                    app:cardElevation="5dp">
    
                    <ImageView
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_gravity="center"
                        android:src="@drawable/people" />
    
                </android.support.v7.widget.CardView>
    

    【讨论】:

    • 它给了我上面提到的相同的内部圆形视图
    【解决方案3】:

    如果您在CardView 周围看到恼人的边框,您可以尝试下面的代码,如배준모's answer 中所述。这也适用于androidx.cardview.widget.CardView

    <android.support.v7.widget.CardView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="@dimen/margin"
        android:background="@android:color/transparent"
        android:elevation="0dp"
        app:cardBackgroundColor="@color/form_card_bg_color"
        app:cardElevation="0dp"
        app:contentPadding="@dimen/margin_large"
        app:cardPreventCornerOverlap="false"
        app:cardUseCompatPadding="true" >
    

    【讨论】:

      猜你喜欢
      • 2023-02-09
      • 2020-08-02
      • 2018-08-19
      • 2021-02-16
      • 2014-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多