【发布时间】:2014-09-01 15:58:36
【问题描述】:
我已经玩了几天的 L 预览,但似乎不起作用的一件事是视图的 android:elevation 属性。调用View.setElevation() 时它工作得很好,但它似乎忽略了 XML 属性。我正在使用 Android Studio 0.8.2 并将minSdkVersion 和targetSdkVersion 设置为'L',并将compileSdkVersion 设置为'android-L'。 buildToolsVersion 是 "20.0.0"。这是一个无法正常工作的示例布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:elevation="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="top"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:elevation="0dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bottom"/>
</android.support.v7.widget.CardView>
</LinearLayout>
这是结果的截图:imgur.com/mqeq9vK
根据我设置的android:elevation,底牌应该平在“地面”上,但事实并非如此。事实上,它看起来与海拔是5dp 的顶牌没有什么不同。这是一个已知问题吗?它对您有用吗?
编辑:似乎这只是CardView 的情况,或者至少Buttons 不是问题。在这个布局中,我用Button 替换了CardView,即使阴影有点搞砸了(已知错误),你仍然可以看到高程的差异。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="2dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="1dp"/>
</LinearLayout>
编辑 2:几乎可以确认,此问题仅影响 CardView,android:elevation 适用于任何其他视图。有关为什么会发生这种情况的详细解释,请查看接受的答案。同时,我的解决方法是将CardView 替换为FrameLayout 并将android:background 设置为可绘制对象。这是一个有效的卡片背景可绘制示例:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#fff" />
</shape>
我也将此作为错误提交,所以如果它影响到您,请在此问题上加注星标。 https://code.google.com/p/android-developer-preview/issues/detail?id=731
【问题讨论】:
标签: android android-5.0-lollipop android-elevation