2014-03-26 23 views
2

我正在爲我們的Android應用程序創建一個默認主題,以指定應用於整個應用程序的默認自定義主題。這個想法是我們不應該在佈局中指定外觀相關的屬性,他們應該通過將自定義主題應用到整個應用程序來自動注入。如何定義自定義應用程序主題以指定Android中佈局的默認背景?

我可以通過覆蓋它們的樣式來爲UI小部件(如TextViewEditText)執行此操作。 例如覆蓋android:textViewStyleandroid:buttonStyle等 如何對佈局(LinearLayoutRelativeLayoutRelativeLayout等)執行相同操作,以便我可以指定佈局的默認背景?

在Android清單中的應用元件看起來像這樣:

.... 
<application android:icon="@drawable/ic_launcher" android:label="@string/app_title"  android:allowBackup="false" 
    android:theme="@style/Theme.MyCustomTheme" android:name="MyAppClass"> 

Theme.MyCustomTheme看起來像這樣:

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 
    <style name="Theme.MyCustomTheme" parent="Theme.AppCompat"> 
    <item name="android:textViewStyle">@style/my_custom_text_view_style</item> 
    <item name="android:editTextStyle">@style/my_custom_edit_text_style</item> 
    <item name="android:buttonStyle">@style/my_custom_button_style</item> 
    <item name="android:listViewStyle">@style/my_custom_list_style</item> 

上述繼承自定義樣式從相應的機器人的基本樣式。

我尋找合適的機器人屬性使用上面覆蓋的佈局風格,這樣我可以申請我的自定義默認背景給每一個佈局(LinearLayoutRelativeLayout等)宣佈在佈局xml文件,而無需在佈局xml中明確指定它。我試圖覆蓋android:colorBackground但這沒有任何區別。我試圖覆蓋android:windowBackground,但也改變了操作欄的顏色。請注意,我正在使用appcompat支持庫中的appcompat主題。

回答

2

我終於想通了如何在我的應用程序定義每個容器佈局自定義的默認背景

  1. 首先,我定義AA自定義主題與應用程序兼容性背景作爲覆蓋父背景風格默認的窗口背景。請注意,我用的主題從Android兼容性庫,支持蜂窩預Android設備

    <style name="Theme.MyCustomAppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    
        <item name="android:windowBackground">@color/my_custom_window_background</item> 
        <item name="android:colorBackground">@color/my_custom_window_background</item> 
    
    </style> 
    
  2. 我應用了上述主題,整個應用程序在AndroidManifest

    < 應用軟件Android:圖標=「@ drawable/ic_launcher」android:label =「@ string/app_title」android:theme =「@ style/Theme.MyCustomTheme」/>

  3. 上述步驟導致每個窗口的默認窗口背景色都被定義在上面的步驟1中包括動作b AR。我絕對不希望我的操作欄,編輯文本,文本視圖等作爲默認的窗口顏色。所以,我需要回到步驟1,覆蓋特定UI組件的風格,我不想有默認的窗口背景

    <style name="Theme.MyCustomAppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
        <item name="android:windowBackground">@color/my_custom_window_background</item> 
        <item name="android:colorBackground">@color/my_custom_window_background</item> 
        <item name="android:actionMenuTextColor">@color/customMenuColor</item> 
        <item name="android:textViewStyle">@style/customTextViewStyle</item> 
        <item name="android:editTextStyle">@style/customEditTextStyle</item> 
        <item name="android:buttonStyle">@style/customButtonStyle</item> 
        <item name="android:listViewStyle">@style/customListViewStyle</item> 
        <item name="android:tabWidgetStyle">@style/customTabWidgetStyle</item> 
        <!-- Support library compatibility --> 
        <item name="actionBarStyle">@style/customActionBarStyle</item> 
        <item name="actionMenuTextColor">@color/customMenuColor</item> 
    

  4. 的最後一步是定義每個自定義風格定義上述要麼對他們有自定義樣式或已賦值,則系統默認樣式(因爲自定義窗口背景覆蓋)

例如,自定義操作欄樣式看起來就像這樣:

<style name="customActionBarStyle" 
      parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"> 
     <item name="android:background">@color/light_black</item> 
     <item name="android:displayOptions">showHome|showTitle</item> 
     <item name="android:titleTextStyle">@style/customActionBarTitleTextStyle</item> 
     <!-- Support library compatibility --> 
     <item name="background">@color/light_black</item> 
     <item name="displayOptions">showHome|showTitle</item> 
     <item name="titleTextStyle">@style/customActionBarTitleTextStyle</item> 
    </style> 
  • 這種方法的優點是你可以在一個集中的方式定義一個默認主題,不必爲每個佈局定義一個背景,因爲它會通過注入適用於整個應用程序的自定義主題..該應用程序的任何未來重新主題化會很容易使用這種方法
  • 請參閱Android Documentation的主題化,包括如何主題操作欄以獲取更多信息

    0

    看着themes.xml我沒有看到任何適用於佈局和ViewGroups的一般情況(儘管您可以看到支持列表和一些小部件的樣式)。

    因此我不確定是否可以將您的自定義樣式應用於所有佈局。

    我猜你仍然要應用您的自定義佈局風格,(在那裏你可以改變背景顏色爲

    <item name="android:background">#ff0000</item> 
    

    )您的每一個佈局自己。

    隨時證明我錯了。

    +0

    感謝回覆。重寫android:background會改變每個UI元素的背景,而不僅僅是佈局。我已經嘗試過並且弄亂了整個用戶界面。如果您看到我的自定義樣式,它們會覆蓋適用於特定元素的樣式。我想爲線性,相對佈局等做同樣的事情,但不知道如何重寫適用於佈局的樣式(從ViewGroup繼承的UI佈局)。 – user3465654

    +0

    @ user3465654我更新了我的答案,恐怕你不能擁有你想要的 – kiruwka

    +0

    重寫android:background對我來說不是解決方案。這個想法本身是爲每個佈局注入背景,而不必爲每個佈局明確指定它。我會繼續研究如何做到這一點。 – user3465654