安卓全部通用标题栏实现

万变不离其宗:

      安卓通用标题栏,不用每次都手动写,除了比较复杂的手动写,这边只是举个例子,布局也很简单,具体根据自己的需求添加

package com.dapeng.base_lib.base;


import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.Nullable;

import com.dapeng.base_lib.R;
import com.dapeng.base_lib.interfaces.IBaseView;
import com.dapeng.utils_lib.DPLogUtils;

public abstract class BaseActivity extends BasePermissionRequestActivity implements IBaseView {

    private String tag = getClass().getSimpleName();
    private Handler handler;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handler = new Handler(Looper.getMainLooper());
        DPLogUtils.errorLevel(tag, tag + "-- onCreate --");
    }

    /**
     * 方法1: Activity.getWindow().getDecorView().findViewById(android.R.id.content)
     * 方法2: Activity.findViewById(android.R.id.content)
     * 方法3: Activity.findViewById(android.R.id.content).getRootView()
     *
     * @return contentView 获取 activity 根view DecorView 相当于根布局
     */
    @Override
    public View getContentView() {
        return (ViewGroup) getWindow().getDecorView();
    }

    /**
     * 判断有没有添加标题栏  xml 文件添加 include< @layout ___自定义通用标题的布局> 
     * 
     * 判断用里面的一个 viewid 就可以 找不到即没有添加 当然排除 android studio 发病的情况,碰到过添加了也不行 然后找半天原因
     * 最后 clean project -> rebuild project ,还有终极操作 Invalidate caches/Restart 能解决大部分的 缓存乱七八糟的问题 比如:
     *  xml 文件写了viewid 但是 找不到id, color文件配置了颜色 找不到 等等资源相关的问题 一般都是 android studio的问题
     *  clean project -> rebuild project 终极操作 Invalidate caches/Restart 整个世界都清净了 
     */
    private boolean hasTitleLayout() {
        if (getContentView().findViewById(R.id.back_iv) == null) {
            throw new RuntimeException("xml not add include<--base_layout_title-->");
        } else {
            return true;
        }
    }

    /**
     * 需要先在 xml 布局中 添加 标题布局使用  把布局 include 进去
     *
     * @param isShow 是否显示返回按钮
     * @param title  标题内容
     */
    public void showBackIvTitleAndFunction(boolean isShow, CharSequence title) {
        if (hasTitleLayout()) {
            View backIv = getContentView().findViewById(R.id.back_iv);
            if (backIv == null) return;
            backIv.setVisibility(isShow ? View.VISIBLE : View.INVISIBLE);
            backIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });

            TextView titleTv = getContentView().findViewById(R.id.title_tv);
            if (titleTv == null) return;
            titleTv.setText(title);
        }
    }


    @Override
    public void showLoadingDialog() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("     正在加载中...");
        }

        // 判断是否是主线程 ,子线程不会显示的,不是主线程 给它 post 丢到主线程
        if (Looper.myLooper() == Looper.getMainLooper()) {
            progressDialog.show();
        } else {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    progressDialog.show();
                }
            });
        }
    }

    @Override
    public void dismissLoadingDialog() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("     正在加载中...");
        }
        if (!progressDialog.isShowing())
            return;
        if (Looper.myLooper() == Looper.getMainLooper()) {
            progressDialog.dismiss();
        } else {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    progressDialog.dismiss();
                }
            });
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        DPLogUtils.errorLevel(tag, tag + "-- onStart --");
    }

    @Override
    protected void onResume() {
        super.onResume();
        DPLogUtils.errorLevel(tag, tag + "-- onResume --");
    }

    @Override
    protected void onPause() {
        super.onPause();
        DPLogUtils.errorLevel(tag, tag + "-- onPause --");
    }

    @Override
    protected void onStop() {
        super.onStop();
        DPLogUtils.errorLevel(tag, tag + "-- onStop --");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DPLogUtils.errorLevel(tag, tag + "-- onDestroy --");
    }

}

布局代码,很简单,具体根据自己业务需要去设计:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/title_bar"
    android:layout_height="50dp"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#03DAC5">

    <FrameLayout
        android:id="@+id/back_iv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:src="@mipmap/back_left" />
    </FrameLayout>


    <TextView
        android:id="@+id/title_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="@android:color/white"
        android:textSize="14sp"
        tools:text="标题" />

</RelativeLayout>

把布局 include 进去到需要的页面

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <include layout="@layout/base_layout_title"/>

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:divider="@null"
        android:dividerHeight="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title_bar" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floating_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:backgroundTint="@color/colorPrimary"
        android:clickable="true"
        android:focusable="true"
        android:src="@drawable/ui_add_float"
        app:elevation="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

在 activity oncreate 方法  setContentView(R.layout.xxx) 之后调用

   showBackIvTitleAndFunction(true, "联系人")

基本效果:

fragment同理 ,万变不离其宗!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值