是否可以使用drawable + color作为背景?

Dmi*_*sev 8 android

我有一个不透明的图案(10%不透明度),我需要将它与一些颜色结合起来.

是否可以将背景颜色设置为图像?

ABe*_*oon 24

当然.只需使用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>
Run Code Online (Sandbox Code Playgroud)

将其保存在您的res/drawables目录中,并将其用作您的背景.

遗憾的是,如果您的应用程序更复杂,有时tileMode将无法解决.如果你跑到那里,你将不得不在Java中重新添加tile模式.

我通常仍然将其设置为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);
Run Code Online (Sandbox Code Playgroud)