日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

【Android Developers Training】 8. 定義Actio

系統(tǒng) 2644 0

注:本文翻譯自Google官方的Android Developers Training文檔,譯者技術(shù)一般,由于喜愛安卓而產(chǎn)生了翻譯的念頭,純屬個人興趣愛好。

原文鏈接: http://developer.android.com/training/basics/actionbar/styling.html


Action Bar能夠向你的用戶提供易掌握的操作方法,同時也能幫助用戶導(dǎo)航,但這不代表所有應(yīng)用的Action都長一個模樣。如果你希望將你的Action Bar風(fēng)格進(jìn)行自定義來使它符合你應(yīng)用的整體風(fēng)格,你可以通過使用安卓的風(fēng)格和主題資源( style and theme )來實現(xiàn)這一設(shè)想。

Android包含一些內(nèi)置的Activity主題,包括“暗(dark)”和“亮(light)”的Action Bar風(fēng)格。你也可以將這些主題做進(jìn)一步地定制化。

Note:

如果你使用的是“ Support Library ” APIs所提供的Action Bar,那么你必須使用(或者說覆寫) Theme.AppCompat 這一系列中的風(fēng)格(而不是在API Level 11或以上中的 Theme.Holo 系列)。因此,每個你聲明的風(fēng)格屬性必須被聲明兩次:一次用于平臺風(fēng)格的聲明(“ android: ”屬性)還有一次用來聲明“ Support Library ”所包含的風(fēng)格的屬性( appcompat.R.attr ”屬性,這些屬性的Context一般就是你的App )。可以接下去看例子來理解。

?

?一). 使用一個Android主題

Android包括兩個基本的activity主題,它們決定了Action Bar的顏色:

你既可以通過在清單文件的 <application> 標(biāo)簽中對 android:theme 屬性標(biāo)簽進(jìn)行聲明,來 將這些主題應(yīng)用到你的整個應(yīng)用當(dāng)中,也可以在清單文件的單個 <activity> 標(biāo)簽中對 android:theme 屬性標(biāo)簽進(jìn)行聲明,來將主題 應(yīng)用到單個activity中。例如:

      
        <
      
      
        application 
      
      
        android:theme
      
      
        ="@android:style/Theme.Holo.Light"
      
      
         ... 
      
      
        />
      
    

你也可以僅讓Action Bar為暗色,并把activity的剩余部分設(shè)置為亮色主題,這可以通過聲明 Theme.Holo.Light.DarkActionBar 這一主題來實現(xiàn)。

當(dāng)你使用的是 Support Library 時,你必須使用 Theme.AppCompat 系列的主題:

請務(wù)必記得,你使用的action bar上的圖標(biāo)要和你的action bar的背景色擁有反差。在 Action Bar Icon Pack 包含了適配于“ Holo light ”和“ Holo dark ”這兩個系列主題的Action Bar的配套圖標(biāo)。

?

二). 自定義背景

為了改變Action Bar的背景色,你可以為你的activity創(chuàng)建一個自定義的主題,這可以通過覆寫 actionBarStyle 這一屬性。這一屬性指向另一個style文件,在其中你可以覆寫 background 屬性,來為 action bar 特定一個圖像資源作為其背景。如果你的應(yīng)用使用 navigation tabs 或者 split action bar ,之后你也可以分別通過使用 backgroundStacked backgroundSplit 這兩個屬性字段為這些 action bar 指定背景。

Caution:

注意,你最好是為你自定義的主題和風(fēng)格聲明一個父主題,使得你的自定義的主題可以繼承父主題的風(fēng)格。如果沒有一個父主題的風(fēng)格,那么你自定義的Action Bar會缺少很多風(fēng)格屬性,除非你顯示地聲明了他們。

對于Android 3.0或更高版本的系統(tǒng)

當(dāng)只支持Android 3.0或更高系統(tǒng)版本,你可以這樣聲明你的Action Bar背景:

