简体   繁体   English

在Android应用程序中设置背景图片

[英]Setting background image in android application

For few last days I'm trying to set background image in my android application. 最近几天,我试图在我的android应用程序中设置背景图片。 First I added my image to res/drawable-hdpi and set it as a background in .xml code of my activity by writing 首先,我将图像添加到res / drawable-hdpi并通过编写以下内容将其设置为活动的.xml代码的背景

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bground"
    tools:context="patrycja.MainActivity" >

It didn't work so I tried also to put this image into another drawable folder in res - without success. 它没有用,所以我也尝试将此图像放入res中的另一个可绘制文件夹中-没有成功。 Then I even changed the extension of my file from .jpg to .png - nothing. 然后,我什至将文件的扩展名从.jpg更改为.png-什么都没有。 So I got into res/values/styles.xml and added this: 因此,我进入res / values / styles.xml并添加了以下内容:

 <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:background">@drawable/bground</item>
 </style>

Everytime my application compiles but when I try to start it I can't even go to my first activity because it stops. 每次我的应用程序编译时,但是当我尝试启动它时,我什至无法进入我的第一个活动,因为它停止了。 LogCat shows me RuntimeException, InvocationTargetException and OutOfMemoryError. LogCat向我显示了RuntimeException,InvocationTargetException和OutOfMemoryError。 Problem must be here because my app works good with setting colour as a background. 问题一定在这里,因为我的应用程序可以很好地将颜色设置为背景。 Has anybody got an idea how to fix it? 有人知道如何解决它吗?

Try this: 尝试这个:

  1. Use a .JPEG background. 使用.JPEG背景。
  2. Keep it in res/drawable-nodpi directory. 将其保存在res / drawable-nodpi目录中。

I think this will work as I have faced a similar crash earlier. 我认为这会起作用,因为我之前遇到过类似的崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM