繁体   English   中英

android:elevation在L预览中不起作用

[英]android:elevation doesn't work in L preview

我已经玩了几天的L预览,有一件似乎不起作用的是视图的android:elevation属性。 它在调用View.setElevation()时工作得很好,但它似乎忽略了XML属性。 我正在使用Android Studio 0.8.2并将minSdkVersiontargetSdkVersion设置为'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:已经证实这个问题只影响CardViewandroid:elevation适用于任何其他视图。 有关为何发生这种情况的详细说明,请查看已接受的答案。 在此期间,我的解决方法是用FrameLayout替换CardView并将android:background设置为drawable。 以下是可用的卡背景可绘制的示例:

<?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));
}

请使用cardElevation属性在CardView上设置高程。 这也将影响前L的阴影大小。

另外,请参阅其他属性的文档

请在您的根目录中添加

xmlns:card_view="http://schemas.android.com/apk/res-auto"

下面是cardview的代码

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardElevation="10dp"
    android:id="@+id/view" >
    .
    .
    .
    .
    .
    Child
    .
    .
    .
    .

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

在发布错误修复之前,您可以以编程方式设置它。

我在RecyclerView完成了这样的事情:

在您的RecyclerView.Adapter中:

public class MyAdapter extends RecyclerView.Adapter<TripsAdapter.ViewHolder> {

    /* ... */

    public class ViewHolder extends RecyclerView.ViewHolder {
        CardView card;

        public ViewHolder(View itemView) {
            card = (CardView) itemView.findViewById(R.id.card_view);
        }
    }

    /* ... */

    @Override public void onBindViewHolder(ViewHolder holder, int position) {
        /// ...

        // Update Card Elevation
        final float scale = mContext.getResources().getDisplayMetrics().density;
        holder.card.setElevation(1f * scale);    //TODO set this in xml once CardView elevation bug is fixed
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM