1,Theme引用的style中添加
<style name="AppTheme" parent="AppBaseTheme">
<item name = "android:actionBarSize">50dp</item>
</style>
item自定义actionBar的高度,在布局中写高度是无效的
2,写actionBar的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00b38b" >
<TextView
android:id="@+id/tv_actionbar_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20px"
android:drawableLeft="@drawable/finish_act"
android:gravity="center_vertical"
android:text="前页"
android:textColor="#fff"
android:textSize="30px" />
<TextView
android:id="@+id/tv_actionbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="标题栏"
android:textColor="#fff"
android:textSize="36px" />
<TextView
android:id="@+id/tv_actionbar_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="20px"
android:gravity="center_vertical"
android:text="刷新"
android:textColor="#fff"
android:textSize="30px"
android:visibility="gone" />
</RelativeLayout>
3,在Activity中代码初始化actionBar
private boolean initCustomActionBar() {
mActionbar = getActionBar();
if (mActionbar == null) {
return false;
}
mActionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
mActionbar.setCustomView(R.layout.actionbar);
tv_actionbar = (TextView) mActionbar.getCustomView().findViewById(
R.id.tv_actionbar);
tv_actionbar.setText("任务详情");
tv_actionbar_left = (TextView) mActionbar.getCustomView().findViewById(
R.id.tv_actionbar_left);
tv_actionbar_left.setText("巡检");
tv_actionbar_left.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
return true;
}
然后在onCreate()调用initCustomActionBar
注:ActionBar在Android5.0以后已被ToolBar替代