Android编程实现自定义控件的方法示例
发布时间 - 2026-01-11 02:01:26 点击率:次本文实例讲述了Android编程实现自定义控件的方法。分享给大家供大家参考,具体如下:

很多时候Android常用的控件不能满足我们的需求,那么我们就需要自定义一个控件了。今天做了一个自定义控件的实例,来分享下。
首先定义一个layout实现按钮内部布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingLeft="40dip"
android:paddingTop="5dip"
android:src="@drawable/right_icon" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dip"
android:text="确定"
android:textColor="#000000" />
</LinearLayout>
接下来写一个类继承LinearLayout,导入刚刚的布局,并且设置需要的方法,从而使的能在代码中控制这个自定义控件内容的显示。
public class ImageBtn extends LinearLayout {
private ImageView imageView;
private TextView textView;
public ImageBtn(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public ImageBtn(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.imagebtn, this);
imageView=(ImageView) findViewById(R.id.imageView1);
textView=(TextView)findViewById(R.id.textView1);
}
/**
* 设置图片资源
*/
public void setImageResource(int resId) {
imageView.setImageResource(resId);
}
/**
* 设置显示的文字
*/
public void setTextViewText(String text) {
textView.setText(text);
}
}
在需要使用这个自定义控件的layout中加入这控件,只需要在xml中加入即可。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<cn.com.karl.view.ImageBtn
android:id="@+id/btn_right"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/btn"
/>
<cn.com.karl.view.ImageBtn
android:id="@+id/btn_error"
android:layout_marginLeft="5dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/btn"
/>
</LinearLayout>
这里用到了背景图片 在drawable/btn.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/btn_normal"></item> <item android:state_pressed="true" android:drawable="@drawable/btn_white"></item> <item android:state_checked="true" android:drawable="@drawable/btn_white"></item> <item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/btn_normal"></item> </selector>
最后在activity中设置该控件,和其他控件差不多:
public class IdentifyButtonActivity extends Activity {
private ImageBtn imageBtn1;
private ImageBtn imageBtn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.identifybutton);
imageBtn1=(ImageBtn) this.findViewById(R.id.btn_right);
imageBtn2=(ImageBtn) this.findViewById(R.id.btn_error);
imageBtn1.setTextViewText("确定");
imageBtn2.setTextViewText("取消");
imageBtn1.setImageResource(R.drawable.right_icon);
imageBtn2.setImageResource(R.drawable.error_icon);
imageBtn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "点击的正确按钮", 1).show();
}
});
imageBtn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "点击的错误按钮", 1).show();
}
});
}
}
最后看看我们自定义控件的效果吧!
点击后还有按下按钮的效果。
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》、《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android数据库操作技巧总结》及《Android资源操作技巧汇总》
希望本文所述对大家Android程序设计有所帮助。
# Android
# 自定义控件
# Android自定义控件实现底部菜单(上)
# Android自定义控件实现底部菜单(下)
# Android自定义控件实现随手指移动的小球
# Android自定义控件实现滑动开关效果
# Android 自定义底部上拉控件的实现方法
# 自定义
# 操作技巧
# 进阶
# 相关内容
# 只需
# 要在
# 感兴趣
# 能在
# 给大家
# 按下
# 更多关于
# 所述
# 使用这个
# 程序设计
# 来写
# 从而使
# 讲述了
# paddingTop
# paddingLeft
# src
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
网页设计与网站制作内容,怎样注册网站?
如何用美橙互联一键搭建多站合一网站?
香港服务器网站生成指南:免费资源整合与高速稳定配置方案
如何在万网自助建站中设置域名及备案?
JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)
高性价比服务器租赁——企业级配置与24小时运维服务
javascript中对象的定义、使用以及对象和原型链操作小结
Win11怎么关闭透明效果_Windows11辅助功能视觉效果设置
Laravel Eloquent模型如何创建_Laravel ORM基础之Model创建与使用教程
邀请函制作网站有哪些,有没有做年会邀请函的网站啊?在线制作,模板很多的那种?
laravel怎么使用数据库工厂(Factory)生成带有关联模型的数据_laravel Factory生成关联数据方法
惠州网站建设制作推广,惠州市华视达文化传媒有限公司怎么样?
laravel怎么配置Redis作为缓存驱动_laravel Redis缓存配置教程
极客网站有哪些,DoNews、36氪、爱范儿、虎嗅、雷锋网、极客公园这些互联网媒体网站有什么差异?
Laravel模型事件有哪些_Laravel Model Event生命周期详解
Laravel如何实现邮件验证激活账户_Laravel内置MustVerifyEmail接口配置【步骤】
瓜子二手车官方网站在线入口 瓜子二手车网页版官网通道入口
Laravel如何记录自定义日志?(Log频道配置)
Laravel怎么解决跨域问题_Laravel配置CORS跨域访问
Laravel数据库迁移怎么用_Laravel Migration管理数据库结构的正确姿势
Laravel Blade模板引擎语法_Laravel Blade布局继承用法
Laravel Eloquent访问器与修改器是什么_Laravel Accessors & Mutators数据处理技巧
Win11怎么关闭资讯和兴趣_Windows11任务栏设置隐藏小组件
详解Android中Activity的四大启动模式实验简述
Laravel怎么为数据库表字段添加索引以优化查询
C++用Dijkstra(迪杰斯特拉)算法求最短路径
如何在Windows 2008云服务器安全搭建网站?
Android 常见的图片加载框架详细介绍
用v-html解决Vue.js渲染中html标签不被解析的问题
rsync同步时出现rsync: failed to set times on “xxxx”: Operation not permitted
Gemini手机端怎么发图片_Gemini手机端发图方法【步骤】
Laravel如何升级到最新的版本_Laravel版本升级流程与兼容性处理
Python进程池调度策略_任务分发说明【指导】
Laravel中Service Container是做什么的_Laravel服务容器与依赖注入核心概念解析
Bootstrap整体框架之CSS12栅格系统
网站制作软件免费下载安装,有哪些免费下载的软件网站?
Laravel怎么实现验证码(Captcha)功能
iOS UIView常见属性方法小结
Microsoft Edge如何解决网页加载问题 Edge浏览器加载问题修复
如何在阿里云香港服务器快速搭建网站?
Laravel如何处理JSON字段的查询和更新_Laravel JSON列操作与查询技巧
家族网站制作贴纸教程视频,用豆子做粘帖画怎么制作?
软银砸40亿美元收购DigitalBridge 强化AI资料中心布局
电视网站制作tvbox接口,云海电视怎样自定义添加电视源?
Laravel怎么创建控制器Controller_Laravel路由绑定与控制器逻辑编写【指南】
成都品牌网站制作公司,成都营业执照年报网上怎么办理?
Laravel如何发送系统通知_Laravel Notifications实现多渠道消息通知
javascript如何操作浏览器历史记录_怎样实现无刷新导航
简单实现jsp分页
Windows10怎样连接蓝牙设备_Windows10蓝牙连接步骤【教程】

