【问题标题】:Elevation is not working in ContraintLayout高程在 ConstraintLayout 中不起作用
【发布时间】:2019-04-05 16:07:42
【问题描述】:

我不知道为什么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>

【问题讨论】:

  • 你的 compile sdk 版本是多少?海拔适用于 >21 个版本。
  • 在 SDK 版本 28 上
  • 您是否已经在您的应用程序中使用android.support.design 库?不使用 ConstraintLayout 也会显示高程吗?
  • 是的,我做到了implementation 'com.android.support:design:28.0.0'
  • 我尝试了RelativeLayout,但还是不行

标签: android


【解决方案1】:

在您将任何背景颜色设置为要在其中设置高度的ViewGroupView 之前,高度不起作用。

设置

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

到您上面的 xml 中的 LinearLayout。

【讨论】:

    【解决方案2】:

    这是默认行为,阴影是被某些背景识别,所以NO background会有NO shadow

    • 为您的视图设置背景。
    • 如果您的视图没有背景。那么你可以设置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放入drawable

    <?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>
    

    你可以找到很多方法来添加阴影here

    【讨论】:

      【解决方案3】:

      希望这可能有用,不幸的是,以前的解决方案不适用于我的情况:我发现了一个类似的问题,材质卡在约束布局内(或不在)内不显示高程或阴影;我已经解决了这两个问题,方法是评估除 cardElevation 之外的 layout_margin 属性,并使用接近 cardElevation 的值,并使后者不要太高以避免剪裁。

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

      我在 ConstraintLayout here 和没有它 here 中的材质卡上有一个要点

      让我知道它是否也适合你。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        相关资源
        最近更新 更多