自定义控件相关知识从郭霖等大神身上学习,这里只不过加上自己的理解和实践,绝非抄袭。
 
组合控件是自定义控件中最简单的方式,但是是入门自定义控件和进阶的过程:
那么常见的组合控件有那些?
比如titlebar和视图中常见的可重用界面布局的可用都可以通过组合控件的方式来进行自定义,并通过向其他类暴露方法和回调来实现对视图内容显示,隐藏,图片展示,动画活动,文字内容的控制。
废话这么多,写个组合控件来加深影响
首先思考下,titleBar应该包含那些内容,对了左中右三组控件,分别是左右按钮和中间的标题栏,但是有的页面不现实右边按钮,或者其他控件不显示,那么就会存在视图组件的隐藏和显示的问题。那么还有什么问题,对了,title的颜色或者左右按钮字体的颜色和文字的内容。
废话真多,你倒是写个组件让我们看看呀。
 
好吧,撸码走起:
xml布局文件:
    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:id="@+id/rl_top"
    3. android:layout_width="fill_parent"
    4. android:layout_height="@dimen/top_height"
    5. android:background="#f8f8f8" >
    6. <Button
    7. android:id="@+id/btn_top_back"
    8. android:layout_width="wrap_content"
    9. android:layout_height="match_parent"
    10. android:layout_centerVertical="true"
    11. android:gravity="center"
    12. android:background="@drawable/btn_top_back"
    13. android:textSize="20sp" />
    14. <TextView
    15. android:id="@+id/tv_top_title"
    16. android:layout_width="wrap_content"
    17. android:layout_height="wrap_content"
    18. android:layout_centerInParent="true"
    19. android:gravity="center"
    20. android:text="@string/app_name"
    21. android:textColor="@color/black"
    22. android:textSize="20sp" />
    23. <Button
    24. android:id="@+id/btn_top_right"
    25. android:layout_width="wrap_content"
    26. android:layout_height="fill_parent"
    27. android:layout_alignParentRight="true"
    28. android:layout_centerVertical="true"
    29. android:layout_marginRight="@dimen/image_corner_radius"
    30. android:background="@null"
    31. android:gravity="center"
    32. android:textColor="@drawable/btn_top_right_selector"
    33. android:textSize="16sp" />
    34. <TextView
    35. android:layout_width="fill_parent"
    36. android:layout_alignParentBottom="true"
    37. android:layout_height="1dp"
    38. android:background="@color/gray" />
    39. </RelativeLayout>


组合控件的写法:在构造方法里面加载布局文件和初始化视图,通过写一些公共的方法控制组件显示和隐藏,以及内容来控制TitleView上面的内容。
    1. public class TitleView extends FrameLayout {
    2. private Button btn_top_back;
    3. private TextView tv_top_title;
    4. private Button btn_top_right;
    5. public TitleView(Context context, AttributeSet attrs) {
    6. super(context, attrs);
    7. LayoutInflater.from(context).inflate(R.layout.common_top, this);
    8. btn_top_back = (Button) findViewById(R.id.btn_top_back);
    9. tv_top_title = (TextView) findViewById(R.id.tv_top_title);
    10. btn_top_right = (Button) findViewById(R.id.btn_top_right);
    11. }
    12. public void setvisible(int left,int middle,int right){
    13. btn_top_back.setVisibility(left);
    14. tv_top_title.setVisibility(middle);
    15. btn_top_right.setVisibility(right);
    16. }
    17. public void setTitle(String title){
    18. tv_top_title.setText(title);
    19. }
    20. public void setLeftOnclickListener(OnClickListener onClickListener){
    21. btn_top_back.setOnClickListener(onClickListener);
    22. }
    23. public void setRightOnclickListener(OnClickListener onClickListener){
    24. btn_top_right.setOnClickListener(onClickListener);
    25. }
    26.  
    27. public void setRightText(String text){
    28. btn_top_right.setText(text);
    29. }
    30. }

    在布局里面就可以通过包名+类名来重用title布局文件,在activity或者fragement里面初始化,通过TitleView的方法控制内容的显示和隐藏,以及内容的变化,控件事件的响应。

