2011-12-14 133 views
0

我想爲我的應用程序中的每個XML佈局設置背景,並且我爲它們全部使用相同的圖片。目前,我經歷的所有百元左右佈局,只是在鍵入代碼:如何爲應用程序設置通用背景

android:background="@drawable/dirtwall" 

到所有佈局。我怎麼能夠自動設置它,所以我不需要更改其他背景,因此我不需要檢查所有的XML文件?

另外,我正在使用Eclipse。

回答

3

您可以使用styles。這樣,您可以在一個地方定義所有常見元素,然後僅在那裏更新它。

style.xml,應位於項目的/res/values/文件夾

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="appearance"> 
     <item name="android:background">@drawable/dirtwall</item> 
    </style> 
</resources> 

,並使用它像

<LinearLayout 
    ... 
    style = "@style/appearance" 
> 
+0

我在哪裏可以把這個代碼的風格? – 2011-12-14 16:01:05

0

應用主題到活動或應用

To set a theme for all the activities of your application, open the AndroidManifest.xml file and edit the <application> tag to include the android:theme attribute with the style name. For example: 

<application android:theme="@style/CustomTheme"> 

If you want a theme applied to just one Activity in your application, then add the android:theme attribute to the <activity> tag instead. 

if you want the background to be transparent, use the Translucent theme: 

<activity android:theme="@android:style/Theme.Translucent"> 

這裏是一個自定義的主題,這簡直就是標準的平臺默認光主題的聲明。它會去在一個XML文件

res/values (typically res/values/styles.xml): 

<style name="CustomTheme" parent="android:Theme.Light"> 
    ... 
</style> 
相關問題