统一为项目中的Activity添加Toolbar

前言

最近因为项目里用到了大量的Toolbar 在学姐的提问下想着如何让封装toolbar 使Toolbar更通用更好看代码看起来更简洁 之前想着是把Toolbar重写 最后在网上看到了很多人是用BaseActivity来实现Toolbar 让自己写的Activity直接去继承写的BaseActivity 这样实现下来真的方便了许多

举个例子

因为自己的代码量还是很少 平常很喜欢用网易云 就准备一点一点模仿网易云作为练手学习Demo 所以这里实现了网易云音乐里的几个Tolbar

几张原生截图

这四个Toolbar就是下面这四个button进入的

我的

代码实现

在项目初期,都会有一个BaseActivity来做一些统一性的操作,然后所有Activity统一继承。

主要代码:

public class BaseActivity extends AppCompatActivity {
    //通用的Toolbar标题
    private TextView commonTitleTv;
    //通用的ToolBar
    private Toolbar commonTitleTb;
    //内容区域
    private FrameLayout content;
    //右上角的图标
    private ImageView img;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        initView();
        setSupportActionBar(commonTitleTb);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
    }

    private void initView(){
        commonTitleTv = findViewById(R.id.commom_title);
        commonTitleTb = findViewById(R.id.toolbar);
        content = findViewById(R.id.content);
        img = findViewById(R.id.commom_img);
    }

    //子类调用 重新设置Toolbar
    public void setToolBar(int layout){
        hidetoolBar();
        commonTitleTb = content.findViewById(layout);
        setSupportActionBar(commonTitleTb);
        //设置actionbar标题是否显示 对应ActionBar.DISPLAY_SHOW_TITLE
        getSupportActionBar().setDisplayShowTitleEnabled(false);
    }

    //隐藏Toolbar 通过setToolbar重新制定Toolbar
    public void hidetoolBar(){
        commonTitleTb.setVisibility(View.GONE);
    }

    //menu的点击事件
    public void setToolBarMenuOnClick(Toolbar.OnMenuItemClickListener onClick){
        commonTitleTb.setOnMenuItemClickListener(onClick);
    }

    //设置左上角back按钮
    public void setBackArrow(){
        final Drawable upArrow = getResources().getDrawable(R.drawable.back);
        //给Toolbar设置左侧的图标
        getSupportActionBar().setHomeAsUpIndicator(upArrow);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //设置返回按钮的点击事件
        commonTitleTb.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //设置右上角的图标
    public void setRightImagine(@DrawableRes int imgId){
        img.setImageResource(imgId);
    }

    //设置toolbar下面内容区域的内容
    public void setContentLayout(int layoutId){
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(layoutId,null);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        content.addView(contentView,params);
    }

    //设置标题
    public void setTitle(String title){
        if(!TextUtils.isEmpty(title)){
            commonTitleTv.setText(title);
        }
    }

    //设置标题
    public void setTitle(int resId){
        commonTitleTv.setText(resId);
    }
}
复制代码

BaseActivity中的代码可以根据自己的需要灵活改变 也可以将BaseActivity改为抽象类 让Activity可以重写抽象方法 其中 setContentLayout()方法很重要 他添加了toolbar及其他它下面的内容

activity_base.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_base"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:id="@+id/toolbar"
        android:background="@color/colorPrimary">

        <TextView
            android:id="@+id/commom_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:text="我是标题"
            android:textSize="20sp"
            android:textColor="@color/white"/>

        <ImageView
            android:id="@+id/commom_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"/>

    </android.support.v7.widget.Toolbar>

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

</LinearLayout>
复制代码

FrameLayout是用来放Activity中的内容的。

RecommendedDailyActivity中的代码:

public class RecommendedDailyActivity extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("每日推荐");
        setBackArrow();
        setRightImagine(R.drawable.question);
    }
}
复制代码

最近看了很多代码模块的封装 因为觉得自己的项目写的很冗杂 想要减少重复性 后面会在练习些BaseFragment RecyclerView 网络框架 及其页面跳转的各个情况的封装 加油哇!!!

猜你喜欢

转载自juejin.im/post/5c666da0e51d45635c5375e7