尝试将自定义属性从父布局传递到子布局.
从ObtainStyledAttributes()返回的TypedArray似乎没有我创建的自定义属性的相应自定义值,尽管我可以将它们的ID映射到Resource.designer中的值.
Attr.xml:
<resources> <declare-styleable name="HeaderView"> <attr name="bgcolor" format="color" /> <attr name="testing" format="string" /> </declare-styleable>
Main.xaml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <views.HeaderView android:id="@+id/hdrWatchList" android:layout_width="fill_parent" android:layout_height="20.0dp" custom:bgcolor="@color/blue" custom:testing="testing text buddy" />
查看课程:
public HeaderView (Context context,IAttributeSet attrs) : base (context,attrs) { int[] styleAttrs = Resource.Styleable.HeaderView; TypedArray a = context.ObtainStyledAttributes(attrs,styleAttrs); string sid = a.GetString(Resource.Styleable.HeaderView_testing); int id = a.GetColor(Resource.Styleable.HeaderView_bgcolor,555); Log.Info( "testing","resource sid : " + sid); // RETURNS '' Log.Info( "testing","resource id : " + id); // RETURNS DEF 555
解决方法
我认为问题在于您如何指定xmlns:自定义命名空间.您需要在已有的字符串末尾添加应用程序命名空间,以便:
xmlns:custom="http://schemas.android.com/apk/res/my.awesome.namespace"
您还需要为Android项目定义AndroidManifest.xml,您已在其中定义了相同的命名空间.
线条:
int[] styleAttrs = Resource.Styleable.HeaderView; TypedArray a = context.ObtainStyledAttributes(attrs,styleAttrs);
看起来有点奇怪,我会写:
var a = context.ObtainStyledAttributes(attrs,Resource.Styleable.HeaderView);
特别是如果你以后不使用styleAttrs.
编辑:自Android SDK rev 17以来,可以使用:
xmlns:custom="http://schemas.android.com/apk/res-auto"
而不是必须编写整个命名空间.
原文地址:https://www.jb51.cc/android/318034.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。