AppCompatButton backgroundTint API < 21

你离开我真会死。 提交于 2019-11-29 02:52:40

Just use app:backgroundTint instead of android:backgroundTint, the tint will take effect below Lollipop. The reason is AppCompatActivity AppCompatDelegateImplV7 use AppCompatViewInflater to auto change Button or TextView to AppCompatButton or AppCompatTextView, then app:backgroundTint take effect.

Ripples are not available as a build in functionality on Android <21. This is due to performance issues: devices with the new API can use the RenderThread which is not available to older devices. See also: http://android-developers.blogspot.de/2014/10/appcompat-v21-material-design-for-pre.html

Why are there no ripples on pre-Lollipop? A lot of what allows RippleDrawable to run smoothly is Android 5.0’s new RenderThread. To optimize for performance on previous versions of Android, we've left RippleDrawable out for now.

To support ripple functionality below API 21 you may have to add a drawable in the background of your button:

<android.support.v7.widget.AppCompatButton
    android:id="@+id/add_remove_button"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/button_ripple"
    android:backgroundTint="@color/primary"
    android:textColor="@android:color/white"
    android:text="Remove" />

Then you have to add your xmls with the same name in both drawable and drawable-v21 directories (if you don't have them you may create and they will be automatically linked).

/res/drawable-v21/button_ripple.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/white">
    <item>
        <shape>
            <solid android:color="@color/white" />
        </shape>
    </item>
</ripple>

/res/drawable/button_ripple.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="@color/white" />
        </shape>
    </item>
</selector>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!