Android 5.0 android:elevation 如果給button設定效果,androidelevation

來源:互聯網
上載者:User

Android 5.0 android:elevation 如果給button設定效果,androidelevation

看到android5.0版本中提供了  elevation  的功能,我在SDK Manager 下載了 Android 5.0  例子代碼,看了下  ElevationBasic 例子,展示了2個view對象,一個圓形一個矩形,圓形設定了 android:elevation  =  30dp,具體例子如下

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 <?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,移除掉按鈕的其他屬性,就不會顯示 elevation  效果,如下
預設主題的按鈕樣式中包含了  android:elevation 和 android:translationZ 屬性,你可以移除掉預設的屬性動畫,並設定你自己的android:stateListAnimator 屬性
如下代碼

123456789 <Button    ...      android:stateListAnimator="@null" /> <Button    ...      android:stateListAnimator="@anim/my_animator" />

 

預設的動畫(animator)在button_state_list_anim_material.xml定義。這裡是一個展示狀態下不可用,按鈕處於點擊狀態的例子

12345678910111213141516171819202122232425262728293031 <?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>    <!-- 基本狀態 -->    <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>

原文地址:http://www.itmmd.com/201412/240.html
該文章由 萌萌的IT人 整理髮布,轉載須標明出處。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.