首页 > 代码库 > 6. ActionBar详解

6. ActionBar详解

?ActionBar简介

? 隐藏和显示ActionBar

隐藏ActionBar      getActionBar().hide(); 

显示ActionBar      getActionBar().show(); 

? 添加和响应Action按钮

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     android:layout_width="match_parent" 3     android:layout_height="match_parent" 4     android:orientation="vertical" > 5     <Button 6         android:id="@+id/button_hide_show_actionbar" 7         android:layout_width="match_parent" 8         android:layout_height="wrap_content" 9         android:onClick="onClick_HideShowActionBar"10         android:text="隐藏ActionBar" />11 </LinearLayout>
 1 package cn.eoe.first.actionbar; 2  3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.Menu; 6 import android.view.MenuInflater; 7 import android.view.MenuItem; 8 import android.view.View; 9 import android.view.Window;10 import android.widget.Button;11 import android.widget.Toast;12 13 public class FirstActionBarActivity extends Activity {14     private Button hideShowActionBar;15 16     @Override17     protected void onCreate(Bundle savedInstanceState) {18         super.onCreate(savedInstanceState);19         getWindow().requestFeature(Window.FEATURE_ACTION_BAR);20         setContentView(R.layout.activity_first_action_bar);21         hideShowActionBar = (Button) findViewById(R.id.button_hide_show_actionbar);22     }23 24     public void onClick_HideShowActionBar(View view) {25         if (getActionBar() == null)26             return;27         if (getActionBar().isShowing()) {28             getActionBar().hide();29             hideShowActionBar.setText("显示ActionBar");30         } else {31             getActionBar().show();32             hideShowActionBar.setText("隐藏ActionBar");33         }34     }35 36     @Override37     public boolean onCreateOptionsMenu(Menu menu) {38         MenuInflater inflater = getMenuInflater();39         inflater.inflate(R.menu.activity_first_action_bar, menu);40         // menu.getItem(2).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);41         return true;42     }43 44     @Override45     public boolean onOptionsItemSelected(MenuItem item) {46         Toast.makeText(this, item.getTitle(), Toast.LENGTH_LONG).show();47         return super.onOptionsItemSelected(item);48     }49 50 }
 1 <?xml version="1.0" encoding="utf-8"?> 2 <menu xmlns:android="http://schemas.android.com/apk/res/android" > 3  4     <item 5         android:id="@+id/menu_save" 6         android:icon="@drawable/save" 7         android:showAsAction="ifRoom|withText" 8         android:title="保存"/> 9     <item10         android:id="@+id/menu_open"11         android:icon="@drawable/open"12         android:showAsAction="ifRoom"13         android:title="打开"/>14     <item15         android:id="@+id/menu_search"16         android:icon="@drawable/search"17         android:showAsAction="always"18         android:title="保存"/>19     <item20         android:id="@+id/menu1"21         android:icon="@drawable/ic_launcher"22         android:showAsAction="ifRoom"23         android:title="菜单项1"/>24     <item25         android:id="@+id/menu2"26         android:icon="@drawable/ic_launcher"27         android:showAsAction="ifRoom"28         android:title="菜单项2"/>29    30 </menu>

 

 

? 可上下两分的Action

 1 package cn.eoe.split.actionbar; 2  3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.view.Menu; 6 import android.view.MenuInflater; 7  8 public class SplitActionBar extends Activity { 9 10     @Override11     protected void onCreate(Bundle savedInstanceState) {12         super.onCreate(savedInstanceState);13         setContentView(R.layout.activity_split_action_bar);14     }15 16     @Override17     public boolean onCreateOptionsMenu(Menu menu) {18         MenuInflater inflater = getMenuInflater();19         inflater.inflate(R.menu.activity_split_action_bar, menu);20         return true;21     }22 }
 1 <?xml version="1.0" encoding="utf-8"?> 2 <menu xmlns:android="http://schemas.android.com/apk/res/android" > 3  4     <item 5         android:id="@+id/menu_save" 6         android:icon="@drawable/open" 7         android:showAsAction="always|withText"/> 8     <item 9         android:id="@+id/menu_open"10         android:icon="@drawable/open"11         android:showAsAction="always"12         android:title="打开"/>13     <item14         android:id="@+id/menu_search"15         android:icon="@drawable/search"16         android:showAsAction="always"17         android:title="保存"/>18     <item19         android:id="@+id/menu1"20         android:icon="@drawable/ic_launcher"21         android:showAsAction="always"22         android:title="菜单项1"/>23     <item24         android:id="@+id/menu2"25         android:icon="@drawable/ic_launcher"26         android:showAsAction="always"27         android:title="菜单项2"/>28 29 </menu>

 

? 收缩和展开Action View 

 1 import android.annotation.SuppressLint; 2 import android.app.Activity; 3 import android.os.Bundle; 4 import android.view.Menu; 5 import android.view.MenuItem; 6 import android.view.MenuItem.OnActionExpandListener; 7 import android.widget.Toast; 8  9 @SuppressLint("NewApi")10 public class ActionViewActivity extends Activity implements11         OnActionExpandListener {12     // private Menu menu;13 14     @Override15     protected void onCreate(Bundle savedInstanceState) {16         super.onCreate(savedInstanceState);17         setContentView(R.layout.activity_action_view);18     }19 20     @Override21     public boolean onCreateOptionsMenu(Menu menu) {22         getMenuInflater().inflate(R.menu.activity_action_view, menu);23         // this.menu = menu;24         for (int i = 0; i < menu.size(); i++) {25             menu.getItem(i).setOnActionExpandListener(this);26         }27         return true;28     }29 30     @Override31     public boolean onMenuItemActionExpand(MenuItem item) {32         Toast.makeText(this, "<" + item.getTitle() + ">已经展开", Toast.LENGTH_LONG)33                 .show();34 35         return true;36     }37 38     @Override39     public boolean onMenuItemActionCollapse(MenuItem item) {40         Toast.makeText(this, "<" + item.getTitle() + ">已经收缩", Toast.LENGTH_LONG)41                 .show();42         return true;43     }44 45 }

res/menu/activity_action_view.xml

 1 <menu xmlns:android="http://schemas.android.com/apk/res/android" > 2  3     <item 4         android:id="@+id/menu_item_edit" 6         android:actionViewClass="android.widget.EditText" 8         android:showAsAction="always" 9         android:title="编辑"/>11     <item12         android:id="@+id/menu_item_clock"13         android:actionLayout="@layout/action_view_clock"14         android:icon="@drawable/time"15      16         android:showAsAction="always|collapseActionView"17         android:title="时间"/>18     <item19         android:id="@+id/menu_item_search"20         android:actionViewClass="android.widget.SearchView"21         android:icon="@drawable/search"23         android:showAsAction="always|collapseActionView"24         android:title="搜素"/>25 26 </menu>