在SDK管理器中的Android 5.0示例中,有一个名为ElevationBasic
的示例。它展示了两个View
对象:一个圆形和一个正方形。圆形的android:elevation
设置为30dp
:
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2014 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<FrameLayout 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">
<View
android:id="@+id/floating_shape"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginRight="40dp"
android:background="@drawable/shape"
android:elevation="30dp"
android:layout_gravity="center"/>
<View
android:id="@+id/floating_shape_2"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="25dp"
android:background="@drawable/shape2"
android:layout_gravity="center"/>
</FrameLayout>
在 Nexus 9 上,运行原始样例代码时,圆圈会出现一个阴影效果:
![ElevationBasic, As Originally Written](https://istack.dev59.com/3pJaD.webp)
![ElevationBasic, Using a Button](https://istack.dev59.com/gQv13.webp)
1. 为什么 `android:elevation` 的行为会发生变化?它不能是由于背景而导致的,因为在两种情况下背景都相同。 2. 哪些类支持 `android:elevation`,哪些不支持?例如,将 `View` 或 `Button` 替换为 `TextView` 仍然会给我们带来阴影效果,因此行为上的这种变化并没有在 `TextView` 级别引入,而是在 `Button` 级别引入的。 3. 如昨天的 这个问题中所见,如何在 `Button` 上使用 `android:elevation`?是否需要在主题或其他某些地方添加 `android:allowElevationToWorkAsDocumented="true"` 属性值?