android-studio - 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,颜色会发生变化,但边框会消失。哪个声明会保留边界?
解决方案
我解决了
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>
这有效(对我来说)
推荐阅读
- python - 在numpy,Python的ndarray中找到最接近的值X
- java - 为什么当我从该类创建一个新对象时,我的类变量似乎被覆盖了?
- java - Spring boot applciation中何时清除SecurityContext?即使在 api 调用完成后它也会被保留
- python - 如何在 Tkinter、Python 中退出命令?
- android - 谷歌地图使用该位置的权限失败
- php - 在 WooCommerce 中为产品添加自定义设置选项卡,允许显示 iframe
- android - 为什么我不能使用 kotlin 语言将数据插入到我的数据库文件中?
- ios - 当应用程序处于后台或设备锁定时,Airplay 停止播放视频流
- mysql - 在 laravel 查询 bulider 中使用 lat 和 long 查找最近的位置
- postgresql - 如何在 jBPM 默认应用程序中从 H2 数据库切换到 Postgres?