這篇文章將對(duì)ActionBar專題前面幾篇學(xué)習(xí)過(guò)的內(nèi)容做一個(gè)總結(jié),順便運(yùn)用以前學(xué)過(guò)的知識(shí)實(shí)現(xiàn)一個(gè)自定義樣式的ActionBar標(biāo)題欄效果。話不多說(shuō),進(jìn)入今天的正題。
一、實(shí)現(xiàn)效果圖 豎屏效果圖:最左邊是Logo圖標(biāo),右邊是工具欄按鈕,點(diǎn)擊Menu鍵顯示其余的按鈕鍵,下方是Tab標(biāo)簽選項(xiàng)。
橫屏效果圖:豎屏中的Tab選項(xiàng)標(biāo)簽變成了中間的下拉導(dǎo)航按鈕
二、項(xiàng)目結(jié)構(gòu)圖
三、編碼準(zhǔn)備工作 先來(lái)學(xué)習(xí)一下自定義樣式的基礎(chǔ)知識(shí),以便大家能看懂關(guān)于后面的代碼。 1、設(shè)置操作欄的樣式 如果你對(duì)應(yīng)用程序中的可視構(gòu)件進(jìn)行了定制化的設(shè)計(jì),那么你可能也會(huì)要對(duì)操作欄做一些重新設(shè)計(jì),以便跟應(yīng)用程序的設(shè)計(jì)匹配。要這樣做的話,需要使用Android的樣式與主題框架中的一些特殊的樣式屬性來(lái)重新設(shè)置操作欄的樣式。 注意:改變外觀的背景圖片依賴與當(dāng)前按鈕的狀態(tài)(選擇、按下、解除選擇),因此你使用的可描畫的資源必須是一個(gè)可描畫的狀態(tài)列表。 警告:對(duì)于你提供的所有可描畫的背景,要確保使用NinePatch類型可描畫資源,以便允許圖片的拉伸。NinePatch類型的圖片應(yīng)該比40像素高30像素寬的圖片要小。
2、普通的外觀 android:windowActionBarOverlay
這個(gè)屬性聲明了操作欄是否應(yīng)該覆蓋Activity布局,而不是相對(duì)Activity的布局位置的偏移。這個(gè)屬性的默認(rèn)值是false。 通常,在屏幕上,操作欄需要它自己的空間,并且把剩下的空間用來(lái)填充Activity的布局。當(dāng)操作欄四覆蓋模式時(shí),Activity會(huì)使用所有的有效空間,系統(tǒng)會(huì)在Activity的上面描畫操作欄。如果你想要在操作欄隱藏和顯示時(shí),布局中的內(nèi)容保持固定的尺寸好位置,那么這種覆蓋模式是有用的。你也可能只是為了顯示效果來(lái)使用它,因?yàn)槟憧梢越o操作欄設(shè)置半透明的背景,以便用戶依然能夠看到操作欄背后的Activity布局。 注意:默認(rèn)情況下,Holo主題會(huì)用半透明背景來(lái)描畫操作欄。但是,你能夠用自己的樣式來(lái)修改它,并且默認(rèn)的情況下,DeviceDefault主題在不同的設(shè)備上可能使用不透明的背景。 覆蓋模式被啟用時(shí),Activity布局不會(huì)感知到操作欄覆蓋在它的上面,因此,在操作欄覆蓋的區(qū)域,最好不要放置一些重要的信息或UI組件。如果適合,你能夠引用平臺(tái)的actionBarSize值來(lái)決定操作欄的高度,例如,在XML布局文件中引用這個(gè)值。 - <SomeView
- ...
- android:layout_marginTop="?android:attr/actionBarSize" />
復(fù)制代碼 你還能夠用getHeight()方法在運(yùn)行時(shí)獲取操作欄的高度。如果在Activity生存周期的早期調(diào)用這個(gè)方法,那么在調(diào)用時(shí)所反映的操作欄的高度可能不包括被堆放的操作欄(因?yàn)閷?dǎo)航選項(xiàng)標(biāo)簽)。要看如何在運(yùn)行時(shí)判斷操作欄總的高度(包括被堆放的操作欄),請(qǐng)看Honeycomb Gallery示例應(yīng)用中的TitlesFragment類。
3、操作項(xiàng)元素
android:actionButtonStyle給操作項(xiàng)按鈕定義樣式資源。android:actionBarItemBackground 給每個(gè)操作項(xiàng)的背景定義可描畫資源(被添加在API 級(jí)別 14中)。android:itemBackground 給每個(gè)懸浮菜單項(xiàng)的背景定義可描畫資源。android:actionBarDivider給操作項(xiàng)之間的分隔線定義可描畫資源(被添加在API 級(jí)別 14中)android:actionMenuTextColor給顯示在操作項(xiàng)中文本定義顏色。android:actionMenuTextAppearance 給顯示在操作項(xiàng)中文本定義樣式資源。android:actionBarWidgetTheme給作為操作視窗被填充到操作欄中的可視構(gòu)件定義主題資源(被添加在API級(jí)別14中)。
4、導(dǎo)航選項(xiàng)標(biāo)簽
android:actionBarTabStyle 給操作欄中的選項(xiàng)標(biāo)簽定義樣式資源。android:actionBarTabBarStyle給顯示在導(dǎo)航選項(xiàng)標(biāo)簽下方的細(xì)條定義樣式資源。android:actionBarTabTextStyle給導(dǎo)航選項(xiàng)標(biāo)簽中的文本定義樣式資源。
5、下拉列表
android:actionDropDownStyle 給下拉導(dǎo)航列表定義樣式(如背景和文本樣式)。如,下例XML文件中給操作欄定義了一些定制的樣式:- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <!-- the theme applied to the application or activity -->
- <style name="CustomActivityTheme" parent="@android:style/Theme.Holo">
- <item name="android:actionBarTabTextStyle">@style/CustomTabTextStyle</item>
- <item name="android:actionBarDivider">@drawable/ab_divider</item>
- <item name="android:actionBarItemBackground">@drawable/ab_item_background</item>
- </style>
- <!-- style for the action bar tab text -->
- <style name="CustomTabTextStyle" parent="@android:style/TextAppearance.Holo">
- <item name="android:textColor">#2456c2</item>
- </style>
- </resources>
復(fù)制代碼 注意:一定要在<style>標(biāo)簽中聲明一個(gè)父主題,這樣定制的主題可以繼承所有沒(méi)有明確聲明的樣式。在修改操作欄樣式時(shí),使用父主題是至關(guān)重要的,它會(huì)讓你能夠簡(jiǎn)單的覆寫你想要改變的操作欄樣式,而不影響你不想修改的樣式(如文本的外觀或操作項(xiàng)的邊緣)。 你能夠在清單文件中把定制的主題應(yīng)用到整個(gè)應(yīng)用程序或一個(gè)單獨(dú)的Activity對(duì)象,如:- <application android:theme="@style/CustomActivityTheme" ... />
復(fù)制代碼 6、高級(jí)樣式 如果需要比上述屬性更高級(jí)的樣式,可以在Activity的主題中包含android:actionBarStyle和android:actionBarSplitStyle屬性。這兩個(gè)屬性的每一個(gè)都指定了另一種能夠給操作欄定義各種屬性的樣式,包括帶有android:background、android:backgroundSplit、android:backgroundStacked屬性的不同背景。如果要覆蓋這些操作欄樣式,就要確保定義一個(gè)像Widget.Holo.ActionBar這樣的父操作欄樣式。例如,如果要改變操作欄背景,你可以使用下列樣式:- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <!-- the theme applied to the application or activity -->
- <style name="CustomActivityTheme" parent="@android:style/Theme.Holo">
- <item name="android:actionBarStyle">@style/MyActionBar</item>
- <!-- other activity and action bar styles here -->
- </style>
- <!-- style for the action bar backgrounds -->
- <style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar">
- <item name="android:background">@drawable/ab_background</item>
- <item name="android:backgroundStacked">@drawable/ab_background</item>
- <item name="android:backgroundSplit">@drawable/ab_split_background</item>
- </style>
- </resources>
復(fù)制代碼 四、詳細(xì)代碼編寫 1、整個(gè)項(xiàng)目中最核心的地方就是在res/values下的styles.xml文件中,這是自定義風(fēng)格樣式的資源文件,styles.xml:- <resources xmlns:android="http://schemas./apk/res/android">
- <style name="AppBaseTheme" parent="android:Theme"></style>
- <!-- 將自定義的style取名為CustomTheme,父類為Theme.Holo.Light,也就是說(shuō)默認(rèn)背景是白底黑字(如果是Theme.Holo,則默認(rèn)是黑底白字) -->
- <style name="CustomTheme" parent="android:style/Theme.Holo.Light">
- <!-- 這是item的背景修改,不按時(shí)透明,按時(shí)顯示綠色 -->
- <item name="android:selectableItemBackground">@drawable/ad_selectable_background</item>
- <item name="android:actionBarTabStyle">@style/MyActionBarTabStyle</item>
- <item name="android:actionBarTabTextStyle">@style/MyActionBarTabTextStyle</item>
- <item name="android:actionDropDownStyle">@style/MyDropDownNav</item>
- <item name="android:dropDownListViewStyle">@style/MyDropDownListView</item>
- </style>
- <!-- Tab選項(xiàng)標(biāo)簽的樣式 -->
- <style name="MyActionBarTabStyle" parent="android:style/Widget.Holo.Light.ActionBar.TabView">
- <item name="android:background">@drawable/actionbar_tab_bg</item>
- <item name="android:paddingLeft">22dp</item>
- <item name="android:paddingRight">22dp</item>
- </style>
- <!-- Tab選項(xiàng)標(biāo)簽字體的樣式 -->
- <style name="MyActionBarTabTextStyle" parent="android:style/Widget.Holo.Light.ActionBar.TabText">
- <item name="android:textColor">#ff0000</item>
- <item name="android:textSize">12sp</item>
- </style>
- <!-- 下拉導(dǎo)航外部按鈕的樣式 -->
- <style name="MyDropDownNav" parent="android:style/Widget.Holo.Light.Spinner">
- <item name="android:background">@drawable/ad_spinner_background_holo_light</item>
- <item name="android:popupBackground">@drawable/ad_menu_dropdown_panel_holo_light</item>
- <item name="android:dropDownSelector">@drawable/ad_selectable_background</item>
- </style>
-
- <!-- 下拉導(dǎo)航內(nèi)部按鈕的樣式 -->
- <style name="MyDropDownListView" parent="android:style/Widget.Holo.ListView.DropDown">
- <item name="android:listSelector">@drawable/ad_selectable_background</item>
- </style>
-
- </resources>
復(fù)制代碼 2、定義好了之后,在AndroidManifest.xml清單文件中使用,可以在application中使用,這樣就會(huì)作用于每一個(gè)Activity,也可以在每一個(gè)單獨(dú)的Activity中使用,AndroidManifest.xml:- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas./apk/res/android"
- package="com.yangyu.myactionbar05"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="11"
- android:targetSdkVersion="14" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:logo="@drawable/xianjian_logo"
- android:theme="@style/CustomTheme" >
-
- <activity
- android:name="com.yangyu.myactionbar05.MainActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
-
- </application>
- </manifest>
復(fù)制代碼 3、新建一個(gè)drawable文件夾,在文件夾下再定義幾個(gè)資源文件:<1> 用于tab選項(xiàng)標(biāo)簽背景的修改, actionbar_tab_bg.xml:- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas./apk/res/android">
- <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/ad_tab_unselected_holo" />
- <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/ad_tab_selected_pressed_holo" />
- <item android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/ad_tab_selected_pressed_holo" />
- <item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/ad_tab_selected_pressed_holo" />
- </selector>
復(fù)制代碼 <2> ActionBar標(biāo)題欄的背景漸變色,actionbar_gradient_bg.xml:- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas./apk/res/android"
- android:shape="rectangle" >
- <gradient
- android:angle="270"
- android:endColor="#FFEFEFEF"
- android:startColor="#cc1115"
- android:type="linear" />
- </shape>
復(fù)制代碼 <3> 用于下拉列表背景的變換,ad_spinner_background_holo_light.xml:- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas./apk/res/android">
- <item android:drawable="@drawable/spinner_disabled_holo_light" android:state_enabled="false"/>
- <item android:drawable="@drawable/ad_spinner_pressed_holo_light" android:state_pressed="true"/>
- <item android:drawable="@drawable/ad_spinner_focused_holo_light" android:state_focused="true" android:state_pressed="false"/>
- <item android:drawable="@drawable/spinner_default_holo_light"/>
- </selector>
復(fù)制代碼 <4> ActionBar標(biāo)題欄上的按鈕選中時(shí)的背景顏色切換,selected_background.xml:- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas./apk/res/android"
- android:shape="rectangle" >
- <solid android:color="#FFA4C639" />
- </shape>
復(fù)制代碼 ad_selectable_background.xml:- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas./apk/res/android" android:exitFadeDuration="@android:integer/config_mediumAnimTime">
- <item android:drawable="@drawable/selected_background" android:state_pressed="true"/>
- <item android:drawable="@android:color/transparent"/>
- </selector>
復(fù)制代碼 4、菜單的資源文件,main_menu.xml:- <menu xmlns:android="http://schemas./apk/res/android" >
- <!-- 自定義搜索按鈕視圖 -->
- <item
- android:id="@+id/menu_search"
- android:actionViewClass="android.widget.SearchView"
- android:showAsAction="ifRoom"
- android:title="@string/action_label_search"/>
- <!-- 刷新按鈕圖標(biāo) -->
- <item
- android:id="@+id/menu_refresh"
- android:icon="@drawable/ic_menu_refresh_holo_light"
- android:showAsAction="ifRoom"
- android:title="@string/action_label_refresh"/>
- <!-- 收藏按鈕圖標(biāo)加文字 -->
- <item
- android:id="@+id/menu_collected"
- android:icon="@drawable/ic_menu_star_holo_light"
- android:showAsAction="ifRoom|withText"
- android:title="@string/action_label_collected"/>
- </menu>
復(fù)制代碼 這里的資源文件有點(diǎn)多,為了節(jié)省篇幅,講解重點(diǎn)和難點(diǎn),博主這里就不一一列出來(lái)了,有需要的同學(xué)可以看源碼,一目了然。
5、主Activity程序入口類,MainActivity.java:- package com.yangyu.myactionbar05;
- import android.app.ActionBar;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.view.Menu;
- import android.view.MenuItem;
- /**
- * @author yangyu
- * 主Activity,程序入口類
- */
- public class MainActivity extends Activity {
- //定義ActionBar
- private ActionBar actionBar;
-
- //定義Handler對(duì)象
- private final Handler handler = new Handler();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- initView();
-
- initData();
- }
- /**
- * 初始化組件
- */
- private void initView(){
- //得到ActionBar
- actionBar = getActionBar();
- }
-
- /**
- * 初始化數(shù)據(jù)
- */
- private void initData(){
- //設(shè)置ActionBar標(biāo)題不顯示
- actionBar.setDisplayShowTitleEnabled(false);
-
- //設(shè)置ActionBar的背景
- actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_gradient_bg));
-
- //設(shè)置ActionBar左邊默認(rèn)的圖標(biāo)是否可用
- actionBar.setDisplayUseLogoEnabled(true);
-
- //設(shè)置導(dǎo)航模式為Tab選項(xiàng)標(biāo)簽導(dǎo)航模式
- actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
-
- //設(shè)置ActinBar添加Tab選項(xiàng)標(biāo)簽
- actionBar.addTab(actionBar.newTab().setText("TAB1").setTabListener(new MyTabListener<FragmentPage1>(this,FragmentPage1.class)));
- actionBar.addTab(actionBar.newTab().setText("TAB2").setTabListener(new MyTabListener<FragmentPage2>(this,FragmentPage2.class)));
- actionBar.addTab(actionBar.newTab().setText("TAB3").setTabListener(new MyTabListener<FragmentPage3>(this,FragmentPage3.class)));
-
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.main_menu, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(final MenuItem item) {
- switch (item.getItemId()) {
- case R.id.menu_refresh:
- //選中刷新按鈕后刷新一秒鐘
- item.setActionView(R.layout.actionbar_progress);
- handler.postDelayed(new Runnable() {
- public void run() {
- item.setActionView(null);
- }
- }, 1000);
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
-
- }
復(fù)制代碼 6、Tab選項(xiàng)標(biāo)簽監(jiān)聽(tīng)接口類,MyTabListener.java:- package com.yangyu.myactionbar05;
- import android.app.ActionBar.Tab;
- import android.app.ActionBar.TabListener;
- import android.app.Activity;
- import android.app.Fragment;
- import android.app.FragmentTransaction;
- /**
- * @author yangyu
- * 功能描述:Tab選項(xiàng)標(biāo)簽監(jiān)聽(tīng)接口
- */
- public class MyTabListener<T extends Fragment> implements TabListener {
- private Fragment fragment;
-
- private final Activity mActivity;
-
- private final Class<T> mClass;
-
- public MyTabListener(Activity activity, Class<T> clz){
- mActivity = activity;
-
- mClass = clz;
- }
-
- @Override
- public void onTabSelected(Tab tab, FragmentTransaction ft) {
- if(fragment == null){
- fragment = Fragment.instantiate(mActivity, mClass.getName());
- ft.add(android.R.id.content, fragment, null);
- }
- ft.attach(fragment);
- }
- @Override
- public void onTabUnselected(Tab tab, FragmentTransaction ft) {
- if (fragment != null) {
- ft.detach(fragment);
- }
- }
- @Override
- public void onTabReselected(Tab tab, FragmentTransaction ft) {
-
- }
- }
復(fù)制代碼 7、Fragment類,用于顯示Tab選項(xiàng)標(biāo)簽的內(nèi)容,列出其中一個(gè),F(xiàn)ragmentPage1.java:- package com.yangyu.myactionbar05;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- public class FragmentPage1 extends Fragment{
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
-
- return inflater.inflate(R.layout.fragment_1, null);
- }
- }
復(fù)制代碼 關(guān)于ActionBar的內(nèi)容總結(jié)到這里差不多就要結(jié)束了,今后如果有什么關(guān)于ActionBar好玩好用的東西再拿出來(lái)更大家一起分享、總結(jié)和交流。在這里博主希望大家能積極評(píng)論,指出文中的不足和錯(cuò)誤的地方,博主一定第一時(shí)間改正和完善。謝謝支持!下一篇開(kāi)始會(huì)講解自定義的頂部標(biāo)題欄,敬請(qǐng)期待!
MyActionBar05.zip
(7.77 MB, 下載次數(shù): 2013)
|