【问题标题】:MaterialTextView shapeAppearanceOverlay not workingMaterialTextView shapeAppearanceOverlay 不起作用
【发布时间】:2021-06-21 09:10:53
【问题描述】:

我正在尝试在两个特定的 MaterialTextView 上实现一个带有彩色背景的简单圆角布局。代码如下:

 <com.google.android.material.textview.MaterialTextView
    android:id="@+id/lblStartTime"
    style="@style/customTimeRangePickerStyle"
    android:layout_width="68dp"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:textAlignment="center"
    android:textSize="18sp" />

<style name="customTimeRangePickerStyle" parent="">
    <item name="shapeAppearanceOverlay">@style/customTimeRangePickerDay</item>
</style>

<style name="customTimeRangePickerDay" parent="">

    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">4dp</item>

    <item name="strokeWidth">1dp</item>
    <item name="strokeColor">@color/Cor2</item>

    <item name="android:background">@color/fieldBackground</item>
</style>

任何提示为什么这不起作用?生成的 textview 不显示边框或背景颜色。

谢谢!

【问题讨论】:

    标签: android material-design material-components-android android-shapedrawable


    【解决方案1】:

    目前 (1.3.0) MaterialTextView 不使用 MaterialShapeDrawable 并且不支持 ShapeAppearanceModelshapeAppearance/shapeAppearanceOverlay 属性。

    关于支持的组件的更多信息在 the doc

    但是你可以申请MaterialShapeDrawable:

          <com.google.android.material.textview.MaterialTextView
              android:id="@+id/textview"
              android:paddingLeft="8dp"
              android:gravity="center_vertical"
              android:backgroundTint="@color/white"
              android:text="Text"
              />
    

    和:

    val radius = resources.getDimension(R.dimen.default_corner_radius)
    
    val textView = findViewById<TextView>(R.id.textview)
    val shapeAppearanceModel = ShapeAppearanceModel()
        .toBuilder()
        .setAllCorners(CornerFamily.ROUNDED, radius)
        .build()
    
    val shapeDrawable = MaterialShapeDrawable(shapeAppearanceModel)
    shapeDrawable.setStroke(2.0f, ContextCompat.getColor(this,R.color.red600));
    ViewCompat.setBackground(textView, shapeDrawable)
    

    您还可以使用主题中定义的叠加层构建ShapeAppearanceModel。只需在上面的代码中使用:

     val shapeAppearanceModel =
                ShapeAppearanceModel.builder( context,
                        R.style.ShapeAppearance_MaterialComponents_SmallComponent,
                        R.style.shapeAppearanceOverlay)
                .build()
    

    与:

    <style name="shapeAppearanceOverlay">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">xxx</item>
    </style>
    

    【讨论】:

    • 谢谢加布里埃尔,我错过了这一点。不过,这并不能解决我当前的问题。我尝试叠加的原因是因为我需要能够更改背景可绘制形状以实现日/夜主题兼容性,并且不愿意为此创建两个不同的可绘制对象。有什么建议吗?
    • @IlianFelinto 我已经更新了答案。您可以使用样式中定义的shapeAppearanceOverlay 构建ShapeAppearanceModel。这样就可以在日/夜主题文件中定义了。
    • 我已将对象类型更改为 MaterialButton,现在我可以看到正确的形状。但它完全忽略了 strokeColor、strokeWidth 和 background(color) 项。另外,唯一的方法是编程,还是xml定义文件上的属性“样式”可以?
    猜你喜欢
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 2020-07-02
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多