怎么在Android中实现一个自定义控件

  介绍

今天就跟大家聊聊有关怎么在Android中实现一个自定义控件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

首先定义一个布局实现按钮内部布局:

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   & lt; LinearLayout  xmlns: android=癶ttp://schemas.android.com/apk/res/android"   android:才能layout_width=癴ill_parent"   android:才能layout_height=癴ill_parent"   android:才能取向=癶orizontal",在   & lt; ImageView才能   ,,,android: id=癅 + id/imageView1"   ,,,android: layout_width=皐rap_content"   ,,,android: layout_height=皐rap_content"   ,,,android: layout_gravity=癱enter_vertical"   ,,,android: paddingBottom=? dip"   ,,,android: paddingLeft=?0 dip"   ,,,android: paddingTop=? dip"   ,,,android: src=https://www.yisu.com/zixun/" @drawable/right_icon "/>      

接下来写一个类继承LinearLayout,导入刚刚的布局,并且设置需要的方法,从而使的能在代码中控制这个自定义控件内容的显示。

public  class  ImageBtn  extends  LinearLayout  {   private 才能;ImageView  imageView;   private 才能;TextView  textView;   public 才能ImageBtn (Context 上下文),{   ,,,超级(上下文);   ,,,//,TODO  Auto-generated  constructor 存根   ,,}   public 才能;ImageBtn (Context 上下文,AttributeSet  attrs), {   ,,,超级(上下文,attrs);   ,,,//,TODO  Auto-generated  constructor 存根   ,,,LayoutInflater 增压泵=(LayoutInflater), context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);   ,,,inflater.inflate (R.layout.imagebtn,,);   ,,,imageView=(imageView), findViewById (R.id.imageView1);   ,,,textView=(textView) findViewById (R.id.textView1);   ,,}   ,/* *   ,,*,设置图片资源   ,,*/public 才能;void  setImageResource (int 渣油),{   ,,,imageView.setImageResource(渣油);   ,,}   ,/* *   ,,*,设置显示的文字   ,,*/public 才能;void  setTextViewText (String 文本),{   ,,,textView.setText(文本);   ,,}   }

在需要使用这个自定义控件的布局中加入这控件,只需要在xml中加入即可。

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   & lt; LinearLayout  xmlns: android=癶ttp://schemas.android.com/apk/res/android"   android:才能layout_width=癴ill_parent"   android:才能layout_height=癴ill_parent"   android:才能取向=癶orizontal",在   & lt; cn.com.karl.view.ImageBtn才能   ,,,android: id=癅 + id/btn_right"   ,,,android: layout_height=皐rap_content"   ,,,android: layout_width=皐rap_content"   ,,,android:背景=癅drawable/btn"   ,,,/比;   & lt; cn.com.karl.view.ImageBtn才能   ,,,android: id=癅 + id/btn_error"   ,,,android: layout_marginLeft=? dp"   ,,,android: layout_height=皐rap_content"   ,,,android: layout_width=皐rap_content"   ,,,android:背景=癅drawable/btn"   ,,,/比;   & lt;/LinearLayout>

这里用到了背景图片在可拉的/btn。xml

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   & lt; selector  xmlns: android=癶ttp://schemas.android.com/apk/res/android",在   & lt;才能item  android: state_focused=皌rue", android: state_pressed=癴alse", android:可拉的=癅drawable/btn_normal"祝辞& lt;/item>   & lt;才能item  android: state_pressed=皌rue", android:可拉的=癅drawable/btn_white"祝辞& lt;/item>   & lt;才能item  android: state_checked=皌rue", android:可拉的=癅drawable/btn_white"祝辞& lt;/item>   & lt;才能item  android: state_focused=癴alse", android: state_pressed=癴alse", android:可拉的=癅drawable/btn_normal"祝辞& lt;/item>   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在Android中实现一个自定义控件