首页 > 解决方案 > Android Studio 4.1 边框按钮在 kotlin 代码中丢失边框设置背景颜色

问题描述

在这个论坛中有很多创建带边框按钮的主题。例如,它与此 xml 文件完美搭配

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
        <solid android:color="#FFEB3B" />`<!--Background Color-->`
        <stroke android:width="2dip" android:color="#000000"/> `<!--Border-->`
    </shape>

我失败的地方如下:

我需要 2 个带边框的按钮,一个是黄色的,另一个是蓝色的。由于程序逻辑我想切换颜色,黄色的变成蓝色,蓝色的变成黄色。

在 xml 文件中

<solid android:color="#FFEB3B" />`<!--Background Color-->`

用来。

在科特林我只发现

button.setBackgroundColor(Color.YELLOW)

但我也找不到 button.setColor 或 button.setdologColor 或其他任何东西。

使用 setBackgroundColor,颜色会发生变化,但边框会消失。哪个声明会保留边界?

标签: android-studiokotlinbuttonborder

解决方案


我解决了

punktelinks.setBackgroundResource(R.drawable.shapecolory)

在代码和资源中

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFFF00" />`<!--Background Color-->`
    <stroke android:width="2dip" android:color="#000000"/> `<!--Border-->`
</shape>

这有效(对我来说)


推荐阅读