自定义控件之--组合控件(titlebar)的更多相关文章

  1. Android开发技巧——自定义控件之组合控件

    Android开发技巧--自定义控件之组合控件 我准备在接下来一段时间,写一系列有关Android自定义控件的博客,包括如何进行各种自定义,并分享一下我所知道的其中的技巧,注意点等. 还是那句老话,尽 ...

  2. Flutter学习笔记(38)--自定义控件之组合控件

    如需转载,请注明出处:Flutter学习笔记(38)--自定义控件之组合控件 在开始之前想先写点其他的,emm...就是今天在学习到自定义控件的时候,由于自定义控件这块一直是我的短板,无论是Andro ...

  3. android--------自定义控件 之 组合控件篇

    上篇介绍了自定义控件的自定义属性篇,地址:http://www.cnblogs.com/zhangqie/p/8969163.html 这篇博文主要来说说 自定义控件的组合控件来提高布局的复用 使用自 ...

  4. Android自定义控件之自定义组合控件

    前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...

  5. Android自定义控件之自定义组合控件(三)

    前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...

  6. Andriod 自定义控件之创建可以复用的组合控件

    前面已学习了一种自定义控件的实现,是Andriod 自定义控件之音频条,还没学习的同学可以学习下,学习了的同学也要去温习下,一定要自己完全的掌握了,再继续学习,贪多嚼不烂可不是好的学习方法,我们争取学 ...

  7. ASP.NET自定义控件组件开发 第四章 组合控件开发CompositeControl

    原文:ASP.NET自定义控件组件开发 第四章 组合控件开发CompositeControl 第四章 组合控件开发CompositeControl 大家好,今天我们来实现一个自定义的控件,之前我们已经 ...

  8. ASP.NET自定义控件组件开发 第四章 组合控件开发CompositeControl 后篇 --事件冒泡

    原文:ASP.NET自定义控件组件开发 第四章 组合控件开发CompositeControl 后篇 --事件冒泡 CompositeControl  后篇 --事件冒泡 系列文章链接: ASP.NET ...

  9. Android自定义控件View(三)组合控件

    不少人应该见过小米手机系统音量控制UI,一个圆形带动画效果的音量加减UI,效果很好看.它是怎么实现的呢?这篇博客来揭开它的神秘面纱.先上效果图 相信很多人都知道Android自定义控件的三种方式,An ...

随机推荐

  1. 升级到macSierra 10.12之后 在模拟器上面滑动视图很卡,

    解决方法 在终端里面输入sudo sysctl -w kern.timer.coalescing_enabled=0

  2. 学习node的REPL

    REPL: read eval print loop 支持 多行代码 编辑模式,即 识别{} () 为代码块. 在repl中快速查看 对象的属性 gl = global repl command .h ...

  3. mybatis_generator

    1.准备工作: jar包: mybatis-generator-core-1.3.2.jar,MyBatis Generator所需jar mysql-connector-java-5.1.28-bi ...

  4. python--函数式登录程序

    # Author:Tim Gu def login(username,password): """这个login函数用于用户登录的验证 :param username:用 ...

  5. 快速挂载和分离VHD文件的小脚本

      =======代码开始======== echo select vdisk file= %VHD文件路径% echo attach vdisk exit 分离VHD的脚本,只需要将上述代码中 at ...

  6. SQL优化 总结 精简

    索引: 考虑在 where 及 order by 涉及的列上建立索引 经常同时存取多列,且每列都含有重复值可考虑建立组合索引,且查询越频繁的字段放前面 按需使用聚集与非聚集索引,聚集不适合频繁更新.适 ...

  7. Hibernate5--课程笔记5

    关联关系映射: 关联关系,是使用最多的一种关系,非常重要.在内存中反映为实体关系,映射到DB中为主外键关系.实体间的关联,即对外键的维护.关联关系的发生,即对外键数据的改变. 外键:外面的主键,即,使 ...

  8. Hibernate5-课程笔记3

    详解Hibernate的API: (1)Configuration接口: org.hibernate.cfg.Configuration接口的作用是加载主配置文件及映射文件,以实现对Hibernate ...

  9. 【转】关于spring集合对象的补充

    <span style="font-size:18px;">关于spring集合对象的补充 spring2.0中对集合对象有了改进,新增了一个<util>标 ...

  10. excel中的数据导入oracle方法

    SQL_loader批量上传数据 1.    注释 在工作中,很多时候会遇到如下情况:需要将excel中的数据批量上传到ORACLE表中.如果是小数据量,如几十条至几百条,那么用plsql dev工具 ...