Android 5.0 android : elevation은 View에서 작동하지만 Button은 작동하지 않습니까?
SDK Manager의 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에서는 원에 그림자가 표시됩니다.
위젯 클래스를로 변경하고 Button
다른 모든 속성을 그대로두면 원의 그림자가 사라집니다.
질문 :
android:elevation
행동이 바뀌는 이유는 무엇 입니까? 두 경우 모두 동일한 배경이기 때문에 배경 때문일 수는 없습니다.Which classes support
android:elevation
, and which do not? For example, usingTextView
instead ofView
orButton
still gives us the drop shadow, so this change in behavior is not introduced at theTextView
level, but rather at theButton
level.As seen in this question from yesterday, how do we get
android:elevation
to be honored on aButton
? Is there someandroid:allowElevationToWorkAsDocumented="true"
value that we have to put in a theme or something?
The default Button style under Material has a StateListAnimator that controls the android:elevation
and android:translationZ
properties. You can remove the existing animator or set your own using the android:stateListAnimator
property.
<Button
...
android:stateListAnimator="@null" />
<Button
...
android:stateListAnimator="@anim/my_animator" />
The default animator is defined in button_state_list_anim_material.xml. Here is a sample showing the enabled and pressed states:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_enabled="true">
<set>
<objectAnimator android:propertyName="translationZ"
android:duration="@integer/button_pressed_animation_duration"
android:valueTo="@dimen/button_pressed_z_material"
android:valueType="floatType"/>
<objectAnimator android:propertyName="elevation"
android:duration="0"
android:valueTo="@dimen/button_elevation_material"
android:valueType="floatType"/>
</set>
</item>
<!-- base state -->
<item android:state_enabled="true">
<set>
<objectAnimator android:propertyName="translationZ"
android:duration="@integer/button_pressed_animation_duration"
android:valueTo="0"
android:startDelay="@integer/button_pressed_animation_delay"
android:valueType="floatType"/>
<objectAnimator android:propertyName="elevation"
android:duration="0"
android:valueTo="@dimen/button_elevation_material"
android:valueType="floatType" />
</set>
</item>
...
</selector>
In my experience with Appcompat v7 running on Lollipop device, Button
works with default features as ripple effect, elevation and z-animation on click, but misses them if is set a personalized android:background
property (as color or selector) in xml element.
This is because you are setting the background of the button manually which will replace all its effects.
As of the version 23.0.0 release of AppCompat, there is a new Widget.AppCompat.Button.Colored style which uses your theme's colorButtonNormal for the disabled color and colorAccent for the enabled color.
<Button
...
style="@style/Widget.AppCompat.Button.Colored" />
If you want different colors than specified you can create a new theme and apply it to the button via android:theme
. Then you can use this theme on all your buttons where you want same effect.
I had a similar problem i thought was due to wrongly inflated layouts, but it appeared that adding clipToPadding
did the trick. This must be set to the parent ViewGroup
containing the view you want to cast a shadow.
... android:clipToPadding="false" ...
This solution works for all the API versions of Android
Create a shadow @android:drawable/dialog_holo_light_frame
& if you want to customize the color of the background instead of white then create a layer-list background with customizable color on top of the shadow as shown below
Create a separate drawable file white_background_shadow.xml
<?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>
and use this drawable as the background like this
android:background="@drawable/shadow"
'programing tip' 카테고리의 다른 글
파이썬에서 클래스의 "불량"정의 (0) | 2020.11.20 |
---|---|
'mm / dd / yyyy'형식에서 루비 DateTime 구문 분석 (0) | 2020.11.20 |
Django REST Framework에서 CORS를 활성화하려면 어떻게해야합니까? (0) | 2020.11.20 |
인수에 대한 android () 메소드를 찾을 수 없습니다. (0) | 2020.11.20 |
mysql 저장 프로 시저에 대한 기본 매개 변수를 가질 수 있습니까? (0) | 2020.11.20 |