android Button如何用代码设置background

小李葛格 2016-03-06 02:39:03
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/login"
android:text="立即充值"
android:onClick="onClick"
android:textColor="#fff" />


上面的background是指定了xml文件,
那么我想用代码来设置background为xml。怎么设置呢?
...全文
1949 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
小李葛格 2016-03-06
  • 打赏
  • 举报
回复
引用 1 楼 wei220000 的回复:
Button button1=(Button)findViewById(R.id.button1); button1.setBackground(R.drawable.login);
解决了,是setBackgroundResource(),谢谢
小李葛格 2016-03-06
  • 打赏
  • 举报
回复
引用 1 楼 wei220000 的回复:
Button button1=(Button)findViewById(R.id.button1);
button1.setBackground(R.drawable.login);


类型 View 中的方法 setBackground(Drawable)对于参数(int)不适用
wei220000 2016-03-06
  • 打赏
  • 举报
回复
Button button1=(Button)findViewById(R.id.button1); button1.setBackground(R.drawable.login);

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