小编Usm*_*sif的帖子

设置背景颜色,并在Android中使用drawable作为背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke android:width="1px" android:color="#696969"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

此代码用于动态创建按钮,问题是我想设置背景颜色并设置背景可绘制对象。

Button btnTag = new Button(alltable.this);
btnTag.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
                       LinearLayout.LayoutParams.WRAP_CONTENT));
try {
    btnTag.setWidth(130);
    btnTag.setBackground(getResources().getDrawable(R.color.blue));

} catch (Exception e){
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

这是一类,我想设置btn的背景色,然后使用我的drawable。

android button android-layout

5
推荐指数
3
解决办法
2万
查看次数

标签 统计

android ×1

android-layout ×1

button ×1