Android:背景色和图像同时显示
我需要有一个文本视图背景的颜色和图像,通过java而不是xml。 换句话说,我需要一个类似css规则的东西: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
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);