Android應(yīng)用開發(fā)中Action bar編寫的入門教程
從Android 3.0開始除了我們重點(diǎn)講解的Fragment外,Action Bar也是一個(gè)重要的內(nèi)容,Action Bar主要是用于代替?zhèn)鹘y(tǒng)的標(biāo)題欄,對于Android平板設(shè)備來說屏幕更大它的標(biāo)題使用Action Bar來設(shè)計(jì)可以展示更多豐富的內(nèi)容,方便操控。
Action Bar主要功能包含:
1. 顯示選項(xiàng)菜單
2. 提供標(biāo)簽頁的切換方式的導(dǎo)航功能,可以切換多個(gè)fragment.
3. 提供下拉的導(dǎo)航條目.
4. 提供交互式活動(dòng)視圖代替選項(xiàng)條目
5. 使用程序的圖標(biāo)作為返回Home主屏或向上的導(dǎo)航操作。
提示在你的程序中應(yīng)用ActionBar需要注意幾點(diǎn),SDK和最終運(yùn)行的固件必須是Android 3.0即honeycomb,在androidmanifest.xml文件中的uses-sdk元素中加入android:minSdkVersion 或android:targetSdkVersion,類似
< manifest xmlns:android="http://schemas.android.com/apk/res/android" package="eoe.android.cwj" android:versionCode="1" android:versionName="1.0"> < uses-sdk android:minSdkVersion="honeycomb" /> < application ... > < /application> < /manifest>
如果需要隱藏Action Bar可以在你的Activity的屬性中設(shè)置主題風(fēng)格為NoTitleBar在你的manifest文件中,下面的代碼在3.0以前是隱藏標(biāo)題,而在3.0以后就是隱藏ActionBar了,代碼為:
< activity android:theme="@android:style/Theme.NoTitleBar">
一、添加活動(dòng)條目 Action Items
對于活動(dòng)條目大家可以在下圖看到Android 3.0的標(biāo)題右部分可以變成工具欄,下面的Save和Delete就是兩個(gè)Action Items活動(dòng)條目。
下面是一個(gè)menu的layout布局文件代碼
< ?xml version="1.0" encoding="utf-8"?> < menu xmlns:android="http://schemas.android.com/apk/res/android"> < item android:id="@+id/menu_add" android:icon="@drawable/ic_menu_save" android:title="@string/menu_save" android:showAsAction="ifRoom|withText" /> < /menu>
而其他代碼類似Activity中的Menu,比如
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // 當(dāng)Action Bar的圖標(biāo)被單擊時(shí)執(zhí)行下面的Intent Intent intent = new Intent(this, Android123.class); startActivity(intent); break; } return super.onOptionsItemSelected(item); }
對于ActionBar的創(chuàng)建,可以在你的Activity中重寫onStart方法:
@Override protected void onStart() { super.onStart(); ActionBar actionBar = this.getActionBar(); actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP); }
調(diào)用getActionBar方式在你的Activity的onCreate中時(shí)需要注意必須在調(diào)用了setContentView之后。
二、添加活動(dòng)視圖 Action View
對于ActionView,我們可以在menu的布局文件使用中來自定義searchview布局,如下:
< item android:id="@+id/menu_search" android:title="Search" android:icon="@drawable/ic_menu_search" android:showAsAction="ifRoom" android:actionLayout="@layout/searchview" />
也可以直接指定Android系統(tǒng)中的SearchView控件,那么這時(shí)menu"_search的代碼要這樣寫:
< item android:id="@+id/menu_search" android:title="Search" android:icon="@drawable/ic_menu_search" android:showAsAction="ifRoom" android:actionViewClass="android.widget.SearchView" />
大家注意上面的兩種方法中一個(gè)屬性是actionLayout制定一個(gè)layout xml布局文件,一個(gè)是actionViewClass指定一個(gè)類,最終調(diào)用可以在Activity中響應(yīng)onCreateOptionsMenu方法映射這個(gè)menu布局即可.
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.options, menu); SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); return super.onCreateOptionsMenu(menu); }
三、添加標(biāo)簽 Tabs
在ActionBar中實(shí)現(xiàn)標(biāo)簽頁可以實(shí)現(xiàn)android.app.ActionBar.TabListener ,重寫onTabSelected、onTabUnselected和onTabReselected方法來關(guān)聯(lián)Fragment。代碼如下:
private class MyTabListener implements ActionBar.TabListener { private TabContentFragment mFragment; public TabListener(TabContentFragment fragment) { mFragment = fragment; } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { ft.add(R.id.fragment_content, mFragment, null); } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { ft.remove(mFragment); } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { } }
接下來我們創(chuàng)建ActionBar在Activity中,代碼如下;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final ActionBar actionBar = getActionBar(); //提示getActionBar方法一定在setContentView后面 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); Fragment artistsFragment = new ArtistsFragment(); actionBar.addTab(actionBar.newTab().setText(R.string.tab_artists).setTabListener(new TabListener(artistsFragment))); Fragment albumsFragment = new AlbumsFragment(); actionBar.addTab(actionBar.newTab().setText(R.string.tab_albums).setTabListener(new TabListener(albumsFragment))); }
四、添加下拉導(dǎo)航 Drop-down Navigation:
創(chuàng)建一個(gè)SpinnerAdapter提供下拉選項(xiàng),和Tab方式不同的是Drop-down只需要修改下setNavigationMode的模式,將ActionBar.NAVIGATION_MODE_TABS改為ActionBar.NAVIGATION_MODE_LIST,最終改進(jìn)后的代碼為
ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); actionBar.setListNavigationCallbacks(mSpinnerAdapter, mNavigationCallback);
上面我們通過setListNavigationCallbacks方法綁定一個(gè)SpinnerAdapter控件,具體的OnNavigationListener代碼示例為;
mOnNavigationListener = new OnNavigationListener() { String[] strings = getResources().getStringArray(R.array.action_list); @Override public boolean onNavigationItemSelected(int position, long itemId) { ListContentFragment newFragment = new ListContentFragment(); FragmentTransaction ft = openFragmentTransaction(); ft.replace(R.id.fragment_container, newFragment, strings[position]); ft.commit(); return true; } };
而其中的ListContentFragment的代碼為:
public class ListContentFragment extends Fragment { private String mText; @Override public void onAttach(Activity activity) { super.onAttach(activity); mText = getTag(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView text = new TextView(getActivity()); text.setText(mText); return text; } }
五、實(shí)現(xiàn)切換Tabs標(biāo)簽;
Activity代碼:
public class ActionBarTabs extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.action_bar_tabs); } public void onAddTab(View v) { final ActionBar bar = getActionBar(); final int tabCount = bar.getTabCount(); final String text = "Tab " + tabCount; bar.addTab(bar.newTab().setText(text) .setTabListener(new TabListener(new TabContentFragment(text)))); } public void onRemoveTab(View v) { final ActionBar bar = getActionBar(); bar.removeTabAt(bar.getTabCount() - 1); } public void onToggleTabs(View v) { final ActionBar bar = getActionBar(); if (bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) { bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE); } else { bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); } } public void onRemoveAllTabs(View v) { getActionBar().removeAllTabs(); } private class TabListener implements ActionBar.TabListener { private TabContentFragment mFragment; public TabListener(TabContentFragment fragment) { mFragment = fragment; } public void onTabSelected(Tab tab, FragmentTransaction ft) { ft.add(R.id.fragment_content, mFragment, mFragment.getText()); } public void onTabUnselected(Tab tab, FragmentTransaction ft) { ft.remove(mFragment); } public void onTabReselected(Tab tab, FragmentTransaction ft) { Toast.makeText(ActionBarTabs.this, "Reselected!", Toast.LENGTH_SHORT).show(); } } private class TabContentFragment extends Fragment { private String mText; public TabContentFragment(String text) { mText = text; } public String getText() { return mText; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View fragView = inflater.inflate(R.layout.action_bar_tab_content, container, false); TextView text = (TextView) fragView.findViewById(R.id.text); text.setText(mText); return fragView; } } }
涉及的布局文件action_bar_tabs.xml代碼為:
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> < FrameLayout android:id="@+id/fragment_content" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" /> < LinearLayout android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" android:orientation="vertical"> < Button android:id="@+id/btn_add_tab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_add_tab" android:onClick="onAddTab" /> < Button android:id="@+id/btn_remove_tab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_remove_tab" android:onClick="onRemoveTab" /> < Button android:id="@+id/btn_toggle_tabs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_toggle_tabs" android:onClick="onToggleTabs" /> < Button android:id="@+id/btn_remove_all_tabs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_remove_all_tabs" android:onClick="onRemoveAllTabs" /> < /LinearLayout> < /LinearLayout>
布局文件action_bar_tab_content.xml;
< ?xml version="1.0" encoding="utf-8"?> < TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" />
相關(guān)文章
cv2.getStructuringElement()函數(shù)及開、閉、腐蝕、膨脹原理講解
getStructuringElement()函數(shù)可用于構(gòu)造一個(gè)特定大小和形狀的結(jié)構(gòu)元素,用于圖像形態(tài)學(xué)處理,這篇文章主要介紹了cv2.getStructuringElement()函數(shù)及開、閉、腐蝕、膨脹原理講解的相關(guān)資料,需要的朋友可以參考下2022-12-12python之Flask實(shí)現(xiàn)簡單登錄功能的示例代碼
這篇文章主要介紹了python之Flask實(shí)現(xiàn)簡單登錄功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12利用pyproj將經(jīng)緯度投影為平面坐標(biāo)以及地理坐標(biāo)系背景知識解讀
這篇文章主要介紹了利用pyproj將經(jīng)緯度投影為平面坐標(biāo)以及地理坐標(biāo)系背景知識解讀,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Python使用ffmpeg合成視頻、音頻的實(shí)現(xiàn)方法
這篇文章主要介紹了Python使用ffmpeg合成視頻、音頻,通過本文的學(xué)習(xí)能幫助大家了解如何在python中調(diào)用ffmpeg模塊,對此進(jìn)行音視頻合并,完成視頻合成,需要的朋友可以參考下2022-04-04Python?numpy生成矩陣基礎(chǔ)用法實(shí)例代碼
矩陣是matrix類型的對象,該類繼承自numpy.ndarray,任何針對ndarray的操作,對矩陣對象同樣有效,下面這篇文章主要給大家介紹了關(guān)于Python?numpy生成矩陣基礎(chǔ)的相關(guān)資料,需要的朋友可以參考下2022-08-08python實(shí)現(xiàn)簡單的井字棋游戲(gui界面)
這篇文章主要介紹了python如何實(shí)現(xiàn)簡單的井字棋游戲,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01Python自動(dòng)化辦公之Word文件內(nèi)容的讀取
word、excel、PPT,雖然說是特殊文件,其實(shí)也是實(shí)際工作中我們經(jīng)常會(huì)用到的文件類型。本文將為大家詳解Python讀取Word文件和文件內(nèi)容的方法,感興趣的可以了解一下2022-05-05