gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:57:59 25 4
gpt4 key购买 nike

我正在寻找一种解决方案来更改某些 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com