ActionBar自定义布局


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替代

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值