`

命名空间

 
阅读更多

1.需要在AndroidManifest.xml添加自己的命名空间

xmlns:android="http://schemas.android.com/apk/res/android"  // Android的命名空间
    xmlns:hehe=http://schemas.android.com/apk/res/(包名)"   // 自定义的命名空间

hehe 为声明的名字

 

2.添加 attrs格式如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="RotateTextView">
        <attr name="rotate" format="integer"></attr>
    </declare-styleable>
</resources>

 

3.添加自己的控件

public class RotateTextView extends TextView{
	private static final String NAMESPACE = "http://schemas.android.com/apk/res/(包名)";
	private static String ATTRIBUTE = "rotate";
	private static final int DEFAULTVALUE = 0;
	private int degrees;
	public RotateTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		attrs.getAttributeResourceValue(NAMESPACE, ATTRIBUTE, DEFAULTVALUE);
	}

	public RotateTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		degrees = attrs.getAttributeIntValue(NAMESPACE, ATTRIBUTE, DEFAULTVALUE);
	}

	public RotateTextView(Context context) {
		super(context);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.rotate(degrees, getMeasuredWidth() / 2, getMeasuredHeight() / 2);
		super.onDraw(canvas);
	}
	
}

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics