Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:背景色和图像同时显示_Android_Image_Colors_Background - Fatal编程技术网

Android:背景色和图像同时显示

Android:背景色和图像同时显示,android,image,colors,background,Android,Image,Colors,Background,我需要有一个文本视图背景的颜色和图像,通过java而不是xml。 换句话说,我需要一个类似css规则的东西: background:#f00 url(...); 如果不使用xml,如何实现这一点 谢谢没有xml,您就不能这样做 步骤1 创建新的可绘制资源文件textview_background.xml <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.andro

我需要有一个文本视图背景的颜色和图像,通过java而不是xml。 换句话说,我需要一个类似css规则的东西:

background:#f00 url(...);
如果不使用xml,如何实现这一点

谢谢

没有xml,您就不能这样做

步骤1

创建新的可绘制资源文件textview_background.xml

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">


<item android:id="@+id/background" >
    <shape android:shape="rectangle">
        <solid android:color="COLOR"/>
    </shape>
</item>

<item android:id="@+id/item_img">
    <bitmap  android:src="IMAGE" />
</item>

</layer-list>
<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/textview_background" />
// change background image
LayerDrawable layer = (LayerDrawable) textView.getBackground();
BitmapDrawable bitmap = (BitmapDrawable) resources.getDrawable(R.drawable.IMAGE);
layer.setDrawableByLayerId(R.id.item_img, bitmap);
// change background color
GradientDrawable bgShape = (GradientDrawable) textView.getBackground();
bgShape.setColor(Color.YOURCOLOR);