当前位置:操作系统 > 安卓/Android >>

Android 之 自定义标签 和 自定义组件

1    自定义标签
这是我的模板项目目录
 
 
 
既然想像 android:text  那样使用自己的标签,那么首先得有标签。
在 res/values/ 下我新建了个 mm_tag.xml (切记不可出现大写,只能是 小写字母、数字、下划线)
第一步:    自定义 标签    
mm_tag.xml
<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <declare-styleable name="GridItem">  
        <attr name="bkground" format="reference|color"/>  
        <attr name="text1"    format="string"/>  
        <attr name="text2"    format="string"/>  
        <attr name="image"    format="reference|integer"/>  
    </declare-styleable>      
</resources>  
 
format 参考:
1. reference:参考某一资源ID
2. color:颜色值
3. boolean:布尔值
4. dimension:尺寸值
5. float:浮点值
6. integer:整型值
7. string:字符串
8. fraction:百分数
9. enum:枚举值
//属性定义:  
< declare -styleable name = "名称" >  
    <attr name = "orientation" >  
        <enum name = "horizontal" value = "0" />  
        <enum name = "vertical" value = "1" />  
    </attr>                        
</ declare -styleable>  
  
//属性使用:  
<LinearLayout  
    xmlns:android = "http://schemas.android.com/apk/res/android"  
    android:orientation = "vertical"  
    android:layout_width = "fill_parent"  
    android:layout_height = "fill_parent"  
>  
</LinearLayout>  
 
10. flag:位或运算
//属性定义:  
< declare -styleable name = "名称" >  
    <attr name = "windowSoftInputMode" >  
        <flag name = "stateUnspecified" value = "0" />  
        <flag name = "stateUnchanged" value = "1" />  
        <flag name = "stateHidden" value = "2" />  
    </attr>                  
</ declare -styleable>  
  
//属性使用:  
<activity  
    android: name = ".StyleAndThemeActivity"  
    android:label = "@string/app_name"  
    android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden" >  
    <intent-filter>  
        < action android: name = "android.intent.action.MAIN" />  
        <category android: name = "android.intent.category.LAUNCHER" />  
    </intent-filter>  
</activity>  
 
11.注意:属性定义时可以指定多种类型值。
//属性定义:  
< declare -styleable name = "名称" >  
    <attr name = "background" format = "reference|color" />  
</ declare -styleable>  
  
//属性使用:  
<ImageView  
    android:layout_width = "42dip"  
    android:layout_height = "42dip"  
    android: background = "@drawable/图片ID|#00FF00" />  
第二步:    在自定义组件中获得标签传回的数据
比如我们在布局中使用自定义组件 GridItem:
首先 声明好 标签的命名空间
xmlns:griditem = "http://schemas.android.com/apk/res/com.mm.template"  
//对比下 android 的命名空间:  
xmlns:android = "http://schemas.android.com/apk/res/android"  
发现只有 res/后面的不同,com.mm.template 是我的应用程序包名,通过上文中的 项目目录图片可以看出来,
griditem 是我随便想的一个命名空间的名字。
接下来就是使用自定义组件
< com.mm.template.GridItem  
     griditem:image = "@drawable/mm_1"  
     android:padding = "5dp"  
     android:layout_width = "wrap_content"  
     android:layout_height = "wrap_content"  
     android:layout_weight = "1"  
     griditem:bkground = "@color/orange"  
     griditem:text1 = "Android"       griditem:text2 = "手机开发" />  
其中 用到了我们的自定义标签:
griditem:image = "@drawable/mm_1"  
griditem:bkground = "@color/orange"  
griditem:text1 = "Android"  
griditem:text2 = "手机开发"  
怎么获取标签传回的数据呢呢?
在自定义组件 GridItem 的实现代码中使用如下方法即可
public GridItem(Context context, AttributeSet attrs) {  
    super(context, attrs);  
      
    TypedArray typedarray=context.obtainStyledAttributes(attrs, R.styleable.GridItem);  
      
    bk_color =typedarray.getResourceId(R.styleable.GridItem_bkground, R.color.burlywood);  
    text1 =typedarray.getString(R.styleable.GridItem_text1);  
    text2 =typedarray.getString(R.styleable.GridItem_text2);  
    image=typedarray.getDrawable(R.styleable.GridItem_image);  
      
    typedarray.recycle();  
  
    view=LayoutInflater.from(context).inflate(R.layout.mm_grid_item, this,true);  
      
    layout=(LinearLayout)view.findViewById(R.id.item_layout);  
    textview1=(TextView)view.findViewById(R.id.text1);  
    textview2=(TextView)view.findViewById(R.id.text2);  
    imageview=(ImageView)view.findViewById(R.id.imageview);  
      
    layout.setBackgroundResource(bk_color); //设置背景色  
    textview1.setText(text1);               //设置第一行文字  
&
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,