自定义属性

<?xml version="1.0" encoding="utf-8"?>   
<resources>   
    <declare-styleable name="Gallery">   
        <attr name="android:galleryItemBackground" />   
    </declare-styleable>   
</resources> 



<com.terry.attrs.EditTextExt1 android:id="@+id/ss3"
        android:layout_width
="wrap_content" android:layout_height="wrap_content"
        Text
="@string/app_name" >

</com.terry.attrs.EditTextExt1>


package com.terry.attrs;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class EditTextExt extends LinearLayout {

    
public EditTextExt(Context context) {
        
this(context, null);
        
// TODO Auto-generated constructor stub
    }

    
public EditTextExt(Context context, AttributeSet attrs) {
        super(context, attrs);
        
// TODO Auto-generated constructor stub
        int resouceId = -1;
        TypedArray typeArray 
= context.obtainStyledAttributes(attrs,
                R.styleable.EditTextExt);

        TextView tv 
= new TextView(context);
        EditText et 
= new EditText(context);
        
        
int N = typeArray.getIndexCount();
        
for (int i = 0; i < N; i++) {
            
int attr = typeArray.getIndex(i);
            
switch (attr) {
            
case R.styleable.EditTextExt_Oriental:
                resouceId 
= typeArray.getInt(R.styleable.EditTextExt_Oriental,
                        
0);
                
this.setOrientation(resouceId == 1 ? LinearLayout.HORIZONTAL
                        : LinearLayout.VERTICAL);
                
break;
            
case R.styleable.EditTextExt_Text:
                resouceId 
= typeArray.getResourceId(
                        R.styleable.EditTextExt_Text, 
0);
                tv.setText(resouceId 
> 0 ? typeArray.getResources().getText(
                        resouceId) : typeArray
                        .getString(R.styleable.EditTextExt_Text));
                
break;
            }
        }
        addView(tv);
        addView(et);
        typeArray.recycle();

    }

}


<?xml version="1.0" encoding="UTF-8"?>
<resources>
    
<declare-styleable name="EditTextExt">
        
<attr name="Text" format="reference|string"></attr>
        
<attr name="Oriental">
            
<enum name="Horizontal" value="1"></enum>
            
<enum name="Vertical" value="0"></enum>
        
</attr>
    
</declare-styleable>
</resources>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:terry
="http://schemas.android.com/apk/res/com.terry.attrs"
    android:orientation
="vertical" android:layout_width="fill_parent"
    android:layout_height
="fill_parent">
    
<TextView android:layout_width="fill_parent"
        android:layout_height
="wrap_content" android:text="@string/hello" />

    
<com.terry.attrs.EditTextExt android:id="@+id/ss"
        android:layout_width
="fill_parent" android:layout_height="wrap_content"
        terry:Text
="fdsafda" terry:Oriental="Vertical"></com.terry.attrs.EditTextExt>

    
<com.terry.attrs.EditTextExt1 android:id="@+id/ss3"
        android:layout_width
="wrap_content" android:layout_height="wrap_content"
        Text
="@string/app_name"  ></com.terry.attrs.EditTextExt1>
</LinearLayout>

上面提供的是android 基础组件的包名,和我们自己组件的包名。

posted on 2011-08-16 09:08  jsonjack  阅读(237)  评论(0)    收藏  举报

导航