- Back to Home »
- Android »
- How to change the Android background
Tuesday, June 26, 2012
Android how to change the background, there are two ways, one is to use the background, one is to use picture
Then join in the main.xml (or your contentView xml file) which
First res-> values under the new xml file called color.xml
Join the following code
<?xml version="1.0" encoding="utf-8"?> <resources> <drawable name="darkgray">#808080</drawable> <drawable name="white">#FFFFFF</drawable> <drawable name="blue">#0000FF</drawable> </resources>
Then join in the main.xml (or your contentView xml file) which
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/white" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > </LinearLayout>
Then main.xml set your contentView replaced with white background
White seems invisible XDDD
Then you can see the following screen
Then call. Java files inside it
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }
Figure
So it seems a thing when the same XDD
Next is the picture you want into the background
First, choose the one you want to get a picture,
Assumption is that this bgbypic.png
Create a folder called drawable in the res which
The future, if there are drawing, you are thrown into this folder
Then change just main.xml can change color, just the name replaced by Image Name.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/bgbypic" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > </LinearLayout>
If you want to use code to control the background of it?
In the main.xml of change it, will LinearLayout more plus an id attribute
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/bgbypic" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/myLinearLayout" >
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout linearlayout = (LinearLayout) findViewById(R.id.myLinearLayout); Resources res = this.getResources(); Drawable drawable = res.getDrawable(R.drawable.blue); linearlayout.setBackgroundDrawable(drawable); }