res/values/themes.xml

      
        <?
      
      
        xml version="1.0" encoding="utf-8"
      
      
        ?>
      
      
        <
      
      
        resources
      
      
        >
      
      
        <!--
      
      
         the theme applied to the application or activity 
      
      
        -->
      
      
        <
      
      
        style 
      
      
        name
      
      
        ="CustomActionBarTheme"
      
      
        

           parent
      
      
        ="@style/Theme.Holo.Light.DarkActionBar"
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:actionBarStyle"
      
      
        >
      
      @style/MyActionBar
      
        </
      
      
        item
      
      
        >
      
      
        </
      
      
        style
      
      
        >
      
      
        <!--
      
      
         ActionBar styles 
      
      
        -->
      
      
        <
      
      
        style 
      
      
        name
      
      
        ="MyActionBar"
      
      
        

           parent
      
      
        ="@style/Widget.Holo.Light.ActionBar.Solid.Inverse"
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:background"
      
      
        >
      
      @drawable/actionbar_background
      
        </
      
      
        item
      
      
        >
      
      
        </
      
      
        style
      
      
        >
      
      
        </
      
      
        resources
      
      
        >
      
    

之后將你的主題應(yīng)用到你的整個系統(tǒng)或單個activity中

      
        <
      
      
        application 
      
      
        android:theme
      
      
        ="@style/CustomActionBarTheme"
      
      
         ... 
      
      
        />
      
    

對于Android 2.1或更高版本的系統(tǒng)

如果使用“ Support Library ”,像上述中的那個主題應(yīng)該這樣聲明:

res/values/themes.xml

      
        <?
      
      
        xml version="1.0" encoding="utf-8"
      
      
        ?>
      
      
        <
      
      
        resources
      
      
        >
      
      
        <!--
      
      
         the theme applied to the application or activity 
      
      
        -->
      
      
        <
      
      
        style 
      
      
        name
      
      
        ="CustomActionBarTheme"
      
      
        

           parent
      
      
        ="@style/Theme.AppCompat.Light.DarkActionBar"
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:actionBarStyle"
      
      
        >
      
      @style/MyActionBar
      
        </
      
      
        item
      
      
        >
      
      
        <!--
      
      
         Support library compatibility 
      
      
        -->
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="actionBarStyle"
      
      
        >
      
      @style/MyActionBar
      
        </
      
      
        item
      
      
        >
      
      
        </
      
      
        style
      
      
        >
      
      
        <!--
      
      
         ActionBar styles 
      
      
        -->
      
      
        <
      
      
        style 
      
      
        name
      
      
        ="MyActionBar"
      
      
        

           parent
      
      
        ="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:background"
      
      
        >
      
      @drawable/actionbar_background
      
        </
      
      
        item
      
      
        >
      
      
        <!--
      
      
         Support library compatibility 
      
      
        -->
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="background"
      
      
        >
      
      @drawable/actionbar_background
      
        </
      
      
        item
      
      
        >
      
      
        </
      
      
        style
      
      
        >
      
      
        </
      
      
        resources
      
      
        >
      
    

之后將你的主題應(yīng)用到你的整個系統(tǒng)或單個activity中

      
        <
      
      
        application 
      
      
        android:theme
      
      
        ="@style/CustomActionBarTheme"
      
      
         ... 
      
      
        />
      
    

?

三). 自定義字體顏色

要修改Action Bar中的字體顏色,你需要分別為每個文本標(biāo)簽覆寫響應(yīng)的屬性:

  • Action Bar標(biāo)題:創(chuàng)建一個指定了“ textColor ”屬性,并且在你的自定義的 actionBarStyle 中為 titleTextStyle 屬性指定了風(fēng)格。

Note:

應(yīng)用在 titleTextStyle 上的自定義風(fēng)格必須使用 TextAppearance.Holo.Widget.ActionBar.Title 作為父風(fēng)格( parent style )。

對于Android 3.0或更高版本的系統(tǒng)

