自定义控件配置新增属性几步骤
1. 在values文件夹下声明属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="navigation">
<attr name="btn_leftText" format="string"/>
<attr name="btn_rightText" format="string"/>
<attr name="btn_drawable" format="reference"/> 定义引用资源
</declare-styleable>
</resources>
2. 在引用自定义控件需要用属性必须声明空间
xmlns:navigation="http://schemas.android.com/apk/res/com.example.mynavigation(自定义类的包名)"
3. 获取属性值(在自定义类的构造方法中通过 AttributeSet获取)
public MyNavigation(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.navigation);
Log.e("zhouyou", typedArray.getText(R.styleable.navigation_btn_leftText)+"");
Log.e("zhouyou", typedArray.getText(R.styleable.navigation_btn_rightText)+"");
typedArray.recycle();
}
1. 在values文件夹下声明属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="navigation">
<attr name="btn_leftText" format="string"/>
<attr name="btn_rightText" format="string"/>
<attr name="btn_drawable" format="reference"/> 定义引用资源
</declare-styleable>
</resources>
2. 在引用自定义控件需要用属性必须声明空间
xmlns:navigation="http://schemas.android.com/apk/res/com.example.mynavigation(自定义类的包名)"
3. 获取属性值(在自定义类的构造方法中通过 AttributeSet获取)
public MyNavigation(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.navigation);
Log.e("zhouyou", typedArray.getText(R.styleable.navigation_btn_leftText)+"");
Log.e("zhouyou", typedArray.getText(R.styleable.navigation_btn_rightText)+"");
typedArray.recycle();
}