How to make layout with rounded corners..?

后端 未结 18 3115
独厮守ぢ
独厮守ぢ 2020-11-22 09:37

How can I make a layout with rounded corners? I want to apply rounded corners to my LinearLayout.

18条回答
  •  醉梦人生
    2020-11-22 10:14

    With the Material Components Library you can use the MaterialShapeDrawable to draw custom shapes.

    Just put the LinearLayout in your xml layout:

    
    
        
    
    
    

    Then in your code you can apply a ShapeAppearanceModel. Something like:

    float radius = getResources().getDimension(R.dimen.default_corner_radius);
    
    LinearLayout linearLayout= findViewById(R.id.linear_rounded);
    ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
        .toBuilder()
        .setAllCorners(CornerFamily.ROUNDED,radius)
        .build();
    
    MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
    //Fill the LinearLayout with your color
    shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color.secondaryLightColor));
    
    
    ViewCompat.setBackground(linearLayout,shapeDrawable);
    

    Note:: it requires the version 1.1.0 of the material components library.

提交回复
热议问题