android自定义控件属性问题
在values目录下创建了attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="PagerHeader">
<attr name="activeTextColor" format="color" />
<attr name="inactiveTextColor" format="color" />
<attr name="backgroundColor" format="color" />
</declare-styleable>
</resources>
写了PagerHeader class继承ViewGroup。
//得到xml中属性布局
public PagerHeader(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.PagerHeader, 0, 0);
activeTextColor = new ColorSet(a.getColor(
R.styleable.PagerHeader_activeTextColor, Color.BLACK));
inactiveTextColor = new ColorSet(a.getColor(
R.styleable.PagerHeader_inactiveTextColor, Color.DKGRAY));
if (a.hasValue(R.styleable.PagerHeader_backgroundColor)) {
int backgroundColor = a.getColor(
R.styleable.PagerHeader_backgroundColor, 0);
setBackgroundColor(backgroundColor);
fadingEdgeColorHint.setColor(backgroundColor);
} else if (a.hasValue(R.styleable.PagerHeader_fadingEdgeColorHint)) {
fadingEdgeColorHint.setColor(a.getColor(
R.styleable.PagerHeader_fadingEdgeColorHint, 0));
} else {
Log.w(TAG,"Either backgroundColor or fadingEdgeColorHint must be specified to "
fadingEdgeColorHint.setColor(0x00000000);
}
}
main_activity.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ihtvpn="http://schemas.android.com/apk/res/com.ht.ui.widget"
android:id="@+id/main_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.ht.ui.widget.PagerHeader
android:id="@+id/pager_header"
android:layout_width="fill_parent"
android:layout_height="@dimen/pager_header_height"
ihtvpn:backgroundColor="@color/pager_header_background_color"
ihtvpn:activeTextColor="@color/pager_header_active_text_color"
ihtvpn:inactiveTextColor="@color/pager_header_inactive_text_color">
</com.ht.ui.widget.PagerHeader>
</LinearLayout>
在这个xml中报错
Multiple annotations found at this line:
- error: No resource identifier found for attribute 'activeTextColor' in package
'com.ht.ui.widget'
- error: No resource identifier found for attribute 'inactiveTextColor' in package
'com.ht.ui.widget'
- error: No resource identifier found for attribute 'backgroundColor' in package
'com.ht.ui.widget'
请问各位大虾是什么情况。好几天了。坐等解答。
[解决办法]
xmlns:ihtvpn="http://schemas.android.com/apk/res/com.ht.ui.widget"
改成
xmlns:ihtvpn="http://schemas.android.com/com.ht.ui.widget"
试试?
android自定义控件属性有关问题
android自定义控件属性问题在values目录下创建了attrs.xml?xml version1.0 encodingutf-8?resour
