如何设置类活动的背景

时间:2014-05-08 12:51:59

标签: android android-layout background-image

我没有任何XML-Layout。我直接启动我的活动

 super.onCreate(savedInstanceState);
 setContentView(new DrawingPanel(this));

这是一个绘图应用程序,默认背景为黑色。我想将Background更改为ImageBackground。我怎么能这样做?

编辑:这不仅仅是关于设置背景。默认情况下,我有一个带黑色背景的SurfaceView(DrawingPanel)。我想将黑色背景更改为图片,因此我可以在图片上绘制。

5 个答案:

答案 0 :(得分:0)

// try this way,hope this will help you...

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout parent = new LinearLayout(this);
        parent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
        parent.setBackgroundResource(R.drawable.ic_launcher); // here set your background
        parent.addView(new DrawingPanel(this));
        setContentView(parent);

}

答案 1 :(得分:0)

我支持@haresh,我认为问题出现是因为你试图加载大图像。这是一个很好的教程如何加载大位图。

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

答案 2 :(得分:0)

下面的代码可以很好地设置背景图像。

protected void onCreate(Bundle savedInstanceState) 
{
     super.onCreate(savedInstanceState);
     ImageView imageView = new ImageView(this);
     imageView.setImageResource(R.drawable.ic_launcher);
     setContentView(imageView);


}

答案 3 :(得分:0)

Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.mypicture);
scaled = Bitmap.createScaledBitmap(background, width, height, true);

然后使用

canvas.drawBitmap(scaled, 0, 0, null);

解决了这个问题!

答案 4 :(得分:0)

这对我来说很好

            logo = new ImageView(this);
    logo.setImageResource(R.drawable.sync_cloud_logo);
    logo.setBackgroundColor(getResources().getColor(R.color.drawer_bkground));// your color
            setContentView(logo);