在Android 5.0上的AppCompat CardView上设置XML高程

时间:2014-11-18 14:49:39

标签: android android-appcompat android-cardview android-elevation

根据我的理解,在预览阶段的早期,似乎没有办法只在CardView上设置XML的高程而没有Java中的黑客攻击。既然官方发布已经发布,有没有办法在XML中执行此操作而无需编写Java代码来设置高程?

我已尝试card_view:cardElevation无效。我曾经想过当我使用5.0的模拟器时一切都很好。但是现在我在我的实际设备上使用正式版本,我的所有CardView都消失了

Pre Lollipop,效果很好。

这是我的完整xml

<?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:orientation="horizontal"
    android:layout_width="match_parent"
    android:id="@+id/cv1"
    card_view:cardElevation="4dp"
    android:layout_margin="6dp"
    card_view:cardCornerRadius="3dp"
    android:layout_height="match_parent">

6 个答案:

答案 0 :(得分:298)

看起来像边距/填充问题,尝试将cardUseCompatPadding属性设置为true。 E.g:

<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="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="6dp"
    card_view:cardUseCompatPadding="true"
    card_view:cardElevation="4dp"
    card_view:cardCornerRadius="3dp">

来自Android doc的说明:

  

CardView添加额外的填充以在平台上绘制阴影   升。

     

这可能会导致卡片在L和L之前有不同的尺寸。   如果您需要将CardView与其他视图对齐,则可能需要api   版本特定维度资源以考虑更改。作为一个   或者,您可以将cardUseCompatPadding标志设置为true,而CardView将在平台L和之后添加相同的填充值。

     

由于将cardUseCompatPadding标志设置为true会在UI中添加不必要的间隙,因此默认值为false。

答案 1 :(得分:12)

您必须使用cardElevation属性

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    card_view:cardElevation="4dp" />

答案 2 :(得分:12)

如果你有这一行

android:hardwareAccelerated="false"
清单应用程序标签中的

您的阴影未显示。尝试删除此行

或使用

android:hardwareAccelerated="true"

这对我有用!我希望它也适合你:)

答案 3 :(得分:1)

它通过添加

解决了我

的xmlns:card_view = “http://schemas.android.com/apk/res-auto”

例如:

In [463]: df = pd.DataFrame({'close in percent':np.random.randn(1000000)})

In [464]: df['momentum'] = np.where(df['close in percent'] > 0, 1, -1)

In [465]: momentum = 2*(df['close in percent'] > 0) - 1

# From @Brad Solomon's soln
In [466]: %timeit df['3_day_momentum'] = df.momentum.rolling(3).mean()
10 loops, best of 3: 27.3 ms per loop

# SciPy's uniform filter
In [467]: %timeit df['3_day_momentum_out'] = rolling_mean(momentum, W=3)
100 loops, best of 3: 7.69 ms per loop

答案 4 :(得分:0)

我要在卡片视图上添加一个app:cardElevation =“ 8dp”属性,因此就像:

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        app:cardElevation="8dp"
        app:cardCornerRadius="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:src="@drawable/kmp_warna1" />

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

希望这会有所帮助

答案 5 :(得分:0)

在cardView中设置

    <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="match_parent"
android:layout_height="match_parent"
android:layout_margin="6dp"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="3dp">