欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android 沉侵式标题栏toolbar

程序员文章站 2022-04-01 10:43:24
1.开发环境:Android stuido4.0 Java1.82.测试设备:红米Note 4x,Oppo k1,Oppo R11(测试正常,标题栏不占用状态栏,状态栏可以正常设置为亮色和神色)3.布局文件:toolbar_layout.xml

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbarOutBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white">

    <RelativeLayout
        android:id="@+id/toolbar_innerBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="40dp">

        <ImageView
            android:id="@+id/toolbar_backIcon"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:background="@mipmap/back_black" />

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:textColor="@android:color/black"
            android:textSize="18dp" />

        <TextView
            android:id="@+id/toolbar_rightText"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="15dp"
            android:gravity="center_vertical|right"
            android:paddingLeft="6px"
            android:paddingRight="6px"
            android:textColor="@android:color/black"
            android:textSize="15sp"
            android:visibility="gone" />

        <ImageButton
            android:id="@+id/toolbar_rightImageBtn"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="15dp"
            android:background="@color/colorPrimary"
            android:visibility="gone" />
    </RelativeLayout>
</LinearLayout>

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("用户充值");
    }
}

本文地址:https://blog.csdn.net/yingtian648/article/details/107188835