我刚刚开始进行 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 图像。
关于java - 为 Android 应用程序创建背景图像 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6801716/