Android background image not shown in device?

2019-05-17 09:04发布

I add the following code to main.xml to set an image as a background image for my app:

android:background="@drawable/bg"

So main.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android= "http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="bottom|fill_vertical"
android:background="@drawable/bg0"
>
<EditText
    android:id="@+id/edWord"
    android:layout_width="fill_parent"
    android:layout_height="53px"
    android:textSize="20px"
    android:singleLine="true"
    android:hint="" 
/>
<LinearLayout xmlns:android=
    "http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical"
    android:layout_weight="1"
    >
    <ListView android:id="@+id/lstWord"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
    />

</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
>
    <ImageButton
        android:id="@+id/btnGetText"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@drawable/get"
        android:layout_weight="0.25"
    />
    <ImageButton
        android:id="@+id/btnFind"
        android:text="Click me..."
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@drawable/find"
        android:layout_weight="0.25"
    /> 

</LinearLayout>
</LinearLayout>

And of course the image bg.png is in drawable.

The problem is that the background image is shown only in the emulator but not in my actual device (HTC Desire).

What have I done wrong? What do I need to do to solve this problem?

Can you guys there help? Thank you very much.

4条回答
forever°为你锁心
2楼-- · 2019-05-17 09:46

I found the problem . Just remove fill_parent from the main Linear Layout Gravity. No need to set background in the Listview as you do right now. Just Change as i suggest it solve your problem.

android:gravity="bottom|fill_vertical"
查看更多
可以哭但决不认输i
3楼-- · 2019-05-17 09:51

Some lolipop devices wouldn't show with higher resolution images. You could also manually resize the bitmap programmatically if you generate the backgrounds randomly or use a set of images to load them into the background

查看更多
叛逆
4楼-- · 2019-05-17 09:56

try this in place of that

<LinearLayout 
    xmlns:android= "http://schemas.android.com/apk/res/android"
    android:id="@+id/linearlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"            
        android:background="@drawable/bg" />
 </Linearlayout>
查看更多
Luminary・发光体
5楼-- · 2019-05-17 10:08

I got a similar bug my background drawable was shown in the graphical editor of eclipse but not on the devices I used for debugging.

-> For me it helped to clean the image (I just opened it in paint and saved it) after that i overwrote the old image with the new one and copied it into the drawable folders.

查看更多
登录 后发表回答