开发者

How to create a Button whose background color is visible through transparent background image?

Earlier, I was able to dynamically create an android.widget.Button whose background color was visible through the transparent parts of the Button's background image. I have not been able to re-create this. I have tried:

Button button = (Button) findViewById(id.button1);
try {
    button.setBackgroundColor(Color.RED);
    Bitmap bm = BitmapFactory.decodeStream(getAssets().open("transparent.png"));
    button.setBackgroundDrawable(new BitmapDrawable(bm));
    button.invalidate();
} catch (IOException e) {     
    throw new RuntimeException(e);
}

Only the image is visible if I run the above. If I move the call to setBackgroundColor below the call to setBackgroundDrawable, I only see the red, not the image. How can I make it开发者_StackOverflow so I can see both the image and, through its transparent parts, the background?


First it is easier to use an ImageButton, which has two layers, one is the background and the other the image on top. Set your background color with button.setBackgroundColor

then set the transparent image on top: button.setImageDrawable(getResources().getDrawable(R.drawable.transparent)); or set SRC preperty in XML

Use the button padding settings to adjust how much backround color should go around the image.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