Android 安卓;立面图“;不露影子

Android 安卓;立面图“;不露影子,android,material-design,shadow,android-elevation,Android,Material Design,Shadow,Android Elevation,我有一个ListView,每个列表项下都有一个阴影。我正在使用Android棒棒糖的新提升功能在我想要投射阴影的视图上设置一个Z,并且已经通过ActionBar(技术上是棒棒糖中的工具栏)有效地实现了这一点。我使用的是棒棒糖的高度,但由于某些原因,它没有在列表项下显示阴影。以下是如何设置每个列表项的布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.an

我有一个ListView,每个列表项下都有一个阴影。我正在使用Android棒棒糖的新提升功能在我想要投射阴影的视图上设置一个Z,并且已经通过ActionBar(技术上是棒棒糖中的工具栏)有效地实现了这一点。我使用的是棒棒糖的高度,但由于某些原因,它没有在列表项下显示阴影。以下是如何设置每个列表项的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    style="@style/block"
    android:gravity="center"
    android:layout_gravity="center"
    android:background="@color/lightgray"
    >

    <RelativeLayout
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:elevation="30dp"
        >

        <ImageView
            android:id="@+id/documentImageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/alphared"
            android:layout_alignParentBottom="true" >

            <appuccino.simplyscan.Extra.CustomTextView
                android:id="@+id/documentName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                app:typeface="light"
                android:paddingLeft="16dp"
                android:paddingTop="8dp"
                android:paddingBottom="4dp"
                android:singleLine="true"
                android:text="New Document"
                android:textSize="27sp"/>

            <appuccino.simplyscan.Extra.CustomTextView
                android:id="@+id/documentPageCount"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                app:typeface="italic"
                android:paddingLeft="16dp"
                android:layout_marginBottom="16dp"
                android:text="1 page"
                android:textSize="20sp"/>

        </LinearLayout>

    </RelativeLayout>

</RelativeLayout>

但是,下面是它在没有阴影的情况下显示列表项的方式:

