Android ActionBar的图标和标题

来源:互联网 发布:全球云计算公司排名 编辑:程序博客网 时间:2024/04/30 01:18

        好多Android开发者会经常用 requestWindowFeature(Window.FEATURE_NO_TITLE)去掉android系统的标题栏,然后自定义自己想要的效果。actionBar的引入,本人觉得在一些简单的标题处理上还是比较方便的,现总结一下经常用到的属性。

       

首先获取到ActionBar对象:ActionBar actionBar=getActionBar();

一般ActionBar默认的既有Logo又有Label,可以根据自己的需要有选择性的保留Logo还是Label,还是二者中的一个。

隐藏Label方法:actionBar.setDisplayShowTitleEnabled(false);反之置为true

隐藏Logo方法:actionBar.setDisplayShowHomeEnabled(false);反之置为true

隐藏掉Logo后左边还会存在一个系统默认的返回箭头,隐藏方法为:

actionBar.setDisplayHomeAsUpEnabled(false);

此外还有设置标题方法,一个主标题,一个子标题

actionBar.setSubtitle(“--”); 
actionBar.setTitle(“--”);


所以Label、Logo的显示很简单,是不是可以不用requestWindowFeature(Window.FEATURE_NO_TITLE)+自定义一个布局的方式了呢,相对来说代码少,简洁。


还有一点常用的就是给标题栏设置背景颜色,使标题栏跟整个APP颜色风格一致:

ActionBar actionBar = getActionBar();
Resources r = getResources();
Drawable myDrawable = r.getDrawable(R.drawable.xxx);
actionBar.setBackgroundDrawable(myDrawable);
这样就满足了我们的需求。

ActionBar最右侧的三个小点菜单点击也可以满足我们很多的需求,例如:

  

 @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, 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.action_search) {           // do something        } else if (id == R.id.action_about) {           // do something        }         return super.onOptionsItemSelected(item);    }

此外acitionbar还可以自定义,style、xml等等,这里不再赘述。


1 0
原创粉丝点击