开发者

How to set the background of the android application?

I want to change the background of my App's Activity.

Now that is available i开发者_运维技巧n black I want to change that with some images or themes.


Add a android:theme="@style/Theme.AppTheme attribute to your application tag in the manifest file with the theme you want to use. This will prevent the default "black" background to be drawn if you only set a background in your Activity/Fragment layout file.

You declared it in a style.xml file.

<?xml version="1.0" encoding="utf-8"?>
<resources>    
<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="@style/Theme">
<!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
-->
</style>
</style>
    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <item name="android:windowBackground">@drawable/custom_background</item>
    </style>

</resources>

AndroidManifest.xml file

...
<application
    android:name="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppTheme">
...


Further to @Ryan:

In the layout for your activities, you can also add the attribute

android:background="@android:color/white"

to set a background color. You can also introduce a custom resource /res/values/colors.xml in which you can declare custom colors. One such file might look like:

<resources>
<color name="fire_brick_red">#B0171F</color>
</resources>

You then introduce these in XML as follows:

android:background="@color/fire_brick_red"


Add the background attribute to your window's xml layout. Example:

<LinearLayout android:background="@drawable/yourbackgroundimage" ... >   


Personally, I would try to do it in xml like Sk9 suggests, but programmatically at runtime you can do

setBackgroundColor(int color)

or

setBackgroundResource(int resourceID)

Source: http://developer.android.com/reference/android/view/View.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