海拔在ContraintLayout中不起作用

时间:2018-11-01 12:21:16

标签: android

我不知道为什么android:elevation无法正常工作

我的代码:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".UI.Activity.ChatActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:elevation="2dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_default="percent"
        app:layout_constraintHeight_percent="0.07"
        app:layout_constraintStart_toStartOf="parent"
        android:gravity="center_vertical">

        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="8"
            android:inputType="text"
            android:layout_marginLeft="15dp"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="5dp"
            android:background="@drawable/border_edit_text_messenger"
            android:textCursorDrawable="@color/colorWhite"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"/>

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.8"
            android:src="@drawable/ic_send"
            android:scaleType="centerInside"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"/>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

3 个答案:

答案 0 :(得分:3)

只有在您要在其中设置高程的ViewGroupView设置任何背景颜色后,高程才能正常工作。

设置

android:background="@color/colorWhite"
android:elevation="2dp"

转到上述xml中的LinearLayout。

答案 1 :(得分:2)

这是默认行为,阴影是由某些背景标识的,因此对于没有背景,将没有阴影

  • 为视图设置背景。
  • 如果您的视图没有背景。那么您可以设置android:outlineProvider="bounds"

建议:

以上两种解决方案均适用于> 21的Android版本。

所有版本的解决方案

您可以在视图上放置可绘制的阴影。

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_default="percent"
    app:layout_constraintHeight_percent="0.07"
    app:layout_constraintStart_toStartOf="parent"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        >

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:background="@drawable/shadow"/>

</FrameLayout>

showdow.xml 放入可绘制对象

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <gradient
        android:angle="-90"
        android:endColor="#33000000"
        android:startColor="@android:color/transparent"/>

</shape>

output

您可以找到许多添加阴影here的方法。

答案 2 :(得分:0)

希望这是有用的,不幸的是,先前的解决方案不适用于我的情况:我发现类似的材质卡问题,如果在ConstraintLayout(或不包含)内不显示高程或阴影;我已经解决了两个问题,即通过对cardElevation以外的layout_margin属性进行赋值,并使其值接近cardElevation,并且使后者不要过高以避免裁剪。

android:layout_margin="5dp"
app:cardElevation="8dp"

我在ConstraintLayout here内有材料卡的要旨,而没有here

让我知道它是否也对您有用。