當(dāng)只支持Android 3.0或更高系統(tǒng)版本,你的風(fēng)格XML文件看上去應(yīng)該像是這樣:

      
        <?
      
      
        xml version="1.0" encoding="utf-8"
      
      
        ?>
      
      
        <
      
      
        resources
      
      
        >
      
      
        <!--
      
      
         the theme applied to the application or activity 
      
      
        -->
      
      
        <
      
      
        style 
      
      
        name
      
      
        ="CustomActionBarTheme"
      
      
        

           parent
      
      
        ="@style/Theme.Holo"
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:actionBarStyle"
      
      
        >
      
      @style/MyActionBar
      
        </
      
      
        item
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:actionBarTabTextStyle"
      
      
        >
      
      @style/MyActionBarTabText
      
        </
      
      
        item
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:actionMenuTextColor"
      
      
        >
      
      @color/actionbar_text
      
        </
      
      
        item
      
      
        >
      
      
        </
      
      
        style
      
      
        >
      
      
        <!--
      
      
         ActionBar styles 
      
      
        -->
      
      
        <
      
      
        style 
      
      
        name
      
      
        ="MyActionBar"
      
      
        

           parent
      
      
        ="@style/Widget.Holo.ActionBar"
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:titleTextStyle"
      
      
        >
      
      @style/MyActionBarTitleText
      
        </
      
      
        item
      
      
        >
      
      
        </
      
      
        style
      
      
        >
      
      
        <!--
      
      
         ActionBar title text 
      
      
        -->
      
      
        <
      
      
        style 
      
      
        name
      
      
        ="MyActionBarTitleText"
      
      
        

           parent
      
      
        ="@style/TextAppearance.Holo.Widget.ActionBar.Title"
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:textColor"
      
      
        >
      
      @color/actionbar_text
      
        </
      
      
        item
      
      
        >
      
      
        </
      
      
        style
      
      
        >
      
      
        <!--
      
      
         ActionBar tabs text styles 
      
      
        -->
      
      
        <
      
      
        style 
      
      
        name
      
      
        ="MyActionBarTabText"
      
      
        

           parent
      
      
        ="@style/Widget.Holo.ActionBar.TabText"
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:textColor"
      
      
        >
      
      @color/actionbar_text
      
        </
      
      
        item
      
      
        >
      
      
        </
      
      
        style
      
      
        >
      
      
        </
      
      
        resources
      
      
        >
      
    

對于Android 2.1或更高版本的系統(tǒng)

如果使用了“ Support Library ”,你的風(fēng)格XML文件看上去應(yīng)該像是這樣:

      
        <?
      
      
        xml version="1.0" encoding="utf-8"
      
      
        ?>
      
      
        <
      
      
        resources
      
      
        >
      
      
        <!--
      
      
         the theme applied to the application or activity 
      
      
        -->
      
      
        <
      
      
        style 
      
      
        name
      
      
        ="CustomActionBarTheme"
      
      
        

           parent
      
      
        ="@style/Theme.AppCompat"
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:actionBarStyle"
      
      
        >
      
      @style/MyActionBar
      
        </
      
      
        item
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:actionBarTabTextStyle"
      
      
        >
      
      @style/MyActionBarTabText
      
        </
      
      
        item
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:actionMenuTextColor"
      
      
        >
      
      @color/actionbar_text
      
        </
      
      
        item
      
      
        >
      
      
        <!--
      
      
         Support library compatibility 
      
      
        -->
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="actionBarStyle"
      
      
        >
      
      @style/MyActionBar
      
        </
      
      
        item
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="actionBarTabTextStyle"
      
      
        >
      
      @style/MyActionBarTabText
      
        </
      
      
        item
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="actionMenuTextColor"
      
      
        >
      
      @color/actionbar_text
      
        </
      
      
        item
      
      
        >
      
      
        </
      
      
        style
      
      
        >
      
      
        <!--
      
      
         ActionBar styles 
      
      
        -->
      
      
        <
      
      
        style 
      
      
        name
      
      
        ="MyActionBar"
      
      
        

           parent
      
      
        ="@style/Widget.AppCompat.ActionBar"
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:titleTextStyle"
      
      
        >
      
      @style/MyActionBarTitleText
      
        </
      
      
        item
      
      
        >
      
      
        <!--
      
      
         Support library compatibility 
      
      
        -->
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="titleTextStyle"
      
      
        >
      
      @style/MyActionBarTitleText
      
        </
      
      
        item
      
      
        >
      
      
        </
      
      
        style
      
      
        >
      
      
        <!--
      
      
         ActionBar title text 
      
      
        -->
      
      
        <
      
      
        style 
      
      
        name
      
      
        ="MyActionBarTitleText"
      
      
        

           parent
      
      
        ="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:textColor"
      
      
        >
      
      @color/actionbar_text
      
        </
      
      
        item
      
      
        >
      
      
        <!--
      
      
         The textColor property is backward compatible with the Support Library 
      
      
        -->
      
      
        </
      
      
        style
      
      
        >
      
      
        <!--
      
      
         ActionBar tabs text 
      
      
        -->
      
      
        <
      
      
        style 
      
      
        name
      
      
        ="MyActionBarTabText"
      
      
        

           parent
      
      
        ="@style/Widget.AppCompat.ActionBar.TabText"
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:textColor"
      
      
        >
      
      @color/actionbar_text
      
        </
      
      
        item
      
      
        >
      
      
        <!--
      
      
         The textColor property is backward compatible with the Support Library 
      
      
        -->
      
      
        </
      
      
        style
      
      
        >
      
      
        </
      
      
        resources
      
      
        >
      
    

