action bar 基本用法

  前言:好一段时间都没有写博客了,不是没时间写,而是最近这段时间又没坚持在学习了,反思一下!  其实在实际应用中早已接触过action bar,只是当时还不知道它的名字。看看下图,你是不是也感觉很熟悉:我们是否经常使用右边这个竖状省略号里隐藏的菜单?

  简介:ctionBar位于Activity的顶部,可用来显示activity的标题、Icon、Actions和一些用于交互的View。它也可被用于应用的导航。ActionBar 是在Android 3.0(API 11)中加入到SK中的,想在低版本中使用ActionBar有两种选择:使用http://actionbarsherlock.com 或使用Support Library v7。

  使用:

    1.在res/menu下创建一个布局文件,在该布局文件中指定actions:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/action_refresh"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:icon="@drawable/ic_action_refresh"
        android:title="Refresh"/>
    <item
        android:id="@+id/action_settings"
        android:title="Settings">
    </item>
</menu> 

  比较关键的一个属性是showAsAction,该属性有三个值:

      a."always"表示永远显示在ActionBar中;

      b."ifRoom"表示如果空间足够,则显示在ActionBar中;

      c.“never”表示永远不显示;

    2.在onCreateOptionsMenu方法中加载:

@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);// 获取当前的菜单 并填充菜单
		return true;
	}

    3.单击事件监听:

Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // action with ID action_refresh was selected
    case R.id.action_refresh:
      Toast.makeText(this, "Refresh selected", Toast.LENGTH_SHORT)
          .show();
      break;
    // action with ID action_settings was selected
    case R.id.action_settings:
      Toast.makeText(this, "Settings selected", Toast.LENG
          .show();
      break;
    default:
      break;
    }
    return true;
  } 

  4.几个常用的操作:

    a.显示、隐藏action bar: getActionBar().show(); getActionBar().hide();

    b.改变程序图标旁边的标题内容:getActionBar().setSubTitle("test");   getActionBar().setTitle('dean');

    c.隐藏导航按钮: getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

 

未完、待续。

 

posted on 2015-11-09 22:05  小太阳550  阅读(578)  评论(0编辑  收藏  举报

导航