android - 如何在 Android 上以编程方式更改 Material Components 中的颜色?

标签 android android-layout android-button android-design-library material-components-android

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

像这样的事情是行不通的:

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

这是 Material 按钮的 XML 布局

<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 样式。

我发现只有知道 XML 模板中的属性的人才能更改布局颜色真的很烦人。

这些是依赖项

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'

最佳答案

不确定是否已经完全回答了这个问题。

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

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

这对我有用。

关于android - 如何在 Android 上以编程方式更改 Material Components 中的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58790816/

相关文章:

android - 如何通过蓝牙连接从 Android 应用程序在 Raspberry Pi 上运行脚本

java - 获取两点之间的坐标

Android 持久 Bottom Sheet 初始可见性

android - 无法使用安卓 :background with button from the new material components

Android Fragment和逻辑应用设计

Android:亚行拒绝连接到客户端

带有 XML 的 Android 按钮样式背景

Android在显示软键盘时调整布局的一部分

android - 使用按钮显示/隐藏文本

android - 如何在Android中实现这种布局?