<item name="android:button",Android Material Design Button Styles

本文介绍了一种实现CardView按钮的方法,在Lollipop及更早版本中保持边框和凸起效果的同时,在Lollipop上实现涟漪效果,且没有明显的缺点。解决方案结合了多种答案,并提供了具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I tried a lot of answer & third party libs, but none was keeping the border and raised effect on pre-lollipop while having the ripple effect on lollipop without drawback. Here is my final solution combining several answers (border/raised are not well rendered on gifs due to grayscale color depth) :

Lollipop

01d5cf42c4b6f5d410b07bb3fc61a80d.gif

Pre-lollipop

XiATp.gif

build.gradle

compile 'com.android.support:cardview-v7:23.1.1'

layout.xml

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:card_view="http://schemas.android.com/apk/res-auto"

android:id="@+id/card"

card_view:cardElevation="2dp"

android:layout_width="match_parent"

android:layout_height="wrap_content"

card_view:cardMaxElevation="8dp"

android:layout_margin="6dp"

>

android:id="@+id/button"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_margin="0dp"

android:background="@drawable/btn_bg"

android:text="My button"/>

drawable-v21/btn_bg.xml

android:color="?attr/colorControlHighlight">

drawable/btn_bg.xml

Activity's onCreate

final CardView cardView = (CardView) findViewById(R.id.card);

final Button button = (Button) findViewById(R.id.button);

button.setOnTouchListener(new View.OnTouchListener() {

ObjectAnimator o1 = ObjectAnimator.ofFloat(cardView, "cardElevation", 2, 8)

.setDuration

(80);

ObjectAnimator o2 = ObjectAnimator.ofFloat(cardView, "cardElevation", 8, 2)

.setDuration

(80);

@Override

public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

o1.start();

break;

case MotionEvent.ACTION_CANCEL:

case MotionEvent.ACTION_UP:

o2.start();

break;

}

return false;

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值