按钮样式api 21

时间:2015-03-13 11:16:33

标签: android button styles

我在api 21上看到按钮的新动画: button animation api21 android

但是我想用蓝色做这个,我在另一个问题上看到在buttonstyle.xml中使用这个自定义样式(在Drawables文件夹上):

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?attr/colorControlHighlight">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <corners android:radius="2dp" />
            <solid android:color="@color/Blue" />
        </shape>
    </item>
</ripple>

但是我的min API是14并且它崩溃因为我需要min 21来使用ripple,如果用户有android 5可以使用蓝色显示此动画并且如果用户有较低版本则显示正常动画还是蓝色的?

谢谢!

1 个答案:

答案 0 :(得分:0)

使用https://github.com/ozodrukh/RippleDrawable库!

buttonstyle.xml:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/Blue" >
    <!-- @color/Blue is ripple color -->
    <!-- item(id=mask): image which will be filled by ripple -->
    <item 
        android:id="@android:id/mask">
        <!-- default button background image -->
        <inset
            android:insetLeft="@dimen/abc_button_inset_horizontal_material"
            android:insetTop="@dimen/abc_button_inset_vertical_material"
            android:insetRight="@dimen/abc_button_inset_horizontal_material"
            android:insetBottom="@dimen/abc_button_inset_vertical_material">
            <shape android:shape="rectangle">
                <corners android:radius="@dimen/abc_control_corner_material" />
                <!-- e0e0e0 is default backround color -->
                <!-- this solid element can be removed, but i dont test this -->
                <solid android:color="#e0e0e0" />
                <padding android:left="@dimen/abc_button_padding_horizontal_material"
                    android:top="@dimen/abc_button_padding_vertical_material"
                    android:right="@dimen/abc_button_padding_horizontal_material"
                    android:bottom="@dimen/abc_button_padding_vertical_material" />
            </shape>
        </inset>
    </item>
    <!-- item(none): default button background -->
    <item>
        <!-- default button background image -->
        <inset
            android:insetLeft="@dimen/abc_button_inset_horizontal_material"
            android:insetTop="@dimen/abc_button_inset_vertical_material"
            android:insetRight="@dimen/abc_button_inset_horizontal_material"
            android:insetBottom="@dimen/abc_button_inset_vertical_material">
            <shape android:shape="rectangle">
                <corners android:radius="@dimen/abc_control_corner_material" />
                <!-- e0e0e0 is default backround color -->
                <solid android:color="#e0e0e0" />
                <padding android:left="@dimen/abc_button_padding_horizontal_material"
                    android:top="@dimen/abc_button_padding_vertical_material"
                    android:right="@dimen/abc_button_padding_horizontal_material"
                    android:bottom="@dimen/abc_button_padding_vertical_material" />
            </shape>
        </inset>
    </item>
</ripple>

活动:

//button is your button
button.setBackgroundDrawable(LollipopDrawablesCompat.getDrawable(getResources(), R.drawable.buttonstyle, getTheme()));
button.setOnTouchListener(new DrawableHotspotTouch((LollipopDrawable) button.getBackground()));

有效! https://i.stack.imgur.com/pzI04.png