CardView with different corner radius

前端 未结 4 1045
小鲜肉
小鲜肉 2020-12-30 00:13

I have the following CardView and I want to set different radius for each corner in the card. Is it possible to change them by XML or programmaticaly? Thanks in advance.

4条回答
  •  长情又很酷
    2020-12-30 00:51

    It requires the official MaterialCardView (which extends the androidx.cardview.widget.CardView) and the version 1.1.0 of the Material components library.

    Add to your layout the MaterialCardView:

        
    
        
    

    Define a custom style inheriting a material card style (for example Widget.MaterialComponents.CardView) and use the shapeAppearanceOverlay attribute:

      
    
    
      
    

    You can also achieve it programmatically.
    Just apply a custom ShapeAppearanceModel to the corners of the card.
    Something like:

    float radius = getResources().getDimension(R.dimen.my_corner_radius);
    cardView.setShapeAppearanceModel(
      cardView.getShapeAppearanceModel()
          .toBuilder()
          .setTopLeftCorner(CornerFamily.ROUNDED,..)
          .setTopRightCorner(CornerFamily.ROUNDED,..)
          .setBottomRightCorner(CornerFamily.ROUNDED,radius)
          .setBottomLeftCornerSize(0)
          .build());
    

    Note: it requires the version 1.1.0 of the library. Currently:

    implementation 'com.google.android.material:material:1.1.0'
    

提交回复
热议问题