How to set the background of the android applicati

2019-03-18 07:11发布

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

Now that is available in black I want to change that with some images or themes.

4条回答
Evening l夕情丶
2楼-- · 2019-03-18 07:40

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"
查看更多
趁早两清
3楼-- · 2019-03-18 07:45

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">
...
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-03-18 07:45

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

<LinearLayout android:background="@drawable/yourbackgroundimage" ... >   
查看更多
我命由我不由天
5楼-- · 2019-03-18 07:48

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

查看更多
登录 后发表回答