+-
设置背景颜色,并在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>

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

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();
}

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

最佳答案
在drawable(如files_bg.xml)文件夹中创建资源,并将其设置为布局背景.

在图层列表中使用两项,一项用于形状的纯色,另一项用于位图.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
       <shape android:shape="rectangle">
            <solid android:color="@color/totalFilesBgColor" />
       </shape>
    </item>
    <item>
       <bitmap
            android:src="@drawable/bg_icon"
            android:tileMode="disabled" />
    </item>
</layer-list> 

现在将drwable设置为在布局中或使用的任何位置的bakcground.

<LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="@drawable/files_bg">

 </LinearLayout>
点击查看更多相关文章

转载注明原文:设置背景颜色,并在Android中使用drawable作为背景 - 乐贴网