【问题标题】:Android four sided cutted cornerAndroid 四边切角
【发布时间】:2020-10-19 12:31:00
【问题描述】:

我想在 android 中获得一个带有四边切角的可绘制形状,如下所示:

【问题讨论】:

  • 是按钮吗?一个文本视图?
  • textView 与背景@GabrieleMariotti

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


【解决方案1】:

通过材质组件库,您可以使用MaterialShapeDrawabledraw custom shapes

使用 TextView 您可以:

    <TextView
        android:id="@+id/textview"
        android:backgroundTint="@color/...."
        android:layout_height="48dp"
        android:gravity="center"
        ../>

然后创建MaterialShapeDrawable:

TextView textView = findViewById(R.id.textview);
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
        .toBuilder()
        .setAllCorners(new CutCornerTreatment()).setAllCornerSizes(new RelativeCornerSize(0.5f))
        .build();

MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
//Set the stroke
shapeDrawable.setStroke(2.0f,
            ContextCompat.getColorStateList(this,R.color.colorSecondaryDark));
//Apply the shapeDrawable as background in the TextView  
ViewCompat.setBackground(textView,shapeDrawable);

只是关于new RelativeCornerSize(0.5f) 的注释:它在1.2.0-beta01 中发生了变化。之前是new RelativeCornerSize(50)

【讨论】:

  • 有什么方法可以通过drawable@Gabriele Mariotti使用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-09
  • 2022-01-24
  • 2017-04-29
  • 2020-07-11
相关资源
最近更新 更多