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