单击文本视图时,根据文本视图形状更改文本视图的背景颜色

change the background color of textview according to textview shape when click on text view

我有三个Material个文本视图,我想根据文本视图的形状改变文本视图的背景颜色。我尝试了多个选项,但它超出了文本视图形状。我有圆角文本视图,但颜色填充像一个简单的矩形。

 <com.google.android.material.textview.MaterialTextView
        android:id="@+id/textViewLoseWeight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="65dp"
        android:text="Lose Weight"
        android:textColor="@color/purple_500"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textViewGoals"
        tools:ignore="MissingConstraints" />

    <com.google.android.material.textview.MaterialTextView
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_marginTop="4dp"
        android:id="@+id/textViewLoseWeightSubtitle"
        android:background="@drawable/textview_outline_style"
        android:paddingStart="16dp"
        android:paddingTop="16dp"
        android:text="Get leaner and improve Your fitness"
        android:textColor="@color/purple_500"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textViewLoseWeight" />



 //.java
    View.OnClickListener listener = new View.OnClickListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.textViewLoseWeightSubtitle:
                        mLoseWgt.setBackgroundColor(Color.parseColor("#363C60"));
                        mLoseWgt.setTextColor(Color.WHITE);

点击文本视图后,如上变化。我想要像原始文本视图那样的圆角形状内的背景颜色

您定义了您的 TextView 以及它的 android:background 属性:

 <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Get leaner and improve Your fitness"
            android:background="@drawable/round_fill_color"
            android:layout_centerInParent="true"
            android:textColor="#ffffff"
            android:padding="10dp"/>
    
    </RelativeLayout>

这里提到的新属性 drawable 称为 round_fill_color.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#363c60"/>
    <stroke android:width="3dp" android:color="#ffffff" />
    <corners android:radius="30dp"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>

这是为单个 widgets 创建自定义属性的建议方法。

如果你想为所有定义一个 layout,我建议在 styles.xmlthemes 上定义一个 layout

结果:

您可以使用不同的东西:

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/textview"
             ../>

然后应用 MaterialShapeDrawable

    val radius = resources.getDimension(R.dimen.default_corner_radius)

    val shapeAppearanceModel = ShapeAppearanceModel()
        .toBuilder()
        .setAllCorners(CornerFamily.ROUNDED, radius)
        .build()

    val shapeDrawable = MaterialShapeDrawable(shapeAppearanceModel)
    shapeDrawable.fillColor = ContextCompat.getColorStateList(this,R.color.xxx)
    shapeDrawable.setStroke(2.0f, ContextCompat.getColor(this,R.color.xx));
    ViewCompat.setBackground(textView, shapeDrawable)

使用此 ShapeAppearanceModel 获得 50% 的圆角:

val shapeAppearanceModel = ShapeAppearanceModel()
    .toBuilder()
    .setAllCorners(RoundedCornerTreatment())
    .setAllCornerSizes(RelativeCornerSize(0.5f))
    .build()