Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android ActionBar的使用

android ActionBar的使用

編輯:關於Android編程


Action Bar主要功能包含:
1. 顯示選項菜單
2. 提供標簽頁的切換方式的導航功能,可以切換多個fragment.
3. 提供下拉的導航條目.
4. 提供交互式活動視圖代替選項條目
5. 使用程序的圖標作為返回Home主屏或向上的導航操作。


首先說下,使用OverFlow的時候需要在onCreate()函數中調用如下方法:

private void forceShowOverflowMenu() {  
    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();  
    }  
}

注:此處未使用這種實現方式,本應用中使用的PopupMen


以下是自己開發的項目所使用到的ActionBar:

public class ShopOrderActivity extends ActionBarActivity implements
		OnTouchListener, OnMenuItemClickListener{
	private Toast mToast;
	private Context context;
	/** AlertDialog中輸入反饋框 */
	private EditText et_FeedBack;
	private PopupMenu popupMenu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shop_order);

		//設置Home圖標區域
//		requestWindowFeature(Window.FEATURE_LEFT_ICON);
//		setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, resId);
		
		this.context = this;
		mToast = Toast.makeText(this, "", Toast.LENGTH_LONG);

		initActionBar();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.actionbar_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.actionbar_overflow) {
			if (popupMenu == null) {
				popupMenu = new PopupMenu(this,
						findViewById(R.id.actionbar_overflow));
				popupMenu.inflate(R.menu.actionbar_pop);
				popupMenu.setOnMenuItemClickListener(this);
			}
			popupMenu.show();
			return true;
		} else if (
public class ShopOrderActivity extends ActionBarActivity implements
		OnTouchListener, OnMenuItemClickListener{
	private Toast mToast;
	private Context context;
	/** AlertDialog中輸入反饋框 */
	private EditText et_FeedBack;
	private PopupMenu popupMenu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shop_order);

		//設置Home圖標區域
//		requestWindowFeature(Window.FEATURE_LEFT_ICON);
//		setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, resId);
		
		this.context = this;
		mToast = Toast.makeText(this, "", Toast.LENGTH_LONG);

		initActionBar();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.actionbar_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.actionbar_overflow) {
			if (popupMenu == null) {
				popupMenu = new PopupMenu(this,
						findViewById(R.id.actionbar_overflow));
				popupMenu.inflate(R.menu.actionbar_pop);
				popupMenu.setOnMenuItemClickListener(this);
			}
			popupMenu.show();
			return true;
		} else if (id == android.R.id.home) {
			finish();
		} else if (id == R.id.new_order_bar) {
			showTip("顯示紅點"); 
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onMenuItemClick(MenuItem arg0) {
		Intent intent;
		switch (arg0.getItemId()) {
		case R.id.actionbar_settings:
			intent = new Intent(this, SettingsActivity.class);
			startActivity(intent);
			break;
		case R.id.historyOrder:
			intent = new Intent(this, HistoryOrderActivity.class);
			startActivity(intent);
			break;
		case R.id.logout:
			AlertDialog.Builder builder = new AlertDialog.Builder(
					ShopOrderActivity.this);
			builder.setTitle("確定要退出嗎?");
			builder.setPositiveButton("確定",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							showTip("退出登錄!");
						}
					});
			builder.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
			builder.create().show();
			break;
		}
		return false;
	}

	/**
	 * 初始化ActionBar
	 */
	private void initActionBar() {
		ActionBar actionBar = getSupportActionBar();
		actionBar.setTitle("搶單");
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setHomeButtonEnabled(true);
		// actionBar.setIcon(R.drawable.back_menu);
		// Drawable background = (Drawable) getResources()
		// .getDrawable(R.drawable.top_bg);
		// getActionBar().setBackgroundDrawable(background);
		setTitleColor(this.getResources().getColor(R.color.green));// 沒反應
	}

	/**
	 * 顯示Toast
	 * 
	 * @param str
	 */
	public void showTip(final String str) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mToast.setText(str);
				mToast.show();
			}
		});
	}
}


id == android.R.id.home
當操作左上角icon圖標的時候實現的功能是返回,配置清單需要設置:

android:name="com.shop.order.ShopOrderActivity"
android:launchMode="singleTop"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar" >

actionbar_menu的代碼:



    
    
    
    

注意:app:showAsAction="ifRoom|withText"如果寫成android:showAsAction="ifRoom|withText"則不會在actionbar顯示,當操作手機右下角的menu鍵時才會顯示


actionbar_pop的代碼:



    
    
    



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved