首页 > 解决方案 > 在 Kotlin 中以编程方式更改可绘制的描边颜色 - Android Studio

问题描述

我正在尝试更改用作背景的 Drawable 笔划的颜色。

这是我的可绘制对象:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <solid android:color="@color/colorPrimary" />
            </shape>
        </item>

        <item
            android:id="@+id/item_border_drawable"
            android:left="16dp"
            android:right="16dp"
            android:top="16dp"
            android:bottom="16dp">
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="rectangle">
                <!-- This is the stroke you want to define -->
                <stroke android:width="3dp"
                    android:color="@color/itemLegendary"/>

                <!-- Optional, round your corners -->
                <corners android:bottomLeftRadius="0dp"
                    android:topLeftRadius="5dp"
                    android:bottomRightRadius="5dp"
                    android:topRightRadius="0dp" />
            </shape>
        </item>
</layer-list>

如您所见,我使用一个项目 Rectangle 来获得背景颜色,然后在顶部使用另一个矩形,在它周围绘制一个边框,但周围有 16dp 的边距。

我想在 MainActivity 中更改笔触的颜色,而不影响填充和背景颜色。

我尝试按照此处提到的方式进行操作: Android以编程方式更改颜色描边(边框) 但是它似乎没有“更改颜色”而是替换了整个Drawable,我不想提供填充等信息。 。 再次。

你们能帮我轻松更改边框的颜色吗?

问候。

标签: androidkotlinborderdrawable

解决方案


推荐阅读