android 自定义控件(初篇)

android 自定义控件

在写UI当中很多时候会用到自定义的控件,其实自定义控件就像是定义一个类进行调用就OK了。有些相关的感念可以查看API

下面就用个简单的例子来说明自定义控件:

 1 public class UserInfoItem extends LinearLayout {
 2 
 3     private TextView aspect;
 4     private ImageView arrow;
 5 
 6     public UserInfoItem(Context context) {
 7         this(context, null);
 8     }
 9 
10     public UserInfoItem(Context context, AttributeSet attrs) {
11         super(context, attrs);
12     LayoutInflater.from(context).inflate(R.layout.user_dynamic_item, this,
13                 true);
14         aspect = (TextView) findViewById(R.id.aspect);
15         arrow = (ImageView) findViewById(R.id.arrow);
16     }
17 
18     /**
19      * @return the aspect
20      */
21     public TextView getAspect() {
22         return aspect;
23     }
24 
25     /**
26      * @param aspect
27      *            the aspect to set
28      */
29     public void setAspect(TextView aspect) {
30         this.aspect = aspect;
31     }
32 
33     /**
34      * @return the arrow
35      */
36     public ImageView getArrow() {
37         return arrow;
38     }
39 
40     /**
41      * @param arrow
42      *            the arrow to set
43      */
44     public void setArrow(ImageView arrow) {
45         this.arrow = arrow;
46     }
47 
48     public void setTextViewText(String str) {
49         aspect.setText(str);
50     }
51 
52     public String getTextViewText() {
53         return aspect.getText().toString();
54 
55     }
56 
57 }

对代码进行一下解读:

定义一个类并继承LinearLayout,LinearLayout本身就是一个View,里面有一句关键语句:LayoutInflater.from(context).inflate(R.layout.user_dynamic_item, this true)它将R.layout.user放入到this当中,而this就是指的这个类,所以

就是将R.layout.user填充到这个类当中了,可以直接findviewById来获取该布局xml中的View进行控制。

设置一些方法来服务其中的View。

R.layout.user_dynamic_item代码如下:

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="55dip"
 5     android:background="@drawable/bg_input_single"
 6     android:gravity="center"
 7     android:orientation="horizontal" >
 8 
 9     <TextView
10         android:id="@+id/aspect"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:layout_weight="3.0"
14         android:gravity="left"
15         android:text="aspect" 
16         android:layout_marginLeft="20dp"
17         />
18 
19     <ImageView
20         android:id="@+id/arrow"
21         android:layout_width="wrap_content"
22         android:layout_height="wrap_content"
23         android:layout_weight="1.0"
24         android:src="@drawable/mail_arrow" />
25 
26 </LinearLayout>

 

 

最后在想要调用该自定义控件的地方进行调用就行了,调用格式和调用类适相似的:

 

1 <com.xiaoguo.wasp.mobile.widget.UserInfoItem
2         android:id="@+id/myAttention"
3         android:layout_width="match_parent"
4         android:layout_height="65dip"
5         android:layout_marginLeft="8dp"
6         android:layout_marginRight="8dp"
7         android:layout_marginTop="15dp" />

 

 

运行后的重复使用自定义控件效果为:

 

转载于:https://www.cnblogs.com/liangstudyhome/p/3684616.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值