通过AttributeSet访问小组件属性 - Android

时间:2011-04-26 02:30:50

标签: java android view namespaces

这应该是相当简单的任务。我有以下观点:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:panel="org.miscwidgets"

<org.miscwidgets.widget.Panel
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="right"
    panel:handle="@+id/panelHandle"
    panel:content="@+id/panelContent"
    panel:position="right">

我试图访问视图构造函数中的position属性,但它始终返回null。

        String posVal = attrs.getAttributeValue("schemas.android.com/apk/gen/org.miscwidgets", "position");

我怎样才能获得价值?我的命名空间错了吗?

我见过对Styleables的引用,但是我找不到任何不被弃用的内容是不成功的。

1 个答案:

答案 0 :(得分:1)

我认为你应该写你的xml

xmlns:panel="http://schemas.android.com/apk/gen/org.miscwidgets"

而不是

xmlns:panel="org.miscwidgets"

并通过

获取
attrs.getAttributeValue("http://schemas.android.com/apk/gen/org.miscwidgets", "position");