?

四). 自定義標(biāo)簽欄

為了改變 navigation tabs 上使用的指引,創(chuàng)建一個覆寫 actionBarTabStyle 屬性的activity主題。這個屬性指向另一個風(fēng)格資源,在其中你覆寫 background 屬性,在這里指定一個圖像文件的狀態(tài)列表。

Note:

一個 圖像文件的狀態(tài)列表是很重要的,因為通過背景的不同可以表示出當(dāng)前那個標(biāo)簽指引是被選中的。可以通過閱讀 State List 來學(xué)習(xí)更多的關(guān)于如何創(chuàng)建圖像資源來多按鈕狀態(tài)的問題。

例如:這里是一個 圖像文件的狀態(tài)列表,它為一個Action Bar標(biāo)簽的每一個不同狀態(tài)聲明一個特定的背景圖片:

res/drawable/actionbar_tab_indicator.xml

      
        <?
      
      
        xml version="1.0" encoding="utf-8"
      
      
        ?>
      
      
        <
      
      
        selector 
      
      
        xmlns:android
      
      
        ="http://schemas.android.com/apk/res/android"
      
      
        >
      
      
        <!--
      
      
         STATES WHEN BUTTON IS NOT PRESSED 
      
      
        -->
      
      
        <!--
      
      
         Non focused states 
      
      
        -->
      
      
        <
      
      
        item 
      
      
        android:state_focused
      
      
        ="false"
      
      
         android:state_selected
      
      
        ="false"
      
      
        

          android:state_pressed
      
      
        ="false"
      
      
        

          android:drawable
      
      
        ="@drawable/tab_unselected"
      
      
        />
      
      
        <
      
      
        item 
      
      
        android:state_focused
      
      
        ="false"
      
      
         android:state_selected
      
      
        ="true"
      
      
        

          android:state_pressed
      
      
        ="false"
      
      
        

          android:drawable
      
      
        ="@drawable/tab_selected"
      
      
        />
      
      
        <!--
      
      
         Focused states (such as when focused with a d-pad or mouse hover) 
      
      
        -->
      
      
        <
      
      
        item 
      
      
        android:state_focused
      
      
        ="true"
      
      
         android:state_selected
      
      
        ="false"
      
      
        

          android:state_pressed
      
      
        ="false"
      
      
        

          android:drawable
      
      
        ="@drawable/tab_unselected_focused"
      
      
        />
      
      
        <
      
      
        item 
      
      
        android:state_focused
      
      
        ="true"
      
      
         android:state_selected
      
      
        ="true"
      
      
        

          android:state_pressed
      
      
        ="false"
      
      
        

          android:drawable
      
      
        ="@drawable/tab_selected_focused"
      
      
        />
      
      
        <!--
      
      
         STATES WHEN BUTTON IS PRESSED 
      
      
        -->
      
      
        <!--
      
      
         Non focused states 
      
      
        -->
      
      
        <
      
      
        item 
      
      
        android:state_focused
      
      
        ="false"
      
      
         android:state_selected
      
      
        ="false"
      
      
        

          android:state_pressed
      
      
        ="true"
      
      
        

          android:drawable
      
      
        ="@drawable/tab_unselected_pressed"
      
      
        />
      
      
        <
      
      
        item 
      
      
        android:state_focused
      
      
        ="false"
      
      
         android:state_selected
      
      
        ="true"
      
      
        

        android:state_pressed
      
      
        ="true"
      
      
        

        android:drawable
      
      
        ="@drawable/tab_selected_pressed"
      
      
        />
      
      
        <!--
      
      
         Focused states (such as when focused with a d-pad or mouse hover) 
      
      
        -->
      
      
        <
      
      
        item 
      
      
        android:state_focused
      
      
        ="true"
      
      
         android:state_selected
      
      
        ="false"
      
      
        

          android:state_pressed
      
      
        ="true"
      
      
        

          android:drawable
      
      
        ="@drawable/tab_unselected_pressed"
      
      
        />
      
      
        <
      
      
        item 
      
      
        android:state_focused
      
      
        ="true"
      
      
         android:state_selected
      
      
        ="true"
      
      
        

          android:state_pressed
      
      
        ="true"
      
      
        

          android:drawable
      
      
        ="@drawable/tab_selected_pressed"
      
      
        />
      
      
        </
      
      
        selector
      
      
        >
      
    

