【发布时间】:2015-12-15 12:23:10
【问题描述】:
此代码用于动态创建按钮。问题是我想设置背景颜色并设置背景Drawables。
<?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>
这是我想设置按钮背景颜色的类,然后我想使用我的 Drawable。
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();
}
【问题讨论】:
-
'android:background' 应该在这里工作。它需要
"@color/..."和@drawable/..."参数 -
然后使用可绘制图层
-
请清除你的答案@piotrek1543
-
抱歉,我不知道您已经在使用
btnTag.setBackground(getResources().getDrawable(R.color.blue));,因为您会注意到通过这种方法您也可以使用drawable (r.drawable.LOGO) -
如果您想为整个布局设置背景颜色,请转到布局 xml 文件并设置为主布局
android:background属性
标签: android android-layout button drawable