自定义控件实现(转)
在Android开发中,常用的组件有时候无法满足我们的需求,因此我们需要自定义组件,这样可以提高组件的复用性,通过继承已有的组件,在此基础上对塔改进,下面演示简单一个一个按钮控件,塔包含2个ImageView和1个TextView。
1.组件模板
mybutton.xml
1 <LinearLayout2 xmlns:android="http://schemas.android.com/apk/res/android"3 android:layout_width="fill_parent" 4 android:layout_height="wrap_content"5 android:padding="10dip"6 android:orientation="horizontal">7 <ImageView 8 android:id="@+id/icon"9 android:layout_width="0dip" 10 android:layout_height="wrap_content" 11 android:layout_weight="1" 12 android:src="@drawable/icon" 13 android:layout_gravity="left|center_vertical" 14 /> 15 <TextView 16 android:id="@+id/text" 17 android:layout_width="0dip" 18 android:layout_height="wrap_content" 19 android:layout_weight="3" 20 android:layout_gravity="left|center_vertical" 21 android:textSize="18sp" 22 android:text="按钮文本"/> 23 <ImageView 24 android:id="@+id/arrow" 25 android:layout_width="0dip" 26 android:layout_height="wrap_content" 27 android:layout_weight="1" 28 android:layout_gravity="center_vertical" 29 android:src="@drawable/arrow" 30 /> 31 </LinearLayout>
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添加组件
<com.forsta.weight.MyButton android:id="@+id/bt1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:background="@drawable/bg"/>
4.定义背景
bg.xml
1 <selector xmlns:android="http://schemas.android.com/apk/res/android"> 2 <item android:state_pressed="true" android:drawable="@drawable/bg2"></item> 3 <item android:state_pressed="false" android:drawable="@drawable/bg1"></item> 4 </selector>
bg1.xml
1 <shape xmlns:android="http://schemas.android.com/apk/res/android"> 2 <gradient android:startColor="#FFFFFF" android:endColor="#8DEEEE"/> 3 </shape>
bg2.xml
1 <shape xmlns:android="http://schemas.android.com/apk/res/android"> 2 <gradient android:startColor="#EEE685" android:endColor="#DDA0DD"></gradient> 3 </shape>
自定义控件实现(转)相关推荐
- 开发Eclipse自定义控件
摘自:http://www.ibm.com/developerworks/cn/opensource/os-eclipcntl/ 我们在开发自定义控件时主要考虑以下问题: 1. 自定义控件的绘制:通常 ...
- qt获取当前系统音量值_Qt编写自定义控件50-迷你仪表盘
一.前言 这个控件取名叫迷你仪表盘,是以为该控件可以缩小到很小很小的区域显示,非常适合小面积区域展示仪表数据使用,还可以手动触摸调节进度,是我个人觉得最漂亮小巧的一个控件.初次看到类似的控件是在一个音 ...
- 【iOS】自定义控件入门:可拖动的环形进度
有时候UIKit的标准控件并不能满足我们的需求,因此我们可以通过自定义控件得到满足我们需求的控件,例如这篇文章将教你如何自定义一个圆形的进度条,并且用户可以通过拖动进度条上的手柄来改变进度值.主要参考 ...
- Android自定义控件系列之基础篇
一.概述 在android开发中很多UI控件往往需要进行定制以满足应用的需要或达到更加的效果,接下来就通过一个系列来介绍自定义控件,这里更多是通过一些案例逐步去学习,本系列有一些典型的应用,掌握好了大 ...
- C#自定义控件四简易时钟
C#自定义控件四简易时钟 效果图: 简易时钟,顾名思义,简单容易,简单到什么程度呢?界面只有数字和指针,甚至连与当前时间都不能匹配!呵呵!就这么简单,学习嘛,从简单开始. 毫无疑问,这里肯定要用到Ti ...
- 对做C#自定义控件的一点心得
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装这个COM组件,中间遇到很多曲折,研究了一个星期,终于完成了 下面总结一下我做DSOFramer这个自定义控件的注意地方: 1.在创建 ...
- ASP.NET自定义控件组件开发 第四章 组合控件开发CompositeControl
第四章 组合控件开发CompositeControl 大家好,今天我们来实现一个自定义的控件,之前我们已经知道了,要开发自定义的控件一般继承三个基 类:Control,WebControl,还有一个就 ...
- Android自定义控件NumberCircleProgressBar(圆形进度条)的实现
Android自定义控件NumberCircleProgressBar(圆形进度条)的实现
- 创建ASP.NET WEB自定义控件——例程2
本文通过一段完整的代码向读者介绍复合自定义控件的制作,包括:自定义属性.事件处理.控件间数据传递等方面的技术. 作者在http://damao.0538.org有一些控件和代码,并在更新中,有兴趣的读 ...
- android自定义view获取控件,android 自定义控件View在Activity中使用findByViewId得到结果为null...
转载:http://blog.csdn.net/xiabing082/article/details/48781489 1. 大家常常自定义view,,然后在xml 中添加该view 组件..如果在 ...
最新文章
- 服务化部署框架Paddle Serving
- Win7 wifi热点设置( 转载)
- spring定时任务重复执行2次问题的解决
- 查看网卡[网络接口]
- Linux版本Membase无法写入default bucket的问题分析
- [JavaWeb-XML]XML组成部分
- Oracle性能调优之--Buffer cache 的调整与优化
- linux mysql 5.7密码忘记_Mysql5.7.14 linux版密码忘记的找回方法
- 2019-1-7Xiaomi Mi5 刷全球版MIUI教程
- Microsoft Excel 不能访问文件
- 从零开始搭二维激光SLAM --- 写作计划
- java架构《并发线程中级篇》
- spring事件监听器系列一:应用
- 前端可视化的四种方式
- SpringBoot学习心得分享
- VUE之多元素组件过渡+动画封装
- 如何把img格式转换成vmdk格式
- 为何要使用加密邮箱?
- a轮b轮c轮天使轮区别是什么?
- openresty ngx_lua重定向
热门文章
- node 常用的一些终端的命令的快捷键
- #一周五# VS2015 CTP6, TFS2015 CTP1更新,老衣的开发工具汇总,2015 MVP 社区巡讲...
- HTML5中微数据在搜索引擎中的使用举例
- 求叶节点带权路径长度之和
- 诗与远方:无题(八十)- 吸烟而作
- C++之使用IO库输入输出
- java设计一百亿的计算器_请设计一个一百亿的计算器
- mysql html 转义_HTML/Mysql/XML 转义字符,备查
- linux let s证书续期,BT(宝塔面板)Let’s Encrypt证书续签方法
- apache php 重写url无效,apache用rewrite重写url时出现问题