android自定义控件并添加属性的方法以及示例

安卓系统为我们提供了丰富的控件,但是在实际项目中我们仍然需要重新通过布局来实现一些效果,比如我们需要一个上面图标,下面文字的button,类似于下面这样的:

最直接的解决办法是通过将imageview和textview放在一个垂直排列的LinearLayout中,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
"http://schemas.android.com/apk/res/android"
     android:layout_width= "fill_parent"
     android:layout_height= "fill_parent"
     android:gravity= "center"
     android:orientation= "vertical" >
    
         android:id= "@+id/icon_part"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:layout_gravity= "center"
         />
    
         android:id= "@+id/text_part"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:layout_gravity= "center"
         android:textColor= "#000000" />

但是每一个button都需要这么长的代码,上面三个按钮的话就需要重复写三次,而且别人一看是个LinearLayout,不会将它button联系起来。

如果有一种办法能将上面那个布局组合成一个控件就好了。

的确是有办法的。主要有两方面的工作。

1.新建一个继承自LinearLayout的类(也可以是其他布局类,不过LinearLayout好像比较合适),然后通过inflater在这个类的构造函数中将上面的布局添加进去。

2.为了能在xml中也给这个自定义控件赋予属性来获得现实效果,比如字体大小、图标资源等,我们还需要在attrs文件中申明一些自定义属性。你可以查阅declare-styleable了解这是怎么回事。


我这里有一个已经实现了这种button效果的类FlexImageButton:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.jcodecraeer.client.widget;
import com.jcodecraeer.client.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class FlexImageButton extends LinearLayout {
     private ImageView imageView;
     private TextView textView;
     private CharSequence text;
     private Drawable drawable;
     private float textSize;
     public FlexImageButton(Context context) {
         super (context);
         // TODO Auto-generated constructor stub
     }
     public FlexImageButton(Context context, AttributeSet attrs) {
         super (context, attrs);
         // TODO Auto-generated constructor stub
         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FlexImageButton);
         text = a.getText(R.styleable.FlexImageButton_text);
         if (text== null ){
             text= "" ;
         }
         Drawable d = a.getDrawable(R.styleable.FlexImageButton_src);
         if (d != null ) {
             drawable=d;
         } else {
             throw new RuntimeException( "图像资源为空" );
         }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
         textSize = a.getDimension(R.styleable.FlexImageButton_textSize,12);
         String infService = Context.LAYOUT_INFLATER_SERVICE;
         LayoutInflater inflater = (LayoutInflater) context
                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         inflater.inflate(R.layout.flex_image_button_layout, this );
         imageView = (ImageView) findViewById(R.id.icon_part);
         imageView.setImageDrawable(drawable);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
         textView = (TextView) findViewById(R.id.text_part);
         textView.setTextSize((float) textSize);
         textView.setText(text);
         if (text.equals( "" )||text== null ){
             textView.setVisibility(View.GONE);
         }
         a.recycle();
     }
     public void setImageResource(int resId) {
         imageView.setImageResource(resId);
     }
     public void setTextViewText(String text) {
         textView.setText(text);
     }
}


在attrs.xml文件中我们声明一些自定义属性,这里我们希望我的FlexImageButton能拥有可以灵活设置的文字属性,字体大小属性、图标资源属性,因此我这样定义:

1
2
3
4
5
6
7
     "FlexImageButton" >
         "text" format= "reference" />
         "src" format= "reference" />
         "textSize"  format= "dimension" />
    


其中format="reference"表示这个属性的值类型是资源id,也就是说在使用FlexImageButton的时候我只可以用资源id来为这个属性赋值。属性值类型有那些,我在文章结尾的附录里面一一列出。



上面我们已经完成了一个自定义的控件,activity的布局文件中如下使用FlexImageButton:

1
2
3
4
5
6
7
8
9
10
11
12
13
     android:layout_height= "fill_parent"       
     android:layout_width= "50dip"
     cl:src= "@drawable/toolbar_collect"
     cl:text= "@string/collect"
     android:clickable= "true"
     android:focusable= "true"
     android:layout_marginLeft= "5dip"
     android:layout_marginRight= "5dip"
     android:contentDescription= "收藏"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
     android:background= "@drawable/back_selector"
/>

