简体   繁体   English

在布局底部创建一个三角形

[英]Creating a triangle shape at the bottom of layout

I'm working on an android application that contains a shape at the top of the activity and I'm trying to implement it but struggling to do it.我正在开发一个在 Activity 顶部包含一个形状的 android 应用程序,我正在尝试实现它,但正在努力实现它。 绿色的形状

I have tried to create a drawable file that create a triangle shape and sets up the bottom corner radius to match the shape above but not working.我试图创建一个可绘制的文件,该文件创建一个三角形并设置底角半径以匹配上面的形状但不起作用。 anyone can help me, please.任何人都可以帮助我,请。

You can use the EdgeTreatment included in the official Material Components Library .您可以使用官方材料组件库中包含的EdgeTreatment

Just extend the EdgeTreatment with something like:只需使用以下内容扩展EdgeTreatment

public class MyTriangleEdge extends EdgeTreatment {

  private final float size;
  private final boolean inside;

  public MyTriangleEdge(float size, boolean inside) {
    this.size = size;
    this.inside = inside;
  }

  @Override
  public void getEdgePath(
      float length, float center, float interpolation, @NonNull ShapePath shapePath) {
    shapePath.lineTo(0, 0);
    shapePath.lineTo(center, inside ? size  : -size );
    shapePath.lineTo(length, 0);
  }

and then apply it:然后应用它:

MyTriangleEdge edgeTreatment = new MyTriangleEdge(height,false);

LinearLayout linearLayout= findViewById(R.id.xxxx);
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
    .toBuilder()
    .setBottomEdge(edgeTreatment)
    .build();

MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);

ViewCompat.setBackground(linearLayout,shapeDrawable);

在此处输入图片说明

Also for edge treatments, the parent view must disable clipping of children by setting android:clipChildren="false" in xml.同样对于边缘处理,父视图必须通过在 xml 中设置android:clipChildren="false"来禁用子视图的剪辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM