android 自定义控件实例 (Linearlayout 组合 TextView 和
ImageView)
2013-12-18 11:25:22
转载自:
/lib/view/open1328836804515.
html
很多时候 android 常用的控件不能满足我们的需求,那么我
们就需要自定义一个控件了。今天做了一个自定义控件的实
例,来分享下。
首先定义一个 layout 实现按钮内部布局:
1 <?xml version="1.0" encoding="utf-8"?>
2
xmlns:android="/apk/res/andro
id"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="horizontal" >
6
7
8 android:id="@+id/imageView1"
9 android:layout_width="wrap_content"
10 android:layout_height="wrap_content"
11 android:layout_gravity="center_vertical"
12 android:paddingBottom="5dip"
13 android:paddingLeft="40dip"
14 android:paddingTop="5dip"
15 android:src="@drawable/right_icon" />
16
17
18 android:id="@+id/textView1"
19 android:layout_width="wrap_content"
20 android:layout_height="wrap_content"
21 android:layout_gravity="center_vertical"
22 android:layout_marginLeft="8dip"
23 android:text=" 确定 "
24 android:textColor="#000000" />
25
26
接下来写一个类继承 LinearLayout ,导入刚刚的布局,并且
设置需要的方法,从而使的能在代码中控制这个自定义控件
内容的显示。
1 public class ImageBtn extends LinearLayout {
2
3 private ImageView imageView;
4 private TextView textView;
5
6 public ImageBtn(Context context) {
7 super(context);
8 // TODO Auto-generated constructor stub
9 }
10 public ImageBtn(Context context, AttributeSet
attrs) {
11 super(context, attrs);
12 // TODO Auto-gen