xmlns: android = "http://schemas.android.com/apk/res/android's role is
to Custom View when using an xml layout file need to add xmlns: prefix = http://schemas.android.com/apk/res/ your application package path .
Here is a simple example:
structure:
MyView.java
package kexc.myView;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyView extends TextView {
private String mString = "Welcome to Kesion's blog";
; public MyView (Context context, AttributeSet attrs) {
super (context, attrs);
TypedArray a = context.obtainStyledAttributes (attrs, < br /> R.styleable.MyView);
int textColor = a.getColor (R.styleable.MyView_textColor,
0XFFFFFFFF );
float textSize = a.getDimension (R.styleable.MyView_textSize, 36);
mString = a.getString (R.styleable.MyView_title);
setText (mString);
setTextSize (textSize) ;
setTextColor (textColor);
}
}
main.xml
xmlns: test = " http://schemas. android.com / apk / res / kexc.myView "
android: orientation =" vertical "
android : layout_width = "fill_parent"
android: layout_height = "fill_parent">
android: layout_height = "wrap_content" ;
android: text = "@ string / hello"
/> < br />
; android: layout_height = "fill_parent"
test: title = "wo shi text"
test: textSize = "20px" ;
test: textColor = "# fff"
/>
properties file value / attrs.xml
Run Results:
没有评论:
发表评论