Android UI编程之自定义控件初步(上)——ImageButton

概述:

    我想我们在使用一些App的时候,应该不会出现一些“裸控件”的吧。除非是一些系统中的软件,那是为了保持风格的一致性,做出的一些权衡。我这里并非是在指责Android原生的控件不好看,说实在的,我很喜欢Android的一些原生控件。只是有些时候为了风格的一致性,就不得不去花些功夫在美工上。这于美工这一点,我对某讯的产品的确欣赏。下面就让我们开始一点一点学习Android UI编程中的自定义控件。


分析:

自定义控件就点像堆积木,并给它涂上颜色,和功能说明。下面就让我们用一个例子来逐一地简单讨论一下。


示例分析:

效果图展示:

本示例将选取ImageButton来做一个简单地分析。下面先来看看运行效果图:

  


搭建基本雏形:

    对于雏形,首先要做的是积木的选择。我们选择的是一个ImageView和一个TextView,上下摆放,然后用一个约束将其绑定在一起。如下所示的代码片段:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:gravity="center_vertical"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <ImageView  
  9.         android:id="@+id/imageView1"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_gravity="center_horizontal"  
  13.         android:paddingBottom="5dip"  
  14.         android:paddingTop="5dip"  
  15.         android:src="@drawable/right_icon" />  
  16.   
  17.     <TextView  
  18.         android:id="@+id/textView1"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_gravity="center_horizontal"  
  22.         android:layout_marginTop="5dp"  
  23.         android:text="确定"  
  24.         android:textColor="#000000" />  
  25.   
  26. </LinearLayout>  
上面的代码只能让我们得到一个如上所示的中间方形图和下方的文本以及紧贴在这两者边缘的一个约束。


外观设计和功能添加:

外观设计:

现在我们就要进行颜色分配和功能说明了,它被实现在Java代码中了。如下关键代码:

[java]  view plain  copy
  1. /** 
  2.      * 设置图片资源 
  3.      */  
  4.     public void setImageResource(int resId) {  
  5.         imageView.setImageResource(resId);  
  6.     }  
  7.   
  8.     /** 
  9.      * 设置显示的文字 
  10.      */  
  11.     public void setTextViewText(String text) {  
  12.         textView.setText(text);  
  13.     }  


功能添加:

而对于此我们仅仅只是让这个“Button”更好看了一些罢了。于是我们现在还要做另外一件事。例如点击后要有一些指定的、绑定死的、使用这个控件所必然会进行的操作。其实和上面的加外套是一个性质。如下关键代码:

[java]  view plain  copy
  1. @Override  
  2.     public void setOnClickListener(OnClickListener l) {  
  3.         auxiliaryFunction();  
  4.           
  5.         super.setOnClickListener(l);  
  6.     }  
  7.       
  8.     protected void auxiliaryFunction() {  
  9.         Log.i("TAG""log message.");  
  10.     }  

上面添加的额外功能,我们可以在Log日志中查看是否有真的完成。


既然是自定义,当然这里的ImageButton原始构建不会是Button。如下真相代码:

[java]  view plain  copy
  1. public class ImageButton extends LinearLayout {  
  2.   
  3.     private ImageView imageView;  
  4.     private TextView textView;  
  5.   
  6.     public ImageButton(Context context) {  
  7.         super(context);  
  8.     }  
  9.   
  10.     public ImageButton(Context context, AttributeSet attrs) {  
  11.         super(context, attrs);  
  12.         LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  13.         inflater.inflate(R.layout.image_button, this);  
  14.         imageView = (ImageView) findViewById(R.id.imageView1);  
  15.         textView = (TextView) findViewById(R.id.textView1);  
  16.     }  
  17.   
  18.     /** 
  19.      * 设置图片资源 
  20.      */  
  21.     public void setImageResource(int resId) {  
  22.         imageView.setImageResource(resId);  
  23.     }  
  24.   
  25.     /** 
  26.      * 设置显示的文字 
  27.      */  
  28.     public void setTextViewText(String text) {  
  29.         textView.setText(text);  
  30.     }  
  31.   
  32.     @Override  
  33.     public void setOnClickListener(OnClickListener l) {  
  34.         auxiliaryFunction();  
  35.           
  36.         super.setOnClickListener(l);  
  37.     }  
  38.       
  39.     protected void auxiliaryFunction() {  
  40.         Log.i("TAG""log message.");  
  41.     }  
  42. }  

使用分析:

1.xml代码中的使用

这里只是有一点需要注意,我们要指明自定义控件的完整路径,如下:

[html]  view plain  copy
  1. <com.demo.customview.imagebutton.widgets.ImageButton  
  2.         android:id="@+id/btn_right"  
  3.         android:layout_width="150dp"  
  4.         android:layout_height="150dp"  
  5.         android:background="@drawable/custom_button" />  


2.动作效果配置

对于Button的动作也就是触摸、按下和抬起,对于这三个动作效果的配置需要在res包下的drawable文件夹中去创建(没有这个文件夹就新建一个)。如下代码:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:drawable="@drawable/button_unpress_background" android:state_focused="true" android:state_pressed="false"></item>  
  5.     <item android:drawable="@drawable/button_pressed_background" android:state_pressed="true"></item>  
  6.     <item android:drawable="@drawable/button_unpress_background" android:state_focused="false" android:state_pressed="false"></item>  
  7.   
  8. </selector>  


3.Java代码中的使用

在Java代码的使用与Button无异,如下:

[java]  view plain  copy
  1. public class MainActivity extends Activity {  
  2.   
  3.     private ImageButton mImageBtn1;  
  4.   
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.   
  10.         mImageBtn1 = (ImageButton) this.findViewById(R.id.btn_right);  
  11.         mImageBtn1.setTextViewText("确定");  
  12.         mImageBtn1.setImageResource(R.drawable.right_icon);  
  13.   
  14.         mImageBtn1.setOnClickListener(new View.OnClickListener() {  
  15.   
  16.             public void onClick(View v) {  
  17.                 Toast.makeText(getApplicationContext(), "点击确定"0).show();  
  18.             }  
  19.         });  
  20.     }  
  21. }  

源码分享:

    对于以上的分析,我想大家应该也已经完成了一个很漂亮的自定义控件。不过也有可能因为本人讲解得不够清楚,致使你没有实现想要的效果,在此也分享了本人的源代码。如下连接:

http://download.csdn.net/detail/u013761665/8408209

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值