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


相关推荐: 简历没回改:利用AI润色让你的文字更专业  微信小程序 scroll-view组件实现列表页实例代码  javascript日期怎么处理_如何格式化输出  如何基于云服务器快速搭建个人网站?  七夕网站制作视频,七夕大促活动怎么报名?  Laravel怎么在Blade中安全地输出原始HTML内容  Laravel如何实现数据库事务?(DB Facade示例)  Laravel怎么多语言本地化设置_Laravel语言包翻译与Locale动态切换【手册】  如何在IIS服务器上快速部署高效网站?  如何在Windows 2008云服务器安全搭建网站?  电视网站制作tvbox接口,云海电视怎样自定义添加电视源?  如何快速登录WAP自助建站平台?  专业商城网站制作公司有哪些,pi商城官网是哪个?  如何用好域名打造高点击率的自主建站?  原生JS实现图片轮播切换效果  关于BootStrap modal 在IOS9中不能弹出的解决方法(IOS 9 bootstrap modal ios 9 noticework)  html如何与html链接_实现多个HTML页面互相链接【互相】  Laravel如何生成URL和重定向?(路由助手函数)  教你用AI将一段旋律扩展成一首完整的曲子  C语言设计一个闪闪的圣诞树  JavaScript如何实现路由_前端路由原理是什么  Laravel怎么实现验证码功能_Laravel集成验证码库防止机器人注册  如何在IIS中配置站点IP、端口及主机头?  Laravel如何实现多表关联模型定义_Laravel多对多关系及中间表数据存取【方法】  Laravel如何升级到最新版本?(升级指南和步骤)  Laravel如何保护应用免受CSRF攻击?(原理和示例)  详解免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)  Laravel如何监控和管理失败的队列任务_Laravel失败任务处理与监控  如何在IIS7中新建站点?详细步骤解析  成都品牌网站制作公司,成都营业执照年报网上怎么办理?  Laravel如何发送系统通知?(Notification渠道示例)  微信小程序制作网站有哪些,微信小程序需要做网站吗?  PythonWeb开发入门教程_Flask快速构建Web应用  网页制作模板网站推荐,网页设计海报之类的素材哪里好?  什么是javascript作用域_全局和局部作用域有什么区别?  Swift中循环语句中的转移语句 break 和 continue  如何快速生成橙子建站落地页链接?  laravel怎么为应用开启和关闭维护模式_laravel应用维护模式开启与关闭方法  车管所网站制作流程,交警当场开简易程序处罚决定书,在交警网站查询不到怎么办?  Laravel如何集成微信支付SDK_Laravel使用yansongda-pay实现扫码支付【实战】  iOS UIView常见属性方法小结  开心动漫网站制作软件下载,十分开心动画为何停播?  Laravel的.env文件有什么用_Laravel环境变量配置与管理详解  Win11怎么关闭透明效果_Windows11辅助功能视觉效果设置  Laravel如何生成和使用数据填充?(Seeder和Factory示例)  如何生成腾讯云建站专用兑换码?  如何破解联通资金短缺导致的基站建设难题?  Laravel项目如何进行性能优化_Laravel应用性能分析与优化技巧大全  如何做网站制作流程,*游戏网站怎么搭建?  无锡营销型网站制作公司,无锡网选车牌流程?