【问题标题】:What's wrong with this layout that's causing the elevation shadow not to appear?这种导致高程阴影不出现的布局有什么问题?
【发布时间】:2017-12-08 14:46:13
【问题描述】:

我正在尝试使用 Android Elevation 来实现视图的“阴影”外观。以下是布局xml:

<android.support.constraint.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="match_parent"
    tools:context="com.sample.app.Main2Activity">

    <LinearLayout
        android:layout_width="395dp"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:orientation="vertical"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:clipChildren="false">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="165dp"
            android:background="#FFFFFF"
            android:padding="30dp"
            android:layout_margin="30dp"
            android:elevation="10dp"
            android:text="TextView" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

这是布局的外观: Look ma, no shadows!

有解决此问题的建议吗?

【问题讨论】:

  • 测试设备上的android是什么?
  • 海拔在我发布的代码中。在发布之前,我已经浏览了其他材料。背景设置为没有 alpha 值,因为我发现其他人在渲染阴影时也遇到了问题。 @mac229 Nexus,API 25。另一个设备是 API 21。
  • @redflour 通常这很奇怪,因为我在 API 级别 23 和 26 上测试了您的代码并且我有阴影
  • 好像和背景元素的android:cornerRadius有关。当that的值设置为0dp,或者标签根本不存在时,不会渲染阴影。

标签: android android-layout material-design android-elevation


【解决方案1】:

给你……也许你需要有人为你做这件事……

我改用了卡片视图...

xml 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        card_view:cardCornerRadius="5dp"
        card_view:contentPadding="10dp">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView
                android:id="@+id/textView"
                android:text="hello"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:textStyle="bold"/>

            <TextView
                android:id="@+id/textView2"
                android:text="world"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textView"
                android:layout_marginTop="10dp"/>
        </RelativeLayout>
    </android.support.v7.widget.CardView>


</LinearLayout>

将这些添加到您的应用 gradle 文件中

compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:cardview-v7:25.1.0'

结果:

如果你不想使用卡片视图.....

xml -> 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/border"
            android:layout_margin="5dp"
            android:padding="5dp">

            <TextView
                android:id="@+id/textView"
                android:text="hello"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:textStyle="bold"/>

            <TextView
                android:id="@+id/textView2"
                android:text="world"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textView"
                android:layout_marginTop="10dp"/>
        </RelativeLayout>


</LinearLayout>

@drawable/边框

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

    <!-- Drop Shadow Stack -->
    <item>
        <shape>
            <gradient
                android:centerColor="#D5E2ED"
                android:endColor="#0000"
                android:angle="90"/>
        </shape>
    </item>

    <item android:bottom="10dp">
        <shape>
            <solid android:color="#D5E2ED"/>
        </shape>
    </item>

    <!-- base background -->
    <item android:bottom="5dp" android:top="1dp">
        <shape android:shape="rectangle">
            <solid android:color="#fff"/>
            <padding android:bottom="10dp" android:top="10dp"/>
        </shape>
    </item>

</layer-list>

结果:

愉快的编码......

【讨论】:

  • 使用卡片视图的好技巧,但我正在寻找一种使用 ConstraintLayout 的方法。只有在我尝试了一个简单的 TextView 之后,我才发现我的问题根本不是因为 ConstraintLayout。
【解决方案2】:

我会得到答案,但首先是一些背景知识。我试图通过使用 ConstraintLayout 的自定义视图来实现外观。此视图将在另一个布局中使用。当我尝试使用一个简单的 TextView 时,我意识到阴影没有被渲染,因为这破坏了我认为它与 ConstraintLayout 相关的假设。

什么有效? 我从文档中知道,阴影和渲染是使用 View 的背景。这个,我有,事实上,它是一种没有 alpha 值的纯色(正如 SO 上的其他人所提到的)。 很像这样:

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

这是在我添加 android:radius 之后,然后渲染阴影。

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

如果您想知道,为什么不将其设置为 0dp?好吧,当半径值设置为 0dp 时,阴影又消失了。

【讨论】:

    猜你喜欢
    • 2013-02-04
    • 1970-01-01
    • 2021-11-28
    • 2016-01-17
    • 2016-08-22
    • 2015-09-22
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多