如何在Android上以编程方式更改Material Components中的颜色? [英] How to change color in Material Components PROGRAMMATICALLY on Android?

本文介绍了如何在Android上以编程方式更改Material Components中的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种更改某些Android Material Components布局对象的颜色的解决方案. 如何以编程方式更改材质按钮"中文本或边框的颜色?所有功能,例如MaterialButton.setTexColor()均不起作用.对于StrokeColor也是如此.必须直接从代码中进行逻辑上的更改. 没有XML样式,颜色必须能够根据从数据库中读取的值实时更改.

I'm looking for a solution to change the color of some Android Material Components layout objects. How can I change the color of text or borders in a Material Button programmatically? All functions like MaterialButton.setTexColor() do not work. Same thing for the StrokeColor. The change must be made logically, directly from code. No XML styles, colors must be able to change in live, based on values read from a database.

类似的东西不起作用:

//renewSetDate is a Material Button Object
renewSetDate.strokeColor = ColorStateList.valueOf(Color.RED)
renewSetDate.setTextColor(Color.WHITE)

这是材质"按钮的XML布局

This is the XML Layout for the Material Button

<com.google.android.material.button.MaterialButton
                        android:id="@+id/addSubs_btnSet_date"
                        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="15dp"
                        android:layout_marginTop="10dp"
                        android:layout_marginEnd="15dp"
                        android:text="Imposta data di rinnovo abbonamento"
                        android:textAlignment="center"
                        android:textColor="#FFFFFF"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintHorizontal_bias="0.0"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/addSubs_detailTitle"
                        app:strokeColor="@color/colorAccent" />

必须从数据库中保存的模板中读取颜色.我无法为每个模板创建XML样式.

Colors must be read from templates saved in a database. I can't create an XML Style for each of the template.

我发现仅由谁知道XML模板中的哪些属性来更改布局颜色真是令人讨厌.

I find it really annoying to have to change the layout colors only by who knows what attributes in an XML template.

这些是依赖项

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.viewpager2:viewpager2:1.0.0-rc01'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

推荐答案

不确定是否已完全回答.

Not sure if this was completely answered yet or not.

我遇到了同样的问题,我最终做了:

I was having the same issue and I ended up doing:

button.strokeColor = ColorStateList.valueOf(Color.RED)
button.strokeWidth = 1

那对我有用.

这篇关于如何在Android上以编程方式更改Material Components中的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