我还尝试了以下方法,但没有成功:

  • 将高程设置为ImageView和TextView本身,而不是父布局
  • 将背景应用于ImageView
  • 使用平移Z代替高程

  • 我在棒棒糖上玩了一会儿阴影,我发现:

  • 似乎父视图组的边界由于某种原因切断了子视图组的阴影;及
  • android:elevation
    设置的阴影被
    视图的边界截断,而不是通过边距延伸的边界
    
  • 让子视图显示阴影的正确方法是在父视图上设置填充,并在父视图上设置
    android:clipToPadding=“false”
  • 根据我所知,以下是我给你的建议:

  • 将顶层
    相对阴影设置为使填充等于您在要显示阴影的相对布局上设置的边距
    
  • 在同一个
    RelativeLayout
    上设置
    android:cliptoppadding=“false”
  • 从同样设置了高程的
    相对线窗口中删除边距
    
  • [编辑]您可能还需要在需要提升的子布局上设置不透明背景色
  • 一天结束时,您的顶层相对布局应该如下所示:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        style="@style/block"
        android:gravity="center"
        android:layout_gravity="center"
        android:background="@color/lightgray"
        android:paddingLeft="40dp"
        android:paddingRight="40dp"
        android:paddingTop="20dp"
        android:paddingBottom="20dp"
        android:clipToPadding="false"
        >
    
    <RelativeLayout
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:background="[some non-transparent color]"
        android:elevation="30dp"
        >
    
    
    
    内部相对布局应如下所示:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        style="@style/block"
        android:gravity="center"
        android:layout_gravity="center"
        android:background="@color/lightgray"
        android:paddingLeft="40dp"
        android:paddingRight="40dp"
        android:paddingTop="20dp"
        android:paddingBottom="20dp"
        android:clipToPadding="false"
        >
    
    <RelativeLayout
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:background="[some non-transparent color]"
        android:elevation="30dp"
        >
    
    
    
    如果您有一个没有背景的视图,这行代码将对您有所帮助

    android:outlineProvider="bounds"
    
    android:outlineProvider="bounds"
    
    android:outlineProvider="paddedBounds"
    

    默认情况下,阴影由视图的背景确定,所以如果没有背景,也将没有阴影

    我认为您的问题源于这样一个事实,即您已将立面元素应用于填充其父元素的相对布局。其父视图在其自己的图形画布中剪辑所有子视图(并且是处理子视图阴影的视图)。您可以通过将高程属性应用于视图xml(根标记)中最顶端的
    RelativeLayout来解决此问题。

    显然,您不能在视图上设置高程并使其显示。您还需要指定背景

    添加到我的LinearLayout的以下线条最终显示了阴影:

    android:background="@android:color/white"
    android:elevation="10dp"
    

    除了公认的答案之外,还有一个原因,即如果手机上的蓝光滤光片功能开启,则提升可能无法按预期工作

    我正面临这个问题时,我的设备中的海拔高度出现了完全扭曲和无法忍受的情况。但在预览中,海拔很好。关闭手机上的蓝光过滤器解决了这个问题

    所以即使在设定之后

    android:outlineProvider="bounds"
    

    立面无法正常工作,您可以尝试修改手机的颜色设置。

    添加背景色对我有帮助

    <CalendarView
        android:layout_width="match_parent"
        android:id="@+id/calendarView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_height="wrap_content"
        android:elevation="5dp"
    
        android:background="@color/windowBackground"
    
        />
    

    呃,我花了一个小时想弄明白。在我的例子中,我有一个背景设置,但是它被设置为一种颜色。这还不够,需要将视图的背景设置为可绘制

    e、 g。 这不会有阴影:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="165dp"
        android:background="@color/ight_grey"
        android:elevation="20dp"
        android:orientation="vertical"
        />
    
    
    
    但这会

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="165dp"
        android:background="@drawable/selector_grey"
        android:elevation="20dp"
        android:orientation="vertical"
        />
    
    
    
    编辑: 我们还发现,如果使用选择器作为背景,如果未设置角点,则阴影不会显示在预览窗口中,但会显示在设备上

    e、 g.预览中没有阴影:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <solid android:color="@color/white" />
                <stroke
                    android:width="1dp"
                    android:color="@color/grey"/>
            </shape>
        </item>
    </selector>
    
    
    
    但这确实:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <solid android:color="@color/white" />
                <corners android:radius="1dp" />
                <stroke
                    android:width="1dp"
                    android:color="@color/grey"/>
            </shape>
        </item>
    </selector>
    

    还有一件事需要注意,如果清单中有这一行,阴影将不会显示:

    android:hardwareAccelerated=“false”


    我尝试了所有建议的内容,但它只在我删除该行时对我有效,我之所以有该行,是因为我的应用程序使用了大量位图图像,它们导致应用程序崩溃。

    如果你想拥有透明的背景并且
    android:outlineProvider=“bounds”
    不适合你,您可以创建自定义ViewOutlineProvider:

    class TransparentOutlineProvider extends ViewOutlineProvider {
    
        @Override
        public void getOutline(View view, Outline outline) {
            ViewOutlineProvider.BACKGROUND.getOutline(view, outline);
            Drawable background = view.getBackground();
            float outlineAlpha = background == null ? 0f : background.getAlpha()/255f;
            outline.setAlpha(outlineAlpha);
        }
    }
    
    并将此提供程序设置为透明视图:

    transparentView.setOutlineProvider(new TransparentOutlineProvider());
    
    资料来源:

    [编辑] getAlpha()的文档说明,它是特定于可绘制文件如何威胁alpha的。它可能总是返回255。 在这种情况下,您可以手动提供alpha:

    class TransparentOutlineProvider extends ViewOutlineProvider {
    
        private float mAlpha;
    
        public TransparentOutlineProvider(float alpha) {
            mAlpha = alpha;
        }
    
        @Override
        public void getOutline(View view, Outline outline) {
            ViewOutlineProvider.BACKGROUND.getOutline(view, outline);
            outline.setAlpha(mAlpha);
        }
    }
    
    如果视图背景为StateListDrawable,默认颜色为#DDFF0000,即alpha DDx0/FFx0==0.86

    transparentView.setOutlineProvider(new TransparentOutlineProvider(0.86f));
    

    有时像
    background=“@color/***”
    background=“#ff33**”
    不起作用,我将
    background\u color
    替换为drawable,然后它起作用如果要向按钮元素添加高程,则需要添加以下行:

    android:stateListAnimator="@null"
    

    请参见

    为适合我的布局添加背景色 比如:


    就我而言,字体是个问题

    1) 如果没有
    fontFamily
    ,我需要将
    android:background
    设置为某个值

    <TextView
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:background="@android:color/white"
        android:elevation="3dp"
        android:gravity="center"
        android:text="@string/fr_etalons_your_tickets"
        android:textAllCaps="true"
        android:textColor="@color/blue_4" />
    

    我有幸在父布局上设置了
    clipChildren=“false”

    如果您有一个没有背景的视图,这一行将帮助您

    android:outlineProvider="bounds"
    
    android:outlineProvider="bounds"
    
    android:outlineProvider="paddedBounds"
    
    如果您有一个背景视图,这一行将帮助您

    android:outlineProvider="bounds"
    
    android:outlineProvider="bounds"
    
    android:outlineProvider="paddedBounds"
    

    我花了一些时间研究它,最终意识到当背景是黑色时,阴影是不可见的

    在我的例子中,这是由自定义的父级造成的