Android--ActionBar的基本用法

來源:互聯網
上載者:User

標籤:oar   並且   導航   over   copyto   ack   title   下拉   val   

 一  說明
android 3.0後出現, 在3.0之前稱為Title Bar  顯示位置在標題列上
可以顯示應用程式的表徵圖和activity的標題
建立方式的和系統功能表相似, 區別在於: android:showAsAction=""
ifRoom  當ActionBar中有空間 則顯示
always  一直顯示在ActionBar中
never 永遠都不顯示在ActionBar中
withText  顯示文字
collapseActionView   可以摺疊

  二,  ActionBar中提供的功能
1, 顯示選項菜單的功能表項目
2, 可以使應用程式的表徵圖作為返回上一級介面的功能
3, 提供了互動式View 和 action View
4, 可以實現基於Tab的導航方式, 可以切換Fragment
5, 下拉導航
6, ActionProvider的使用
三, 建立方式
1, 在res/menu 檔案中建立每一個功能表項目  聲明<item/>
顯示方式:  showAsAction 
表徵圖:       icon
2, 在Activity中, 重寫父類的 onCreateOptionsMenu(Menu menu) ,載入菜單資源
3, 在Activity中, 重寫父類的 onOptionsItemSelected(MenuItem item) 處理每個Item的點擊事件

四,分割 操作欄   API 14以上  
在資訊清單檔中<application/> 或者<activity/> 中添加屬性:android:uiOptions="splitActionBarWhenNarrow"
五, 啟動互動中心圖示
作用:  可以讓當前App的表徵圖作為可以點擊的表徵圖
擷取ActionBar的對象:  getActionBar()
setDisplayShowHomeEnabled   設定是否顯示應用程式的表徵圖
setDisplayHomeAsUpEnabled    設定應用程式的表徵圖可以被點擊, 並且在表徵圖的左側出現一個向左的箭頭
setHomeButtonEnabled設定應用程式的表徵圖可以被點擊,但是沒有箭頭
        得到應用程式互動中心圖示的ID:   android.R.id.home

六, ActionBar中的常用方法
actionBar.isShowing()  判斷當前的ActionBar是否正在顯示
actionBar.show()       顯示ActionBar
actionBar.hide()       隱藏ActionBar
七, Action View  的使用
可以編輯動作項, 如searchView控制項可以直接顯示在ActionBar
實現方式有兩種:
1, actionViewClass 屬性  實現了CollapsibleActionView類
2, actionLayout屬性   把普通的布局頁面顯示
八, ActionBar Tab 導航的功能   通過選項標籤來切換Fragment
ActionBar + Fragment
1, 擷取ActionBar的對象, 並且設定導航的模式為TABS
     setNavigationMode (int mode) 設定導航模式
    NAVIGATION_MODE_STANDARD   預設
    NAVIGATION_MODE_LIST列表方式
    NAVIGATION_MODE_TABStab標籤模式
2, 讓當前類實現介面 TabListener , 重寫3個方法
3, 建立 每個Tab項 並且把它增加到ActionBar中
ActionBar.Tab tab = ActionBar.newTab();
tab.setIcon();//設定表徵圖
tab.stText();//設定要顯示的文字
tab.setTabListener();//設定監聽

actionBar.add(tab);//把Tab增加到ActionBar中

九:去除ActionbBar 

          在setContentView之前 requestWindowFeature(Window.FEATURE_NO_TITLE);
           或者:Android:theme="@android:style/Theme.Black.NoTitleBar"

實現Fragment切換執行個體:

 

[java] view plain copy  print?
  1. protected void onCreate(Bundle savedInstanceState) {  
  2.         super.onCreate(savedInstanceState);  
  3.         setContentView(R.layout.activity_main);  
  4.   
  5.         actionBar = getActionBar();  
  6.         // 獲得導航模式  
  7.         actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);  
  8.         // 建立ActionBar.Tab 項, 並且把它增加到ActionBar中  
  9.         ActionBar.Tab tab = actionBar.newTab();  
  10.   
  11.         tab.setText("頭條");  
  12.         tab.setIcon(R.drawable.ic_launcher);  
  13.         tab.setTabListener(this);  
  14.         actionBar.addTab(tab);  
  15.   
  16.         tab = actionBar.newTab();  
  17.         tab.setText("體育");  
  18.         tab.setIcon(R.drawable.ic_launcher);  
  19.         tab.setTabListener(this);  
  20.         actionBar.addTab(tab);  
  21.   
  22.         actionBar.addTab(actionBar.newTab().setText("財經")  
  23.                 .setIcon(R.drawable.ic_launcher).setTabListener(this));  
  24.   
  25.         actionBar.addTab(actionBar.newTab().setText("汽車")  
  26.                 .setIcon(R.drawable.ic_launcher).setTabListener(this));  
  27.     }  
  28.   
  29.     @Override  
  30.     public void onTabSelected(Tab tab, FragmentTransaction ft) {  
  31.         // TODO Auto-generated method stub  
  32.         FragmentNew f = new FragmentNew();  
  33.         Bundle b = new Bundle();  
  34.         int position = tab.getPosition();  
  35.         b.putInt("position", position);  
  36.         f.setArguments(b);  
  37.         ft.replace(R.id.f, f);  
  38.   
  39.     }  
  40.   
  41.     @Override  
  42.     public void onTabUnselected(Tab tab, FragmentTransaction ft) {  
  43.         // TODO Auto-generated method stub  
  44.   
  45.     }  
  46.   
  47.     @Override  
  48.     public void onTabReselected(Tab tab, FragmentTransaction ft) {  
  49.         // TODO Auto-generated method stub  
  50.   
  51.     }  
  52. }  
[java] view plain copy  print?
  1. public class FragmentNew extends Fragment {  
  2.     @Override  
  3.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  4.             Bundle savedInstanceState) {  
  5.         // TODO Auto-generated method stub  
  6.         TextView t = new TextView(getActivity());  
  7.         int position = getArguments().getInt("position");  
  8.   
  9.         switch (position) {  
  10.         case 0:  
  11.             t.setText("頭條");  
  12.             break;  
  13.         case 1:  
  14.             t.setText("體育");  
  15.             break;  
  16.         case 2:  
  17.             t.setText("財經");  
  18.             break;  
  19.         case 3:  
  20.             t.setText("汽車");  
  21.             break;  
  22.   
  23.         }  
  24.   
  25.         return t;  
  26.     }  
  27. }  

 

[html] view plain copy  print?
    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    2.     xmlns:tools="http://schemas.android.com/tools"  
    3.     android:layout_width="match_parent"  
    4.     android:layout_height="match_parent"  
    5.     android:paddingBottom="@dimen/activity_vertical_margin"  
    6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
    7.     android:paddingRight="@dimen/activity_horizontal_margin"  
    8.     android:paddingTop="@dimen/activity_vertical_margin"  
    9.     tools:context=".MainActivity" >  
    10.   
    11.     <FrameLayout  
    12.         android:id="@+id/f"  
    13.         android:layout_width="match_parent"  
    14.         android:layout_height="match_parent" />  
    15.   
    16. </RelativeLayout>  

Android--ActionBar的基本用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.