android - 更改设置为 ConstraintLayout 背景的 Nine-Patch 的颜色

标签 android android-layout android-view nine-patch android-constraintlayout

非常疯狂的问题:

我正在尝试更改设置为 ConstraintLayout 背景的 Nine-Patch 的颜色。 XML 中的 android:backgroundTint 工作正常,但这仅受 Android 5.0+ 支持。使用 app:backgroundTint 而不是 android:backgroundTint 不会改变颜色。

我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="29dp"
    android:background="@drawable/round"

    android:backgroundTint="@android:color/darker_gray">
</android.support.constraint.ConstraintLayout>

有人知道另一种改变颜色的方法吗? 感谢任何帮助。

最佳答案

我刚找到答案:

onCreate() 方法中:

View line = findViewById(R.id.line);
ColorStateList tint = ColorStateList.valueOf(ContextCompat.getColor(this, android.R.color.holo_blue_light));
ViewCompat.setBackgroundTintList(line, tint);

感谢@azizbekian 的建议。

关于android - 更改设置为 ConstraintLayout 背景的 Nine-Patch 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47004857/

相关文章:

android - 具有自定义 View 的 RadioButton Android

java - Android - 使自定义 View 的透明区域不可点击

android - Nexus 5x 反向风景传感器在 Android 相机预览应用程序中修复

android - 打开跟踪文件时出错 : No such file or directory (2)

android - 后台服务需要在服务器上发送 GPS 位置

android - Kotlin 的 "@Parcelize"是否适用于非数据类?

android - 在操作栏中移动图像按钮

java - 确定 Android 上的可用内存

android - 单击按钮时更改 fragment

android - 是否可以在 View 上设置背景 png 并将其锚定到顶部而不在 Android 中缩放?