设置在提升XML的AppCompat CardView在Android 5.0 [英] Setting Elevation in XML on AppCompat CardView on Android 5.0

查看:206
本文介绍了设置在提升XML的AppCompat CardView在Android 5.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,在preVIEW阶段早期似乎有没有办法只对 CardView S设定海拔XML没有Java中的黑客。现在正式发布出来了,有什么办法没有编写Java code将抬高XML这样做的?

我已经试过 card_view:cardElevation 没有效果。我曾在我所用的仿真器5.0一切都很好的想法。但现在,我使用的是正式版我的实际设备上我所有的 CardView 失踪以及

pre棒棒糖,它的伟大工程。

下面是我的完整的XML

 < XML版本=1.0编码=UTF-8&GT?;
< android.support.v7.widget.CardView的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:card_view =htt​​p://schemas.android.com/apk/res-auto
    机器人:方向=横向
    机器人:layout_width =match_parent
    机器人:ID =@ + ID / CV1
    card_view:cardElevation =4DP
    机器人:layout_margin =6DP
    card_view:cardCornerRadius =3DP
    机器人:layout_height =match_parent>
 

解决方案

它看起来像一个保证金/填充的问题,尝试设置<一个href="https://developer.android.com/reference/android/support/v7/widget/CardView.html#attr_android.support.v7.cardview:cardUseCompatPadding"相对=nofollow> cardUseCompatPadding 属性为true。例如:

 &LT; android.support.v7.widget.CardView
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:card_view =htt​​p://schemas.android.com/apk/res-auto
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_margin =6DP
    card_view:cardUseCompatPadding =真
    card_view:cardElevation =4DP
    card_view:cardCornerRadius =3DP&GT;
 

这是Android的文档说明:

  

CardView增加了额外的填充之前绘制的平台上阴影   L;

     

这可能会导致卡为L之间和L.之前不同的尺寸   如果您需要对齐CardView与其他视图,你可能需要的API   版本特定维度的资源占的变化。作为   此外,也可以设置cardUseCompatPadding标志设置为true,并CardView将平台上的L和后添加相同的填充值。

     

由于cardUseCompatPadding标志设置为true,增加了不必要的空白界面,默认值为false。

From what I understand, early in the preview stage there seemed to be no way to set elevation in XML only on CardViews without a hack in Java. Now that the official release is out, is there any way of doing this in XML without writing Java code to set elevation?

I have tried card_view:cardElevation to no effect. I had thought when I was using the emulators for 5.0 everything was fine. But now that I'm using the official version on my actual device all of my CardViews disappeared

Pre Lollipop, it works great.

Here is my full 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">

解决方案

It looks like a margin/padding problem, try to set the cardUseCompatPadding attribute to 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">

Explanation from Android doc :

CardView adds additional padding to draw shadows on platforms before L.

This may cause Cards to have different sizes between L and before L. If you need to align CardView with other Views, you may need api version specific dimension resources to account for the changes. As an alternative, you can set cardUseCompatPadding flag to true and CardView will add the same padding values on platforms L and after.

Since setting cardUseCompatPadding flag to true adds unnecessary gaps in the UI, default value is false.

这篇关于设置在提升XML的AppCompat CardView在Android 5.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