android 自定义actionbar(左右两边的空白)

工作环境(蓝色粗体字为特别注意内容)
1,系统环境:Win7 Ultimate sp1、Android Studio 3.2

2、手机环境:Android 5.1.1

网上找了很多相关代码,要不就是无法实现自定义actionbar,要不就是自定义actionbar左右两边无法充满整个父布局,很是郁闷,最终找到以下方案

/**
     * set up the actionbar
     */
    private void setupActionBar() {
        // 首先在actionbar里头加载自定义的确定和取消的布局
        final LayoutInflater inflater = (LayoutInflater) getSupportActionBar().getThemedContext()
                .getSystemService(LAYOUT_INFLATER_SERVICE);
        final View customActionBarView = inflater.inflate(
                R.layout.comm_action_bar, null);
        mRightImg = (ImageView) customActionBarView.findViewById(R.id.bar_right_img);
        //   loginedBtn.setOnClickListener(this);

        final ActionBar actionBar = getSupportActionBar();
        // 设置actionbar,并且隐藏返回按钮和title
        actionBar.setDisplayOptions(
                ActionBar.DISPLAY_SHOW_CUSTOM,
                ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
                        | ActionBar.DISPLAY_SHOW_TITLE);
        // actionbar设置自定义的布局和布局参数
        actionBar.setCustomView(customActionBarView,
                new ActionBar.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));
        //to avoid the space in tab left and right side
        Toolbar parent = (Toolbar) customActionBarView.getParent();
        parent.setPadding(0, 0, 0, 0);
        parent.setContentInsetsAbsolute(0, 0);

    }

记得自定义view的根布局中:android:layout_width="match_parent",并且:<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

猜你喜欢

转载自blog.csdn.net/pang9998/article/details/88031636