Android CardView remove padding

后端 未结 4 1136
走了就别回头了
走了就别回头了 2020-12-02 22:39

how do I get rid of this strange padding in the layout below:

\"enter

         


        
相关标签:
4条回答
  • 2020-12-02 22:59

    UDPATE

    Well, seems there is a much easier way to do it, without guessing the padding and all:

    card_view:cardPreventCornerOverlap="false"
    

    or using java:

    cardView.setPreventCornerOverlap(false)
    

    You can read all about it here.

    ORIGINAL ANSWER

    It is an intentional padding to avoid content from bleeding off the rounded corner of the card in pre lollipop versions (since clipping is not available). If you want to get rid of it all together you may use a negative padding in pre-lollipop versions for the contentPadding attribute of the CardView such as:

    card_view:contentPadding="-8dp"
    

    or using java:

    int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
    cardView.setContentPadding(-padding,-padding,-padding,-padding);
    

    I'm not particularly sure which value will work seamlessly, you'll need to experiment and see for yourself.

    0 讨论(0)
  • 2020-12-02 23:00

    Please remove this from your code -

    card_view:cardUseCompatPadding="true"
    
    0 讨论(0)
  • 2020-12-02 23:07

    Got the same problem and this worked for me.

    app:cardPreventCornerOverlap="false"
    

    For device with API < 21, I had to make a custom ImageView class, override the onDraw to draw the round corners.

    @Override
    protected void onDraw(Canvas canvas) {
        float radius = getContext().getResources().getDimension(R.dimen.card_corner_radius);
        Path path = new Path();
        RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
        path.addRoundRect(rect, radius, radius, Path.Direction.CW);
        canvas.clipPath(path);
        super.onDraw(canvas);
    }
    
    0 讨论(0)
  • 2020-12-02 23:08

    use this:

    cardView.setPadding(0,0,0,0);
    cardView.setUseCompatPadding(true);
    cardView.setContentPadding(-6,-6,-6,-6);
    cardView.setPreventCornerOverlap(false);
    
    0 讨论(0)
提交回复
热议问题