【问题标题】:Android Elevation not working in LinearLayout AndroidAndroid Elevation 在 LinearLayout Android 中不起作用
【发布时间】:2021-08-31 12:56:21
【问题描述】:

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

主布局

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

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

我想要什么,即预期结果

【问题讨论】:

    标签: android kotlin material-design shadow android-elevation


    【解决方案1】:

    我建议你改用卡片视图。

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

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

    将您当前的 LinearLayout sn-p 替换为

    <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

    【讨论】:

    • 我改为 cardview 但也不能正常工作。我尝试了 cardUseCompatPadding 它可以工作,但在 startend 侧添加了一些边距。它看起来很丑。 @Tajwane
    【解决方案2】:

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

    【讨论】:

    • 我已经添加了 android:clipToPadding="false 但没有任何效果 @ramona.cristea
    猜你喜欢
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多