我已经玩了几天 L 预览版,似乎不起作用的一件事是 View 的 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
的问题。在这个布局中,我用 CardView
替换了 Button
,即使阴影有点搞砸了(已知错误),你仍然可以看到高程的差异。<?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
对任何其他 View 都可以正常工作。有关为什么会发生这种情况的详细说明,请查看已接受的答案。同时,我的解决方法是将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
最佳答案
CardView 在初始化期间设置自己的高度,这将覆盖您从 XML 设置的任何内容。你应该在 https://code.google.com/p/android-developer-preview/wiki/FilingIssues?tm=3 将此作为错误提交
@Override
public void initialize(CardViewDelegate cardView, Context context, int backgroundColor,
float radius) {
cardView.setBackgroundDrawable(new RoundRectDrawable(backgroundColor, radius));
View view = (View) cardView;
view.setClipToOutline(true);
view.setElevation(context.getResources().getDimension(R.dimen.cardview_elevation));
}