简体   繁体   English

Android-如何更改由BitmapDrawable创建的灰色背景

[英]Android - How to change grey background created by a BitmapDrawable

I'm using the follow code, to create a non-scaled, centred image as a background, in a relative layout:- 我正在使用以下代码,以相对布局创建未缩放的居中图像作为背景:-

RelativeLayout explosionlayout = (RelativeLayout) findViewById (R.id.explosionlayout);
explosionlayout.setBackgroundColor(R.color.white);

Bitmap myBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.bomb);
    BitmapDrawable test1 = new BitmapDrawable(myBitmap);
    test1.setGravity(Gravity.CENTER);

The only problem I have, is that the background of the relativelayout is grey, regardless of what I set it to, either via XML or in code. 我唯一的问题是,无论我通过XML或代码将其设置为什么,relativelayout的背景都是灰色。

Any ideas would be appreciated, thanks. 任何想法将不胜感激,谢谢。

You probably want this instead: 您可能想要这样:

explosionlayout.setBackgroundColor(getResources().getColor(R.color.white));

or just 要不就

explosionlayout.setBackgroundColor(0xffffffff);

The reason is that R.color.white is an ID, while setBackgroundColor expects an actual 32-bit integer representation of the color. 原因是R.color.white是ID,而setBackgroundColor需要该颜色的实际32位整数表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM