How to mimic the Material-design raised button style, even for pre-Lollipop (minus the special effects)?

后端 未结 7 1930
别跟我提以往
别跟我提以往 2020-12-07 16:54

Google has shown some nice ways that buttons are shown on Lollipop here.

I\'m talking about both the raised and the flat buttons.

How can I mimic them on pre

7条回答
  •  粉色の甜心
    2020-12-07 17:34

    This one is hacky according to Yigit (google dev on cardview) but does a descent job.

    You can use negative numbers in order to get rid of the padding and margin inside of CardView.

    Tested on api 15 nexus one and nexus 4 emulators. This solution will work on pre lollipop.

    
        

    Code:

            final CardView cardView = (CardView) rootView.findViewById(R.id.elevated_button_card);
            final Button button = (Button) rootView.findViewById(R.id.elevated_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;
                }
            });
    

提交回复
热议问题