统一为项目中的Activity添加Toolbar

栏目: IOS · Android · 发布时间: 7年前

内容简介:最近因为项目里用到了大量的Toolbar 在学姐的提问下想着如何让封装toolbar 使Toolbar更通用更好看代码看起来更简洁 之前想着是把Toolbar重写 最后在网上看到了很多人是用BaseActivity来实现Toolbar 让自己写的Activity直接去继承写的BaseActivity 这样实现下来真的方便了许多因为自己的代码量还是很少 平常很喜欢用网易云 就准备一点一点模仿网易云作为练手学习Demo 所以这里实现了网易云音乐里的几个Tolbar几张原生截图

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

举个例子

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

几张原生截图

统一为项目中的Activity添加Toolbar
统一为项目中的Activity添加Toolbar
统一为项目中的Activity添加Toolbar
统一为项目中的Activity添加Toolbar

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

统一为项目中的Activity添加Toolbar

我的

统一为项目中的Activity添加Toolbar

代码实现

在项目初期,都会有一个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 网络框架 及其页面跳转的各个情况的封装 加油哇!!!


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

编写可维护的JavaScript

编写可维护的JavaScript

扎卡斯 / 李晶、郭凯、张散集 / 人民邮电出版社 / 2013-4 / 55.00元

《编写可维护的JavaScript》向开发人员阐述了如何在团队开发中编写具备高可维护性的JavaScript代码,书中详细说明了作为团队一分子,应该怎么写JavaScript。《编写可维护的JavaScript》内容涵盖了编码风格、编程技巧、自动化、测试等几方面,既包括具体风格和原则的介绍,也包括示例和技巧说明,最后还介绍了如何通过自动化的工具和方法来实现一致的编程风格。 《编写可维护的Ja......一起来看看 《编写可维护的JavaScript》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具