android - 在 Button 上以编程方式设置 selectableItemBackground。安卓。科特林
问题描述
我有这个代码,但它不工作。
val background = TypedValue()
context!!.theme.resolveAttribute(android.R.attr.selectableItemBackground, background, true)
button.setBackground(background.resourceId)
错误:类型不匹配;找到:整数;必需:可绘制。
解决方案
你必须使用setBackgroundResource(int id)
. 因为setBackground(Drawable d)
接受一个Drawable
对象。
val background = TypedValue()
context!!.theme.resolveAttribute(android.R.attr.selectableItemBackground, background, true)
button.setBackgroundResource(background.resourceId)
推荐阅读
- php - 一个事务ID在数据库中多次插入
- php - MYSQL:SQLSTATE [22007]:日期时间格式无效
- ios - 为什么我的空 iOS 单视图应用程序项目会崩溃?
- c++ - C++ 中的多态性在幕后发生了什么?
- php - 使用 PHP,我如何能够在我的内容模板中加载锚定部分,从文件名中读取基于 id 的内容?
- python - Pandas:多索引数据帧索引 - 将值设置为数据帧子集
- android - 如何使用 PagerAdapter 在 ViewPager 中调用 destroyItem()
- java - 为什么我的数据库中的所有数据都没有显示到我的 JTextfield 上
- python - 在 Python 中从列表创建元组
- javascript - 展平路线树