lig*_*igi 41 android android-layout
CardView(android.support.v7.cardview)保持白色,即使我通过android:backround设置了一个backround drawable - 文档让我感觉它应该工作.不知道我在这里做错了什么.
Phi*_*hil 51
我知道这是一个老问题,但我有一个简单的解决方案 - 只需让你的CardView的第一个孩子成为一个ImageView,并指定比例类型为fitXY.您可以通过将cardElevation和cardMaxElevation设置为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>
vik*_*mar 10
对于可绘制或颜色只是使用:
cvSellerShopp.setBackgroundResource(R.drawable.ic_action_add_image);
用于颜色:
 cvSellerShopp.setCardBackgroundColor(R.color.colorPrimary);
但是这个没有产生预期的结果
在这种情况下,让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>
我通过在cardview中添加linearlayout,然后在cardview中将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>
我使用的命令是:
cardView.setBackgroundResource(R.drawable.card_view_bg);
其中card_view_bg是自定义 XML 资源文件。
如果您在卡片视图中有一些布局并且这些布局与卡片视图的边距重叠,那么您可能需要一个单独的自定义背景资源文件用于布局,就像我用于卡片视图背景本身的那个。
| 归档时间: | 
 | 
| 查看次数: | 54586 次 | 
| 最近记录: |