博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义控件实现(转)
阅读量:7211 次
发布时间:2019-06-29

本文共 1471 字,大约阅读时间需要 4 分钟。

在Android开发中,常用的组件有时候无法满足我们的需求,因此我们需要自定义组件,这样可以提高组件的复用性,通过继承已有的组件,在此基础上对塔改进,下面演示简单一个一个按钮控件,塔包含2个ImageView和1个TextView。

1.组件模板

mybutton.xml

1 
7
15
23
31

2.继承父组件

MyButton.java

1 public class MyButton extends LinearLayout{ 2  3     private ImageView imgaeView;  //图标 4     private TextView  textView;   //文字 5     private ImageView arrow;      //箭头 6     public MyButton(Context context) { 7         super(context); 8          9     }10     public MyButton(Context context, AttributeSet attrs) {11         super(context, attrs);12         initView(context);13     }14     private void initView(Context context){15         LayoutInflater inflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);16         LinearLayout linearLayout=(LinearLayout) inflater.inflate(R.layout.mybutton, this);17         imgaeView=(ImageView)linearLayout.findViewById(R.id.icon);18         textView=(TextView) linearLayout.findViewById(R.id.text);19         arrow=(ImageView) linearLayout.findViewById(R.id.arrow);20     }21     //修改icon 22     public void setImageViewResource(int resId){23         imgaeView.setImageResource(resId);24     }25     //修改文本26     public void setTextViewText(String text){27         textView.setText(text);28     }29     30     31     32 }

3.Layout添加组件

4.定义背景

bg.xml

1 
2
3
4

bg1.xml

1 
2
3

bg2.xml

1 
2
3

转载地址:http://tzrum.baihongyu.com/

你可能感兴趣的文章
git分支建立删除,打tag
查看>>
mac地址规范及算法
查看>>
#10 Linux硬盘管理
查看>>
实战操作主机角色转移(二)
查看>>
MySQL数据库基本操作
查看>>
我写的oracleRAC状态监控脚本(在单位一直使用,比较稳定)
查看>>
**PHP中替换换行符
查看>>
更改数据库的兼容模式
查看>>
确保帐户安全 谈MySQL数据库安全解决方案
查看>>
MySQL修改root密码的多种方法
查看>>
Kali 系统 apt-get update更新失败解决方案
查看>>
用nginx TCP反向代理作mail邮件代理
查看>>
zabbix中文配置指南
查看>>
Windows系统下,使用Emacs+Putty操作远程机器
查看>>
ORA-00600:[kclchkinteg_2]及[kjmsm_epc]内部错误一例
查看>>
【Linux】基础每日一命之cut&mv&scp命令
查看>>
浪擎全融合灾备云获大数据安全领域最佳创新奖
查看>>
建议重写equals方法时也一并重写hashCode方法
查看>>
根据post请求调取app接口,map对象转字符串,json对象转字符串
查看>>
jenkins添加linux作为slave
查看>>