How to add a background image to an Android Activity?
My goal is to develop a GUI application on top of a background image with buttons in specific places on the image. The first step is to display the background.
The image can be displayed with resources and is described in several FAQs including this one:
how-to-add-background-image-to-activity
It compiles and runs without errors, but the background is black.
Here is the main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk开发者_StackOverflow/res/android"
android:id="@+id/rootRL"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background">
</RelativeLayout>
The image has been in png, 9.png, and jpg format with basenames of 'main' and 'background'. It builds but does not display. Making clean and recompiling does not help.
This behavior occurs on both the emulator and on hardware -- an Atmel AT91SAM9M10-G45-EK. The SDK version is 2.0.1.
Thank you for your help.
Problem solved: The code was based on code that had a text view and set up the window dynamically.
The Hello Android example does call setContentView(R.layout.main)
which was the clue I needed.
Of course the background in defined in res/layout/main.xml
so it is necessary to call setContentView(R.layout.main)
to get those settings.
Here the complete explanation about how to add background images.
http://androidforbeginners.blogspot.fr/2010/06/how-to-tile-background-image-in-android.html
精彩评论