为Android应用程序创建背景图像 – Java

我刚刚开始使用android开发,我需要有关背景图像的帮助。 我希望能够拥有背景图像,然后使用布局覆盖该背景上的其他项目(按钮,文本等)。 我使用LinearLayout只是为了简单,因为我不知道什么对我来说最好。

无论如何,我无法使用以下代码显示图像:

import android.app.Activity; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.widget.*; public class NewGameActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout ll = new LinearLayout(this); ll.setBackgroundDrawable(Drawable.createFromPath("/assets/images/androidBackground.png")); this.setContentView(ll); } } 

  ll.setBackgroundResource(R.drawable.image_name); 

http://developer.android.com/reference/android/view/View.html#setBackgroundResource%28int%29

这是访问可绘制资源的首选方式。 image_name是drawable或drawable-mdpi文件夹中的png图像。

是的,如果你使用的是XML,那么就找到那个linearlayout的id

 ll=(LinearLayout)findViewById(R.id.linear1) 

然后将其背景设置为

 ll.setBackgroundResource(R.drawable.image_name); 

在这里你可以直接找到你的代码

 ll.setBackgroundResource(R.drawable.image_name);