Android应用开发之Android用注解打造一个简易的TitleView
白羽 2018-12-03 来源 :网络 阅读 298 评论 0

摘要:本文将带你了解Android应用开发之Android用注解打造一个简易的TitleView,希望本文对大家学Android有所帮助。

    本文将带你了解Android应用开发之Android用注解打造一个简易的TitleView,希望本文对大家学Android有所帮助。


<

最近手上在同时做两个项目,有点疲于奔命,博客都无暇顾及;
但是也得出一个心得:做一个项目,首先要审视UI给你的图和后台给你的需求,找到复用之处,这是快速开发的第一步。
所以TitleView能不封装吗?
我的A项目的title(仿微信的title)

B项目的title

所以只需要简单封装下返回的按键就可以了
至于title name,可以采用注解,免去几行代码,进行优雅的声明,如下所示:

全部代码
java
<strong>public class </strong>TitleView <strong>extends </strong>LinearLayout {     <strong>private </strong>Context <strong>context</strong>;     <strong>public </strong>TitleView(Context context) {        <strong>super</strong>(context);        init();    }     <strong>public </strong>TitleView(Context context, @Nullable AttributeSet attrs) {        <strong>super</strong>(context, attrs);        <strong>this</strong>.<strong>context </strong>= context;        init();    }     <strong>public </strong>TitleView(Context context, @Nullable AttributeSet attrs, <strong>int </strong>defStyleAttr) {        <strong>super</strong>(context, attrs, defStyleAttr);        init();    }     <strong>private void </strong>init() {        ViewGroup rootView = (ViewGroup) LayoutInflater.from(<strong>context</strong>).inflate(R.layout.<strong><em>title</em></strong>, <strong>this</strong>, <strong>true</strong>);         View backIcon = MyUtils.getChildAt(rootView, 0, 0);        View backText = MyUtils.getChildAt(rootView, 0, 1);        TextView tvTitle = (TextView) MyUtils.getChildAt(rootView, 0, 2);         <em>//活动finish监听</em><em>        </em>View.OnClickListener onClickListener = <strong>new </strong>OnClickListener() {            @Override            <strong>public void </strong>onClick(View v) {                ((Activity)<strong>context</strong>).finish();            }        };        backIcon.setOnClickListener(onClickListener);        backText.setOnClickListener(onClickListener);         <em>//注解搜寻title</em><em>        </em>String titleName = <strong>context</strong>.getClass().getAnnotation(TitleName.<strong>class</strong>).titleName();        tvTitle.setText(titleName);    }}
xml
<!--<strong-->xml version=<strong>"1.0" </strong><strong>encoding=</strong><strong>"utf-8"</strong>><<strong>FrameLayout </strong><strong>xmlns:</strong><strong>android</strong><strong>=</strong><strong>"//schemas.android.com/apk/res/android"</strong><strong>    </strong><strong>android</strong><strong>:layout_width=</strong><strong>"match_parent"</strong><strong>    </strong><strong>android</strong><strong>:layout_height=</strong><strong>"40dp"</strong><strong>    </strong><strong>xmlns:</strong><strong>tools</strong><strong>=</strong><strong>"//schemas.android.com/tools"</strong><strong>    </strong><strong>android</strong><strong>:background=</strong><strong>"#686f78"</strong>>     <<strong>ImageView</strong><strong>        </strong><strong>android</strong><strong>:layout_width=</strong><strong>"20dp"</strong><strong>        </strong><strong>android</strong><strong>:layout_height=</strong><strong>"20dp"</strong><strong>        </strong><strong>android</strong><strong>:layout_marginStart=</strong><strong>"4dp"</strong><strong>        </strong><strong>android</strong><strong>:layout_gravity=</strong><strong>"center_vertical"</strong><strong>        </strong><strong>android</strong><strong>:background=</strong><strong>"@drawable/title_back"</strong><strong>        </strong><strong>android</strong><strong>:layout_centerVertical=</strong><strong>"true"</strong>/>     <<strong>TextView</strong><strong>        </strong><strong>android</strong><strong>:layout_width=</strong><strong>"wrap_content"</strong><strong>        </strong><strong>android</strong><strong>:layout_height=</strong><strong>"match_parent"</strong><strong>        </strong><strong>android</strong><strong>:gravity=</strong><strong>"center"</strong><strong>        </strong><strong>android</strong><strong>:textColor=</strong><strong>"#FFFFFF"</strong><strong>        </strong><strong>android</strong><strong>:layout_marginStart=</strong><strong>"21dp"</strong><strong>        </strong><strong>android</strong><strong>:layout_gravity=</strong><strong>"center_vertical"</strong><strong>        </strong><strong>android</strong><strong>:text=</strong><strong>"返回" </strong>/>     <<strong>TextView</strong><strong>        </strong><strong>android</strong><strong>:id=</strong><strong>"@+id/tv_title"</strong><strong>        </strong><strong>android</strong><strong>:layout_width=</strong><strong>"wrap_content"</strong><strong>        </strong><strong>android</strong><strong>:layout_height=</strong><strong>"wrap_content"</strong><strong>        </strong><strong>android</strong><strong>:layout_gravity=</strong><strong>"center"</strong><strong>        </strong><strong>tools</strong><strong>:text=</strong><strong>"选择小区"</strong><strong>        </strong><strong>android</strong><strong>:textColor=</strong><strong>"#FFFFFF" </strong>/> <!--<strong-->FrameLayout>
还有一个心得,就是常用的颜色要放到colors里,距离要放到dimen里,因为一个UI的风格是固定的,他认为好看的颜色和间距,其颜色代码和间隔距离都是一样的。
最后补充一点,如果右上角还有控件,比如加号什么的,你有3个方法去做:
1.addView,设置一定的布局参数,代码量相比最大
2.本来就放在那里,只是进行隐藏,代码量最小,但是也意味着每个应用这个标题的容器都具有这个控件
3.放一个view stub在那里,代码量适中,但是解决了第二条的那个缺点
最后再补充一点,如果点击返回后面你还有另外的操作咋办?
来个代理模式?来个jdk1.8后的新特性传递方法?
小老板,生命周期可以用起来了,有无数个方法你可以重写的。    

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!

本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程