【问题标题】:Drawable as Background for CardView可绘制为 CardView 的背景
【发布时间】:2022-03-24 21:56:28
【问题描述】:

即使我通过 android:backround 设置了可绘制的背景,CardView ( android.support.v7.cardview ) 仍保持白色 - 文档让我觉得它应该可以工作。不知道我在这里做错了什么。

【问题讨论】:

标签: android android-layout


【解决方案1】:

我知道这是一个老问题,但我有一个简单的解决方案 - 只需将您的 CardView 的第一个孩子设为 ImageView 并将 scale 类型 指定为 适合XY。您可以通过将 cardElevationcardMaxElevation 设置为 0dp 来消除额外的 CardView 填充:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:cardCornerRadius="4dp"
    app:cardElevation="0dp"
    app:cardMaxElevation="0dp">

    <ImageView
        android:src="@drawable/your_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"/>

    <... your layout

    .../>


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

【讨论】:

  • 有没有办法在不放弃高程阴影的情况下做到这一点?
【解决方案2】:

对于可绘制或颜色,只需使用:

cvSellerShopp.setBackgroundResource(R.drawable.ic_action_add_image);

颜色用途:

 cvSellerShopp.setCardBackgroundColor(R.color.colorPrimary);

但是这个没有产生预期的结果

【讨论】:

    【解决方案3】:

    在这种情况下,Make Cardview 将为例如相对布局托管一个视图组,然后简单地将任何背景设置为相对布局。

    <?xml version="1.0" encoding="utf-8"?>
        <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:id="@+id/list_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            card_view:cardCornerRadius="4dp"
            card_view:cardUseCompatPadding="true">
    
            <RelativeLayout
                android:id="@+id/list_container_bg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:orientation="vertical">
    
               <!--Add cardview contents-->
    
            </RelativeLayout>
        </android.support.v7.widget.CardView>
    

    【讨论】:

      【解决方案4】:

      我通过在卡片视图中添加一个线性布局然后在卡片视图中将 cardPreventCornerOverlap 设置为 false 来实现此功能。

         <android.support.v7.widget.CardView
          android:id="@+id/result_cv"
          android:layout_width="match_parent"
          android:layout_height="150dp"
          android:layout_margin="4dp"
          app:cardCornerRadius="16dp"
          app:cardElevation="8dp"
          app:cardPreventCornerOverlap="false"
          >
      
           <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@drawable/gradient"
              android:gravity="center"
              >
      
             <!-- your views -->
      
          </LinearLayout>
      
          </android.support.v7.widget.CardView>
      

      【讨论】:

        【解决方案5】:

        我使用的命令是:

        cardView.setBackgroundResource(R.drawable.card_view_bg);
        

        其中 card_view_bg 是自定义 XML 资源文件。

        如果您在卡片视图中有一些布局,并且这些布局与卡片视图的边距重叠,那么您可能需要一个单独的自定义背景资源文件用于布局,就像我用于卡片视图背景本身的那个一样。

        【讨论】:

          【解决方案6】:

          试试这个

          <?xml version="1.0" encoding="utf-8"?>
          <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:id="@+id/list_container"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:layout_margin="5dp"
              card_view:cardCornerRadius="4dp"
              card_view:cardUseCompatPadding="true">
          
              <RelativeLayout
                  android:id="@+id/list_container_bg"
                  android:layout_width="match_parent"
                  android:background="@drawable/yourbackground"
                  android:layout_height="match_parent">
          
                 <!--Add cardview contents-->
          
              </RelativeLayout>
          </android.support.v7.widget.CardView>
          

          card_view:cardBackgroundColor="@android:color/holo_green_dark

          在这里您可以设置任何颜色或通过赋予颜色透明使其透明 如果你想要一些可绘制的图像或 svg 放置相对布局背景

          【讨论】:

            【解决方案7】:

            您可以在 CardView 中使用 LinearLayout 或相对布局,并设置可绘制背景,如下所示:

            <android.support.v7.widget.CardView
                android:id="@+id/card6"
                android:layout_width="180dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="8dp"
                android:layout_marginTop="10dp"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/card4"
                app:cardCornerRadius="10dp"
                app:cardElevation="4dp"
                app:cardMaxElevation="8dp">
            
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:background="@drawable/card_bg"
                    android:padding="10dp">
            
                    <ImageView
                        android:id="@+id/card6Image"
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_gravity="center_horizontal"
                        android:src="@mipmap/ic_launcher_round" />
            
                    <TextView
                        android:id="@+id/card6Title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:padding="5dp"
                        android:text="Daily Check In"
                        android:textColor="#ffffff"
                        android:textSize="15sp" />
            
                    <TextView
                        android:id="@+id/card6Description"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:padding="5dp"
                        android:text="Just Check in Daily and Earn Credits"
                        android:textColor="#ffffff"
                        android:textSize="10sp" />
                </LinearLayout>
            
            </android.support.v7.widget.CardView>
            

            下面是我的 Drawable 资源文件:

            <?xml version="1.0" encoding="UTF-8"?>
            <shape xmlns:android="http://schemas.android.com/apk/res/android" 
            android:shape="rectangle" >
            <gradient
                android:angle="90"
                android:centerColor="#555994"
                android:endColor="#b5b6d2"
                android:startColor="#555994"
                android:type="linear" />
            
            <corners
                android:radius="0dp"/>
            

            【讨论】:

              【解决方案8】:

              您可以为卡片视图设置如下前景以设置自定义背景

              android:foreground="@drawable/bg"
              

              这是透明的 bg.xml

              <shape xmlns:android="http://schemas.android.com/apk/res/android">
                  <solid android:color="@android:color/transparent" />
                  <stroke
                      android:width="1dp"
                      android:color="#d9d9d9" />
                  <corners android:radius="4dp" />
                  <padding
                      android:bottom="0dp"
                      android:left="0dp"
                      android:right="0dp"
                      android:top="0dp" />
              </shape>
              

              【讨论】:

                【解决方案9】:

                您有 2 种可能的解决方案。

                1. 以编程方式设置背景:
                cardView.setBackgroundResource(R.drawable.background)
                
                1. 向 cardview 添加一些子项,以填充 cardview 的边界并在其上设置背景:
                <android.support.v7.widget.CardView
                ...>
                     <ConstraintLayout
                        android:background="@drawable/background"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </android.support.v7.widget.CardView>
                

                这样你也可以给图像一些形状,你可以通过cardview的形状来配置。缺点是性能不好。

                【讨论】:

                  【解决方案10】:

                  试试这个代码:

                  cardView.setForeground(getResources().getDrawable(R.drawable.your_drawable));
                  

                  【讨论】:

                    【解决方案11】:
                    sCard.setBackgroundResource(R.drawable.scard_background);
                    

                    【讨论】:

                      【解决方案12】:

                      如果您想更改 cardView 的背景颜色,只需在您的 cardview XML 中添加此代码

                      app:cardBackgroundColor="@color/whatever_color_you"
                      

                      【讨论】:

                        【解决方案13】:

                        您可以在 xml 文件中简单地使用以下行。

                        app:cardBackgroundColor="@drawable/your_custom_theme_here"
                        

                        【讨论】:

                        • 为什么不喜欢这个答案?这对我有用。
                        • 因为问题是关于可绘制而不是颜色。
                        猜你喜欢
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2014-07-04
                        • 2018-08-19
                        • 1970-01-01
                        相关资源
                        最近更新 更多