1、声明:xmlns:thinking="http://schemas.android.com/thinking"
2、使用:thinking:test_str="yuyong"
3、获取:String str = attrs.getAttributeValue(
"http://schemas.android.com/thinking", "test_str");
示例:
activity_main.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:thinking="http://schemas.android.com/thinking"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.thinking.skintest.MyText
android:id="@+id/txt_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
thinking:test_str="yuyong" />
</RelativeLayout>
MyText.java文件
package com.thinking.skintest;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyText extends TextView {
// 这两个函数是自定义View必须实现的
public MyText(Context context) {
super(context);
}
public MyText(Context context, AttributeSet attrs) {
super(context, attrs);
String str = attrs.getAttributeValue(
"http://schemas.android.com/thinking", "test_str");
this.setText(str + "_" + this.getText());
}
}
MainActivity.java文件
package com.thinking.skintest;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
运行结果: