android使用设计模式打造标题栏

从大学出来实习已经有半年了,之前只写过一篇笔记就没有下文了。
现在来重新记录自己的学习。
年前回家前,给自己定一个小目标,一个星期一两篇android的笔记,有空的时候写写博客,对以后开发还有见解有一定的帮助,所以撸起袖子就是干。
今天写一个用builder设计模式来写一个标题栏,关于builder这个设计模式我就不介绍了

1,首先先定义好一个接口,这个接口里,我就不一一介绍了,代码里有注释

public interface ItoolBarBuild {
    //设置标题
    public ItoolBarBuild setTitle(int title);
    //设置左边的按钮
    public ItoolBarBuild setLeftIcon(int leftIcon);
    //设置右边的按钮
    public ItoolBarBuild setRightIcon(int rightIcon);
    //设置左边的点击
    public ItoolBarBuild setLeftIconOnClickListener(View.OnClickListener onClickListener);
    //设置右边的点击
    public ItoolBarBuild setRightIconOnClickListener(View.OnClickListener onClickListener);
    //绑定view
    public void addIn(ViewGroup group);
}

2.写一个ToolBarBuild类来实现ItoolBarBuild 接口

public abstract class ToolBarBuild implements ItoolBarBuild {
    private int title;
    private int leftIcon;
    private int rightIcon;
    private Context mContext;
    private View contentView;
    View.OnClickListener leftIconOnClickListener;
    View.OnClickListener rightIconOnClickListener;
    public ToolBarBuild(Context context){
        this.mContext = context;
    }
    @Override
    public ItoolBarBuild setTitle(int title) {
        this.title = title;
        return this;
    }
    
    @Override
    public ItoolBarBuild setLeftIcon(int leftIcon) {
        this.leftIcon = leftIcon;
        return this;
    }

    @Override
    public ItoolBarBuild setRightIcon(int rightIcon) {
        this.rightIcon = rightIcon;
        return this;
    }

    @Override
    public ItoolBarBuild setLeftIconOnClickListener(View.OnClickListener onClickListener) {
        this.leftIconOnClickListener = onClickListener;
        return this;
    }

    @Override
    public ItoolBarBuild setRightIconOnClickListener(View.OnClickListener onClickListener) {
        this.rightIconOnClickListener = onClickListener;
        return this;
    }

    public int getLeftIcon() {
        return leftIcon;
    }

    public int getTitle() {
        return title;
    }

    public int getRightIcon() {
        return rightIcon;
    }

    public Context getmContext() {
        return mContext;
    }

    public View.OnClickListener getLeftIconOnClickListener() {
        return leftIconOnClickListener;
    }

    public View.OnClickListener getRightIconOnClickListener() {
        return rightIconOnClickListener;
    }

    @Override
    public void addIn(ViewGroup parent) {
        //加载标题栏的视图
        contentView = LayoutInflater.from(getmContext()).inflate(getToolBarLayout(),parent,false);//将layout写成一个抽象的getToolBarLayout方法
        //获取该view的父组件
        ViewGroup viewGroup = (ViewGroup) contentView.getParent();
        //如果该视图的父组件已经存在就将remove掉
        if (viewGroup!=null){
            viewGroup.removeView(contentView);
        }
        //将view加入从外面传进来的ViewGroup里,标题栏都是在最顶上,所以在第二个参数传入0
        parent.addView(contentView,0);
    }
    //实例化ImageView
    public void setImageView(int id, int res , View.OnClickListener onClickListener) {
        ImageView imageView = (ImageView) contentView.findViewById(id);
        imageView.setVisibility(View.VISIBLE);
        imageView.setImageResource(res);
        imageView.setOnClickListener(onClickListener);
    }
    //实例化TextView
    public void setTextView(int id, int res , View.OnClickListener onClickListener){
        TextView textView  = (TextView) contentView.findViewById(id);
        textView.setVisibility(View.VISIBLE);
        textView.setOnClickListener(onClickListener);
        textView.setText(res);
    }
    public abstract int getToolBarLayout();

}

3,在写一个ToolBarView去继承ToolBarBuild;

public class ToolBarView extends ToolBarBuild {

    public ToolBarView(Context context) {
        super(context);
    }

    @Override
    public int getToolBarLayout() {
        return R.layout.toolbar_layout;
    }

