MaterialTextView shapeAppearanceOverlay 不起作用

问题描述

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

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

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

谢谢!

解决方法

目前 (1.3.0) MaterialTextView 不使用 MaterialShapeDrawable 并且不支持 ShapeAppearanceModel 和 {{1} } 属性。

有关 the doc 中支持的组件的更多信息。

但是您可以应用shapeAppearance/shapeAppearanceOverlay

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"
          />

enter image description here

您还可以使用主题中定义的叠加层构建 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()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...