Android适配 - 去除5.0以后的Button阴影

简单一个Button按钮,在Android 5.0之前以及之后有了默认样式的改变,遇到UI设计师曾经要求将Android 5.0之后Button样式出现的默认阴影去掉。本篇略简单的记录Android 5.0去掉Button阴影的方法。

实现的效果如下:

索尼xm50t(4.3) 谷歌N6(5.0)
锤子坚果pro(7.1.1) HUAWEI Mate8

适配方法

直接在Button加上去除阴影的属性样式

<Button
    android:layout_margin="16dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="?android:attr/borderlessButtonStyle"//注意这句
    android:text="不带阴影样式"
    android:textColor="@android:color/white"
    android:background="@drawable/btn_main_selector"/>

自定义style,继承Widget.AppCompat.Button.Borderless

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <!-- 5.0 以后Button去除阴影-->
    <style name="BorderLessButton" parent="Widget.AppCompat.Button.Borderless">

    </style>

</resources>

在Button中使用这个自定义的style

<Button
    android:layout_margin="16dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="不带阴影样式"
    android:textColor="@android:color/white"
    style="@style/BorderLessButton"
    android:background="@drawable/btn_main_selector"/>

猜你喜欢

转载自blog.csdn.net/scau_zhangpeng/article/details/79046148