【问题标题】:Creating a triangle shape at the bottom of layout在布局底部创建一个三角形
【发布时间】:2020-01-28 07:54:48
【问题描述】:

我正在开发一个在 Activity 顶部包含一个形状的 android 应用程序,我正在尝试实现它,但很难做到。

我尝试创建一个可绘制文件,该文件创建一个三角形并设置底角半径以匹配上面的形状但不起作用。任何人都可以帮助我,拜托。

【问题讨论】:

    标签: java android android-drawable material-components-android material-components


    【解决方案1】:

    您可以使用官方Material Components Library中包含的EdgeTreatment

    只需将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);
      }
    

    然后应用它:

    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);
    

    同样对于边缘处理,父视图必须通过在 xml 中设置 android:clipChildren="false" 来禁用子视图的剪辑。

    【讨论】:

    • 非常感谢回复,但是toBuilder()方法无法解析。
    • @user8545027 你必须至少使用1.1.0版本(目前是1.1.0-rc02)
    猜你喜欢
    • 1970-01-01
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多