Android CardView卡高度和阴影无法正常工作

问题描述 投票:3回答:4

我在活动中使用CardView这套卡片。

在运行应用程序时,我得到的不是预期的cardview:没有高程,没有阴影......

Android 7设备上的结果:

Android 7

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="4dp"
    card_view:cardUseCompatPadding="true"
    android:layout_margin="8dp"
    android:gravity="center_horizontal"
    android:id="@+id/card_view"
    android:padding="10dp"
    android:layout_centerHorizontal="true"
    card_view:cardBackgroundColor="@color/main_color_grey_800">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="350.0dp"
            android:scaleType="centerCrop"
             />

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:padding="15.0dp"
            android:textColor="@android:color/black"
            android:textSize="16.0sp"
            android:text="@string/LoremText" />

        <TextView
            android:id="@+id/desc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:padding="15.0dp"
            android:textColor="@android:color/black"
            android:textSize="12.0sp"
            android:textAlignment="center"
            android:text="@string/LoremText" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="end"
            android:orientation="horizontal"
            android:paddingTop="10.0dip">

            <Button
                android:id="@+id/answer_1"
                android:layout_width="0px"
                android:layout_height="50.0dip"
                android:text="@string/Maybe"
                android:textColor="@color/white"
                android:background="@color/cpb_red"
                android:textSize="14sp"
                style="@style/DBTheme.Button.Orange"
                android:textStyle="bold"
                android:layout_weight="1"
                tools:ignore="ButtonStyle"
                android:visibility="gone" />

            <Button
                android:id="@+id/answer_2"
                style="@style/DBTheme.Button.Orange"
                android:layout_width="0px"
                android:layout_height="50.0dip"
                android:text="@string/yes_button"
                android:textColor="@android:color/white"
                android:textSize="14sp"
                android:textStyle="bold"
                android:background="@color/cpb_red"
                android:layout_weight="1"
                android:visibility="gone" />
            <Button
                android:id="@+id/answer_3"
                style="@style/DBTheme.Button.Orange"
                android:layout_width="0px"
                android:layout_height="50.0dip"
                android:layout_weight="1.0"
                android:textStyle="bold"
                android:text="@string/no_button"
                android:textColor="@color/white"
                android:background="@color/cpb_red"
                android:textSize="14sp"
                android:visibility="gone" />
        </LinearLayout>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="4.0dp"
        android:background="@drawable/shadow" />
</android.support.v7.widget.CardView>

关于如何解决这个问题的任何想法?谢谢 :)

android android-layout android-recyclerview android-cardview
4个回答
16
投票

在挖掘,播放和修改布局后,我甚至创建了一个新项目并在当前项目中添加了相同的代码我得到了不同的结果xD,事实证明问题是由android:hardwareAccelerated="false"文件中的AndroidManifest.xml引起的。只需设置值该属性为真,你会很高兴。


2
投票

使用此代码解决您的问题

<android.support.v7.widget.CardView
            android:id="@+id/cardview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:elevation="5dp"
            card_view:cardBackgroundColor="@color/cardview_initial_background"
            card_view:cardCornerRadius="8dp"
            android:layout_marginLeft="@dimen/margin_large"
            android:layout_marginRight="@dimen/margin_large"
            >

1
投票

在XML文件中使用它

 <android.support.v7.widget.CardView
            android:id="@+id/card_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            app:cardBackgroundColor="@color/white"
            app:cardCornerRadius="@dimen/_5sdp"
            app:cardElevation="@dimen/_8sdp"
            app:contentPadding="@dimen/_15sdp"
            app:cardUseCompatPadding="true">

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

0
投票

要显示高程阴影需要一些余量。只要把layout_margin="<some_value>",它的工作完美。

© www.soinside.com 2019 - 2024. All rights reserved.