【问题标题】:Can't put an image as android activity background无法将图像作为 android 活动背景
【发布时间】:2015-01-06 17:08:35
【问题描述】:

我正在尝试将图像作为背景,但我不明白为什么它不起作用。

这是我的 activity_start.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/startbackg" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="153dp"
        android:textColor="#555555"
        android:textSize="30sp"
        android:text="Hello" />

</RelativeLayout>

和 StartActivity.java

import android.app.Activity;
import android.os.Bundle;

public class StartActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

    }

}

在eclipse中我可以正确看到背景,像这样:

但是当我在 nexus 7 中运行它时,我看不到任何背景图片

我不知道该尝试什么...有什么想法吗?

这是我正在使用的图像

【问题讨论】:

  • 如果有像位图大小太高这样的警告,你能检查一下logcat吗?
  • 我在 logcat 中没有看到任何内容。图片为1080x1920,与nexus 5分辨率相同

标签: android xml android-activity background


【解决方案1】:

使用

    int sdk = android.os.Build.VERSION.SDK_INT;
    if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        myLInearLayout.setBackground(myImage);
        myLInearLayout.setBackgroundDrawable(myImage);

    } else {

        myLInearLayout.setBackground(myImage);

    }

【讨论】:

  • 你能解释一下你为什么这样做吗?
  • setBackgroundDrawable 用于低于 jellybean 的 api 版本,这种方法在 jellybean 之后被弃用,所以我在 else 语句中使用了 set background
  • 但是 OP 在 XML 中设置了背景
猜你喜欢
  • 2015-12-26
  • 2011-04-11
  • 2020-02-13
  • 2017-11-10
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多