我有一个不透明的图案(10% 的不透明度),我需要将它与一些颜色结合起来。
是否可以为图片设置背景颜色?
最佳答案
当然。只需使用 LayerListDrawable。
类似于:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#FF00ffff" />
</shape>
</item>
<item>
<bitmap
android:src="@drawable/your_tiled_drawable"
android:tileMode="repeat" />
</item>
</layer-list>
将其保存在您的 res/drawables
目录中,并将其用作您的背景。
遗憾的是,如果您的应用程序更复杂,有时 tileMode 会无缘无故地取消设置。如果遇到这种情况,您将不得不在 Java 中重新添加平铺模式。
我一般还是在xml中设置,然后做类似的事情:
View v = findViewById(R.id.my_view);
LayerDrawable lld = (LayerDrawable) v.getBackground();
BitmapDrawable tiled = (BitmapDrawable) lld.getDrawable(1);
tiled.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
关于android - 是否可以使用 drawable+color 作为背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11599281/