Android Elevation 在 LinearLayout Android 中不起作用

如何解决Android Elevation 在 LinearLayout Android 中不起作用

我有线性布局,里面有回收站视图。我想要高程和拐角半径,但它不能正常工作。有人可以告诉我我做错了什么。

主布局

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:paddingStart="16dp"
    android:paddingEnd="16dp"
    android:clipToPadding="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

     <LinearLayout
        android:id="@+id/container"
        style="@style/WhiteBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/header">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

 </androidx.constraintlayout.widget.ConstraintLayout>

RecyclerView 项目布局

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipToPadding="false"
    android:paddingStart="12dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    tools:ignore="RtlSymmetry">

  .....more elements..

</androidx.constraintlayout.widget.ConstraintLayout>

样式

<style name="WhiteBox" >
    <item name="android:background">@drawable/white_Box_background</item>
    <item name="android:elevation">8dp</item>
</style> 

white_Box_background

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="7dp"/>
    <solid android:color="@android:color/white"/>
</shape>

下图显示我得到的,角落和阴影也没有正确显示

enter image description here

我想要什么,即预期结果

Expected Output

解决方法

我建议您改用卡片视图。

将依赖项添加到您的应用模块的 build.gradle

dependencies {
  implementation "androidx.cardview:cardview:1.0.0"
}

替换您当前的 LinearLayout 片段
<androidx.cardview.widget.CardView
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="14dp"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:orientation="vertical"
    app:cardCornerRadius="5dp"
    android:elevation="2dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/header">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</androidx.cardview.widget.CardView>

有关 CardView 的更多信息,请参阅 https://developer.android.com/guide/topics/ui/layout/cardview#kts

,

尝试将 android:clipToPadding="false" 添加到持有提升视图的父 ConstraintLayout。有关详细信息,请参阅此post 的已接受答案。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?