    @Override
    public void addIn(ViewGroup parent) {
        super.addIn(parent);
        //假如在toolbar_layout设置了TextView的text,ImageView的图片而且并不用实现点击事件的话可以不调用
        //在第二个参数如果传入0,该空间就会隐藏
        setImageView(R.id.iv_left,getLeftIcon(),getLeftIconOnClickListener());
        
        setImageView(R.id.iv_right,getRightIcon(),getRightIconOnClickListener());
        //如果需要动态设置的话,但是不用点击事件的话直接在第三个参数传null,就可以了
        setTextView(R.id.tv_title,getTitle(),null);
    }
}

标题栏的视图如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:background="#79b2f9">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="center_vertical">
        <ImageView
            android:id="@+id/iv_left"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginLeft="@dimen/margin_left_12_dp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="center">
        <TextView
            android:id="@+id/tv_title"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="center_vertical|right">
        <ImageView
            android:id="@+id/iv_right"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_marginRight="@dimen/margin_left_12_dp"/>
    </LinearLayout>

</LinearLayout>

这样子标题栏就算是完成了,下面我们来使用它;

public class MainActivity extends AppCompatActivity {
    LinearLayout linearLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ToolBarView toolBarView = new ToolBarView(getBaseContext());
        linearLayout = (LinearLayout) findViewById(R.id.ll_content);
        toolBarView.setTitle(R.string.title)
                    .setLeftIcon(R.drawable.left_icon)
                    .setRightIcon(R.drawable.right_icon)
                    .setLeftIconOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(getBaseContext(),"点击左边按钮",Toast.LENGTH_SHORT).show();
                        }
                    })
                    .setRightIconOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(getBaseContext(),"点击右边按钮",Toast.LENGTH_SHORT).show();
                        }
                    })
                    .addIn(linearLayout);//一定要调用这个方法,不然就没有实现这个标题栏

    }
}

运行结果:


QQ截图20170212235748.png
Paste_Image.png

基本的标题栏就是左中右的布局,但是也有一些不一样的
在这里我介绍写一种其他的,并在原来的代码添加一些代码,感觉代码这样封装也是挺灵活的,2333....
直接在ToolBarView里添加两个跟ToolBarView的超类里类似的方法;
如下所示

public class ToolBarView extends ToolBarBuild {
    private int res;
    private View.OnClickListener listener;
    public ToolBarView(Context context) {
        super(context);
    }

    @Override
    public int getToolBarLayout() {
        return R.layout.toolbar_layout;
    }
    public ToolBarView setOtherRightIcon(int res){
        this.res = res;
        return this;
    }
    public ToolBarView setOtherOnClickListener(View.OnClickListener listener){
        this.listener = listener;
        return this;
    }
    @Override
    public void addIn(ViewGroup parent) {
        super.addIn(parent);
        //假如在toolbar_layout设置了TextView的text,ImageView的图片而且并不用实现点击事件的话可以不调用
        //在第二个参数如果传入0,该空间就会隐藏
        setImageView(R.id.iv_left,getLeftIcon(),getLeftIconOnClickListener());

        setImageView(R.id.iv_right,getRightIcon(),getRightIconOnClickListener());
        //如果需要动态设置的话,但是不用点击事件的话直接在第三个参数传null,就可以了
        setTextView(R.id.tv_title,getTitle(),null);
        
        setImageView(R.id.iv_right_other,res,listener);
    }
}

运行结果:

Paste_Image.png

就这样搞定了;
其他的情况也是同样的思路;
欢迎交流,我还是一个小菜鸟,android基础不太牢固,虽然出来实习半年了,但是其中干了两个月的前端,有空写写js跟android的交互和开发的时候遇到的那些问题解决方法;虽然网上也有很多教程,但是还是想自己写笔记,好记性不如烂笔头;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,050评论 25 707
  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,658评论 22 664
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,945评论 4 60
  • 其实,说起这个标题,我好想的并不是某一个人,或者某一个物品更加不是某一个地方。我好想的是一种生活模式,在那样的生活...
    Ge珊珊阅读 243评论 0 1
  • 昨晚的一场小雪,终于驱散了烦人多日的雾霾!傲娇的阳光,也露出笑颜了!于是空气清新的扑面而来!冬日的阳光总是认人心暖...
    金典生活阅读 222评论 1 1