對于Android 3.0或更高版本的系統(tǒng)

當(dāng)只支持Android 3.0或更高系統(tǒng)版本,你的風(fēng)格XML文件看上去應(yīng)該像是這樣:

      
        <?
      
      
        xml version="1.0" encoding="utf-8"
      
      
        ?>
      
      
        <
      
      
        resources
      
      
        >
      
      
        <!--
      
      
         the theme applied to the application or activity 
      
      
        -->
      
      
        <
      
      
        style 
      
      
        name
      
      
        ="CustomActionBarTheme"
      
      
        

           parent
      
      
        ="@style/Theme.Holo"
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:actionBarTabStyle"
      
      
        >
      
      @style/MyActionBarTabs
      
        </
      
      
        item
      
      
        >
      
      
        </
      
      
        style
      
      
        >
      
      
        <!--
      
      
         ActionBar tabs styles 
      
      
        -->
      
      
        <
      
      
        style 
      
      
        name
      
      
        ="MyActionBarTabs"
      
      
        

           parent
      
      
        ="@style/Widget.Holo.ActionBar.TabView"
      
      
        >
      
      
        <!--
      
      
         tab indicator 
      
      
        -->
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:background"
      
      
        >
      
      @drawable/actionbar_tab_indicator
      
        </
      
      
        item
      
      
        >
      
      
        </
      
      
        style
      
      
        >
      
      
        </
      
      
        resources
      
      
        >
      
    

對于Android 2.1或更高版本的系統(tǒng)

如果使用了“ Support Library ”,你的風(fēng)格XML文件看上去應(yīng)該像是這樣:

      
        <?
      
      
        xml version="1.0" encoding="utf-8"
      
      
        ?>
      
      
        <
      
      
        resources
      
      
        >
      
      
        <!--
      
      
         the theme applied to the application or activity 
      
      
        -->
      
      
        <
      
      
        style 
      
      
        name
      
      
        ="CustomActionBarTheme"
      
      
        

           parent
      
      
        ="@style/Theme.AppCompat"
      
      
        >
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:actionBarTabStyle"
      
      
        >
      
      @style/MyActionBarTabs
      
        </
      
      
        item
      
      
        >
      
      
        <!--
      
      
         Support library compatibility 
      
      
        -->
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="actionBarTabStyle"
      
      
        >
      
      @style/MyActionBarTabs
      
        </
      
      
        item
      
      
        >
      
      
        </
      
      
        style
      
      
        >
      
      
        <!--
      
      
         ActionBar tabs styles 
      
      
        -->
      
      
        <
      
      
        style 
      
      
        name
      
      
        ="MyActionBarTabs"
      
      
        

           parent
      
      
        ="@style/Widget.AppCompat.ActionBar.TabView"
      
      
        >
      
      
        <!--
      
      
         tab indicator 
      
      
        -->
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="android:background"
      
      
        >
      
      @drawable/actionbar_tab_indicator
      
        </
      
      
        item
      
      
        >
      
      
        <!--
      
      
         Support library compatibility 
      
      
        -->
      
      
        <
      
      
        item 
      
      
        name
      
      
        ="background"
      
      
        >
      
      @drawable/actionbar_tab_indicator
      
        </
      
      
        item
      
      
        >
      
      
        </
      
      
        style
      
      
        >
      
      
        </
      
      
        resources
      
      
        >
      
    

?

更多資源:

更多Action Bar的風(fēng)格屬性: Action Bar

更多主題方面的知識: Styles and Themes

*更多完整的Action Bar風(fēng)格: Android Action Bar Style Generator

【Android Developers Training】 8. 定義Action Bar風(fēng)格


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 茂名市| 海伦市| 茂名市| 克山县| 双流县| 留坝县| 霍山县| 泗水县| 西贡区| 遵化市| 吉木萨尔县| 河北省| 鄂温| 齐河县| 洪洞县| 买车| 张家口市| 黎平县| 桓台县| 莒南县| 军事| 东源县| 兴海县| 武汉市| 绥德县| 石渠县| 奉节县| 锦州市| 长兴县| 通化市| 乌什县| 会东县| 天津市| 富阳市| 竹北市| 东海县| 明溪县| 伊宁县| 浪卡子县| 军事| 澄迈县|