Android ActionBar自定义

1.设置action bar的背景色 

第一种方式:getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.ActionBarBackground));

ActionBarBackground 为定义在values/colors.xml

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="ActionBarBackground">#ED4026</drawable>
</resources>

此种方法特点:简单方便,但必须在代码中设置,若不同activity公用同一个actionbar样式,则不利于复用 。 so, 参考第二种方式

 

第二种方式:由官方文档给出,通过设置主题的属性实现。(不同activity可复用)

具体做法: (以下方法适合3.0及以上,2.1及以上参考官方文档)

res/values/themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/actionbar_background</item>
    </style>
</resources>

Then apply your theme to your entire app or individual activities:
<application android:theme="@style/CustomActionBarTheme" ... />
也可在建立项目后application或activity默认引用的主题样式文件styles.xml中直接修改添加
修改 <style name="AppBaseTheme" parent="@android:style/Theme.Holo"> 
或添加以上文件内容到styles.xml
actionbar_background可添加在colors.xml文件中


colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="ActionBarBackground">#ED4026</drawable>
<drawable name="actionbar_background">#ED4026</drawable>
</resources>

 
2.修改左侧图标 提供向上导航(自定义导航图标)
第一步:Manifest设置activity的父子关系 android:parentActivityName指定父activity
参考: http://www.cnblogs.com/vagrant1991/p/4319567.html
第二步:
actionBar.setDisplayHomeAsUpEnabled(false); 不显示《
bar.setDisplayShowHomeEnabled(true); 显示icon或logo作为home affordance(官方说法)可通过android.R.id.home识别

actionBar.setLogo(resId); 显示自定义Logo 或actionBar.setIcon(resId);

第三步:通过NavUtils函数实现向上导航  

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
  case android.R.id.home:
  NavUtils.navigateUpFromSameTask(this);
  return true;
}
  return super.onOptionsItemSelected(item);
}

ps: 也可以不通过NavUtils类实现向上导航,直接通过设置setDisplayHomeAsUpEnabled(true);但此种方法将使用系统默认的向上导航图标《 ,或通过如下代码所示通过控件的点击事件实现导航。

 3. 添加居中文字 可通过自定义布局实现  自定义CustomeView

如:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton android:id="@+id/left_btn"
android:contentDescription="@string/register_left_btn_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="12dp"
android:layout_gravity="start|center_vertical"
android:src="@drawable/ic_action_back"
android:background="@drawable/ActionBarBackground"/>
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/register_title"
android:textColor="#FFFFFF"
android:textSize="20sp"/>
<TextView android:id="@+id/right_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="12dp"
android:layout_gravity="center_vertical|end"
android:text="@string/register_right_text"
android:textColor="#FFFFFF"
android:textSize="18sp"/>
</FrameLayout>

然后在相应activity代码中实现相应功能逻辑 

如:onCreate()中核心代码

 

ActionBar bar = getActionBar();
bar.setDisplayHomeAsUpEnabled(false); //不显示《《
bar.setDisplayShowHomeEnabled(false); //不显示home affordance
bar.setDisplayShowTitleEnabled(false); // 不显示title
bar.setDisplayShowCustomEnabled(true); //显示自定义View

 

View CustomTitle = getLayoutInflater().inflate(R.layout.action_bar_title, null);
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
bar.setCustomView(CustomTitle, lp);
ImageButton navButton = (ImageButton)bar.getCustomView().findViewById(R.id.left_btn);
navButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
NavUtils.navigateUpFromSameTask(registerActivity.this);
}
});

 

效果图:

 

参考:http://blog.csdn.net/jwzhangjie/article/details/28852967

        http://blog.csdn.net/yueqinglkong/article/details/20876401

        http://blog.csdn.net/sumakira/article/details/7069179

        http://blog.csdn.net/jwzhangjie/article/details/28852967

        http://bbs.csdn.net/topics/390843333

    http://blog.csdn.net/android2me/article/details/8874846

        http://www.baidu.com/index.php?tn=utf8kb_oem_dg

posted on 2015-03-07 22:03  流浪者的五月  阅读(988)  评论(0编辑  收藏  举报

导航