1.首先去掉原先系统自带的actionBar,在api 11 后才出现actionBar的概念,在v11的value的style.xml的文件中修改,如果没有的话默认的value就是v11
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:actionBarStyle">@style/myActvionBar</item>
</style>
<style name="myActvionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<!-- 去掉actionBar的左边图标 -->
<item name="android:displayOptions">@null</item>
</style>
2.在layout文件夹中写出actionBar的布局文件
3.在Java代码中加载布局
/**
* 设置ActionBar
* @param context
* @param layoutId
*/
public static void setActonBarLayout(Context context,int layoutId) {
ActionBar actionBar = ((Activity)context).getActionBar();
if (actionBar != null) {
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = layoutInflater.inflate(layoutId, null);
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
v.setLayoutParams(layoutParams);
actionBar.setCustomView(v);
}
}
最终的效果: