gpt4 book ai didi

android - 高程阴影不会仅在预览时显示在设备上

转载 作者:太空狗 更新时间:2023-10-29 13:01:55 29 4
gpt4 key购买 nike

我正在尝试对我的布局实现投影效果,并决定使用高程选项。

但出于某种原因,我最终在 Android Studio 预览中得到的内容并没有显示在设备上。我将附上 Android Studio 预览和设备的屏幕截图。我会提供我使用过的代码。

[ 1] enter image description here

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#90000000" />
<corners android:radius="10dp" />
</shape>


<RelativeLayout
android:outlineProvider="bounds"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center"
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="@drawable/button_shadow"
android:elevation="30dp"
/>

</RelativeLayout>

最佳答案

elevation 属性仅在 Android 5.0+ 上受支持。所以我认为您正在旧设备上运行您的应用程序。

这是一个解决方案,您可以使用 CardView 作为 RelativeLayout 的替代方案。

在你的 build.gradle()app

添加依赖
compile 'com.android.support:cardview-v7:26.0.0'

然后像这样在你的xml文件中使用它

<android.support.v7.widget.CardView
android:id="@+id/media_card_view"
android:layout_width="300dp"
android:layout_height="300dp"
card_view:cardBackgroundColor="@android:color/white"
card_view:cardElevation="30dp"
card_view:cardUseCompatPadding="true">
...
</android.support.v7.widget.CardView>

或者您可以使用LayerList 制作您自己的阴影,创建一个名为shadow.xml 的文件并将其放置在您的Drawable 文件夹中

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--the shadow comes from here-->
<item
android:bottom="0dp"
android:drawable="@android:drawable/dialog_holo_light_frame"
android:left="0dp"
android:right="0dp"
android:top="0dp">

</item>

<item
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp">
<!--whatever you want in the background, here i preferred solid white -->
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />

</shape>
</item>
</layer-list>

然后你像这样将它分配给你的 View

android:background="@drawable/shadow"

关于android - 高程阴影不会仅在预览时显示在设备上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55540701/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com