仔细的人会注意到所有这些属性中,有两种形式。其中凡是android:开头的都是系统属性,而

1
2
cl:src= "@drawable/toolbar_collect"
cl:text= "@string/collect"

为我自定义的属性,为什么是cl:开头?

你也可以不用cl开头,但是不管你用什么开头,如果你用到了自定义属性,你都必须在activity布局文件的最开始这样声明:

1
2
3
4
"1.0" encoding= "utf-8" ?>
     xmlns:android= "http://schemas.android.com/apk/res/android"
     xmlns:cl= "http://schemas.android.com/apk/res/com.jcodecraeer.client"

其中cl就是刚刚用到的,com.jcodecraeer.client为我的apk包名,注意是apk包名,而不是你自定义控件的在包中的路径。


附录:自定义属性的值类型:

1. reference:参考某一资源ID。

(1)属性定义:

1
2
3
"名称" >
     "reference" name= "background" />

(2)属性使用:

1
2
3
4
     android:layout_width= "42dip"
     android:layout_height= "42dip"
     android:background= "@drawable/图片ID" />

2. color:颜色值。

(1)属性定义:

1
2
3
"名称" >
     "color" name= "textColor" />

(2)属性使用:

1
2
3
4
     android:layout_width= "42dip"
     android:layout_height= "42dip"
     android:textColor= "#00FF00" />

3. boolean:布尔值。

(1)属性定义:

1
2
3
"名称" >
     "boolean" name= "focusable" />

(2)属性使用:

1
2
3
4
     android:layout_width= "42dip"
     android:layout_height= "42dip"
     android:focusable= "true" />

4. dimension:尺寸值。

(1)属性定义:

1
2
3
"名称" >
     "dimension" name= "layout_width" />

(2)属性使用:

1
2
3
     android:layout_width= "42dip"
     android:layout_height= "42dip" />

5. float:浮点值。

(1)属性定义:

1
2
3
4
"AlphaAnimation" >
     "float" name= "fromAlpha" />
     "float" name= "toAlpha" />

(2)属性使用:

1
2
3
     android:fromAlpha= "1.0"
     android:toAlpha= "0.7" />

6. integer:整型值。

(1)属性定义:

1
2
3
4
"AnimatedRotateDrawable" >
     "integer" name= "frameDuration" />
     "integer" name= "framesCount" />

(2)属性使用:

1
2
3
4
     android:frameDuration= "100"
     android:framesCount= "12"
      />

7. string:字符串。

(1)属性定义:

1
2
3
"MapView" >
     "string" name= "apiKey" />

(2)属性使用:

1
2
3
4
     android:layout_width= "fill_parent"
     android:layout_height= "fill_parent"
     android:apiKey= "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g" />

8. fraction:百分数。

(1)属性定义:

1
2
3
4
"RotateDrawable" >
     "fraction" name= "pivotX" />
     "fraction" name= "pivotY" />

(2)属性使用:

1
2
3
4
     android:pivotX= "200%"
     android:pivotY= "300%"
     />

9. enum:枚举值。

(1)属性定义:

1
2
3
4
5
6
"名称" >
     "orientation" >
         "horizontal" value= "0" />
         "vertical" value= "1" />
    

(2)属性使用:

1
2
3
     android:orientation= "vertical" >

10. flag:位或运算。

(1)属性定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
"名称" >
     "windowSoftInputMode" >
         "stateUnspecified" value= "0" />
         "stateUnchanged" value= "1" />
         "stateHidden" value= "2" />
         "stateAlwaysHidden" value= "3" />
         "stateVisible" value= "4" />
         "stateAlwaysVisible" value= "5" />
         "adjustUnspecified" value= "0x00" />
         "adjustResize" value= "0x10" />
         "adjustPan" value= "0x20" />
         "adjustNothing" value= "0x30" />
    

(2)属性使用:

1
2
3
     android:windowSoftInputMode= "stateUnspecified | stateUnchanged | stateHidden" >

注意:属性定义时可以指定多种类型值:

(1)属性定义:

1
2
3
"名称" >
     "reference|color" name= "background" />

(2)属性使用:

1
2
3
4
     android:layout_width= "42dip"
     android:layout_height= "42dip"
     android:background= "@drawable/图片ID|#00FF00" />

你可能感兴趣的:(Android,UI,Android开发)