【发布时间】:2016-02-22 06:55:38
【问题描述】:
我正在用 C# 为 Xamarin/Android 创建一个自定义控件
它需要在 xml 布局中设置对他们的 2 个孩子的引用,所以我尝试了这个。
<declare-styleable name="CustomControl">
<attr name="firstViewReference" format="reference" />
<attr name="SecondViewReference" format="reference" />
</declare-styleable>
然后在我这样做的代码中检索值:
TypedArray a = this.Context.ObtainStyledAttributes(this.xmlAttrs, Resource.Styleable.CustomControl);
int aid =a.GetResourceId(Resource.Styleable.CustomControl_firstViewReference,0);
但对 getResourceId 的调用总是返回错误值(此处为 0)。
我的 xml:
<CustomControl xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:firstViewReference="@+id/firstView">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@id/firstView"/>
</CustomControl>
有什么问题吗?为什么我没有将我的视图设置为 firstViewReference 的 id?
提前致谢。
【问题讨论】:
-
什么是
this.xmlAttrs?发布更多代码,因为现在不清楚您在哪里使用ObtainStyledAttributes