【问题标题】:Creating Background Image for Android App - Java为 Android 应用程序创建背景图像 - Java
【发布时间】:2011-10-11 17:12:39
【问题描述】:

我刚刚开始进行 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);
    }
}

【问题讨论】:

    标签: java android image background android-linearlayout


    【解决方案1】:

    是的,如果您使用的是 XML,然后找到该线性布局的 id,例如

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

    然后将其背景设置为

    ll.setBackgroundResource(R.drawable.image_name);
    

    这里给你的代码你可以直接去

    ll.setBackgroundResource(R.drawable.image_name);
    

    【讨论】:

      【解决方案2】:
      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 中做到这一点。
      猜你喜欢
      • 2012-07-14
      • 1970-01-01
      • 1970-01-01
      • 2012-12-25
      • 2016-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多