玩轉android之Action bar
玩轉android之Action bar
背景:
在Android3.0之後,Google對UI導航設計上進行了一系列的改革,其中有一個非常好用的新功能就是引入的ActionBar,他用於取代3.0之前的標題列,並提供更為豐富的導航效果。
常用屬性:
1.showAsAction:
當你的應用程式目標設為蜂巢平台時,你可以利用Action Bar組件提供的全部功能,將你的選項功能表項目放在Action Bar的右上方,對使用者來說使用更方便,控制該行為的主功能表項目屬性是android:showAsAction。
這個屬性可接受的值有:
1、always:這個值會使功能表項目一直顯示在Action Bar上。
2、ifRoom:如果有足夠的空間,這個值會使功能表項目顯示在Action Bar上。
3、never:這個值使功能表項目永遠都不出現在Action Bar上。
4、withText:這個值使功能表項目和它的表徵圖,菜單文本一起顯示。
常用功能:搜尋、分享、重新整理、下拉式清單、更多
目標:
實現:
MainActivity.java
package com.hitwh.readscarf;import java.lang.reflect.Field;import android.app.ActionBar;import android.app.ActionBar.OnNavigationListener;import android.app.Activity;import android.content.Intent;import android.os.AsyncTask;import android.os.Bundle;import android.view.Menu;import android.view.MenuInflater;import android.view.MenuItem;import android.view.ViewConfiguration;import android.widget.ArrayAdapter;import android.widget.ShareActionProvider;import android.widget.Toast;public class MainActivity extends Activity { private String[] actions = new String[] { Action-1, Action-2, Action-3 }; private MenuItem menuItem = null; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 顯示ActionBarActionBar actionBar = getActionBar();actionBar.show();// 顯示擴充menugetOverflowMenu();// 顯示DropDownList/** Create an array adapter to populate dropdownlist */ ArrayAdapter adapter = new ArrayAdapter( getBaseContext(), android.R.layout.simple_spinner_dropdown_item, actions); /** Enabling dropdown list navigation for the action bar */ actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); /** Setting dropdown items and item navigation listener for the actionbar */ actionBar.setListNavigationCallbacks(adapter, navigationListener); } /** * 預設 Navigation listener **/ ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() { @Override public boolean onNavigationItemSelected(int itemPosition, long itemId) { Toast.makeText(getBaseContext(), You selected : + actions[itemPosition], Toast.LENGTH_SHORT).show(); return false; } }; /** * 建立 Menu */@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.// 載入 MenuMenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); // 載入分享menu ShareActionProvider provider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider(); // 初始化 Share Intent Intent intent = new Intent(Intent.ACTION_SEND); intent.setType(text/plain); intent.putExtra(Intent.EXTRA_TEXT, Text I want to share); provider.setShareIntent(intent); return true;}/** * Menu 選擇事件 */@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// TODO Auto-generated method stubswitch (item.getItemId()) { // 重新整理menu case R.id.refresh: menuItem = item; menuItem.setActionView(R.layout.progressbar); TestTask task = new TestTask(); task.execute(test); break; // 儲存menu case R.id.save: Toast.makeText(this, Menu Item save selected, Toast.LENGTH_SHORT).show(); break; // 搜尋menu case R.id.search: Toast.makeText(this, Menu Item search selected, Toast.LENGTH_SHORT).show(); break; // 分享menu case R.id.share: Toast.makeText(this, Menu Item share selected, Toast.LENGTH_SHORT).show(); break; default: break; } return super.onOptionsItemSelected(item); }/** * 顯示更多菜單 */private void getOverflowMenu() { try { ViewConfiguration config = ViewConfiguration.get(this); Field menuKeyField = ViewConfiguration.class.getDeclaredField(sHasPermanentMenuKey); if(menuKeyField != null) { menuKeyField.setAccessible(true); menuKeyField.setBoolean(config, false); } } catch (Exception e) { e.printStackTrace(); }}/** * 顯示載入進度條 * @author Mr-rxz * */private class TestTask extends AsyncTask { @Override protected String doInBackground(String... params) { // Simulate something long running try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String result) { // 這個方法需要 API 14 以上 menuItem.collapseActionView(); menuItem.setActionView(null); } }; }