-1

我需要制作一个中间有一个圆孔的矩形,像这样

在此处输入图像描述

4

1 回答 1

3

有不同的方法来获得它。
您可以使用材料组件库和MaterialShapeDrawable创建自定义形状路径(您需要版本1.1.0

例如,您可以执行以下操作:

LinearLayout linearLayout= findViewById(R.id.linear_rounded);
//Use the BottomAppBarTopEdgeTreatment to apply the bottom edge shape, or just create a custom class to obtain a similar shape
BottomAppBarTopEdgeTreatment bottomAppBarTopEdgeTreatment = new BottomAppBarTopEdgeTreatment(
    getResources().getDimension(R.dimen.margin),
    getResources().getDimension(R.dimen.rounded_corner),
    getResources().getDimension(R.dimen.vertical_offset)
);
bottomAppBarTopEdgeTreatment.setFabDiameter(getResources().getDimension(R.dimen.diameter));

ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
    .toBuilder()
    .setAllCorners(CornerFamily.ROUNDED,radius)
    .setBottomLeftCorner(CornerFamily.ROUNDED,0)  
    .setBottomRightCorner(CornerFamily.ROUNDED,0)
    .setBottomEdge(bottomAppBarTopEdgeTreatment)
    .build();
MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
ViewCompat.setBackground(linearLayout,shapeDrawable);

在此处输入图像描述

于 2020-01-15T10:42:30.067 回答