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

问题描述

我有三个 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);

This is the text view whose background and text color I want to change

After I click on it background color changes but goes outside of shape

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

解决方法

您定义了 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 上这样做。

结果:

enter image description here

,

你可以使用不同的东西:

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

enter image description here

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

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

enter image description here

相关问答

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