Android 沉侵式标题栏toolbar

1.开发环境:Android stuido4.0 Java1.8
2.测试设备:红米Note 4x,Oppo k1,Oppo R11(测试正常,标题栏不占用状态栏,状态栏可以正常设置为亮色和神色)
3.布局文件:toolbar_layout.xml




    

        

        

        

        
    

4.Activity基类 BaseToolBarActivity.java

import android.os.Build;
import android.os.Bundle;
import android.view.View;

import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.sol.novel.R;
import com.sol.novel.utils.Tools;

import butterknife.ButterKnife;

public abstract class BaseToolBarActivity extends AppCompatActivity {

    //可以在 initConfig中设置
    protected boolean isSetResult = false;
    protected int resultCode = 0;
    protected boolean isUseNormalToolbar = true;//默认使用默认的Toolbar
    protected boolean isUseIntrusiveToolbar = true;//默认使用侵入式的Toolbar(状态栏背景色与toolbar相同)
    /**
     * toolbar相关
     */
    protected TextView toolbarTitle, toolbarRightText;
    protected ImageButton toolbarRightImageBtn;
    protected RelativeLayout toolbar_innerBox;
    protected int statuBarBgColor = R.color.toolbar_bg_color;
    protected View rootView = null;
    protected boolean islightStatusBar = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initConfig();
        setConfigAbout();
        setContentView(setContentViewLayoutId());
        ButterKnife.bind(this);
        initNormalToolbar();
        initView();
        setConfigAboutAfterInitView();
        initData();
    }

    private void initNormalToolbar() {
        if (isUseNormalToolbar) {
            toolbarTitle = findViewById(R.id.toolbar_title);
            toolbarRightText = findViewById(R.id.toolbar_rightText);
            toolbarRightImageBtn = findViewById(R.id.toolbar_rightImageBtn);
            toolbar_innerBox = findViewById(R.id.toolbar_innerBox);
            findViewById(R.id.toolbar_backIcon).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isSetResult) {
                        setResult(resultCode);
                    }
                    finish();
                }
            });
        }
    }

    /**
     * 需要返回上一个Activity才设置
     */
    protected void setResultCode(int resultCode) {
        isSetResult = true;
        this.resultCode = resultCode;
    }

    /**
     * 初始化配置之后使之生效
     * 参考:
     * https://www.jianshu.com/p/e104cf35d061
     * https://blog.csdn.net/weixin_41870092/article/details/102660191?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-1
     */
    protected void setConfigAbout() {
        if (isUseIntrusiveToolbar) {
            Window window = getWindow();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(getResources().getColor(statuBarBgColor));
                View decor = window.getDecorView();
                int ui = decor.getSystemUiVisibility();
                ui |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (islightStatusBar) {
                        ui |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                    } else {
                        ui &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                    }
                    decor.setSystemUiVisibility(ui);
                }
            }
        }
    }

    private void setConfigAboutAfterInitView() {
        if (!isUseIntrusiveToolbar && toolbar_innerBox != null) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    Tools.dip2px(this, 50));
            toolbar_innerBox.setLayoutParams(params);
        }
    }

    protected abstract void initConfig();

    protected abstract int setContentViewLayoutId();

    protected abstract void initView();

    protected abstract void initData();
}

5.Activity实例:ToolbarActivity .Java

import com.example.testany.R;

public class ToolbarActivity extends BaseToolBarActivity {

    @Override
    protected void initConfig() {

        isUseIntrusiveToolbar = false;
    }

    @Override
    protected int setContentViewLayoutId() {

        return R.layout.activity_toolbar;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        toolbarTitle.setText("用户充值");
    }
}

你可能感兴趣的:(Android基础)