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

3
我刚开始学习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);
    }
}
2个回答

4

谢谢,我刚刚也想到了如何在XML中实现它。 - HJM

1
如果您正在使用 XML,则可以查找该 LinearLayout 的 id。
ll=(LinearLayout)findViewById(R.id.linear1)

然后将其背景设置为

ll.setBackgroundResource(R.drawable.image_name);

如果你的代码已经给出,那么你可以直接使用这里的else语句

ll.setBackgroundResource(R.drawable.image_name);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接