我想在某些Android佈局中使用自定義屬性,但當我嘗試使用android:
以外的命名空間前綴時,出現錯誤(來自Eclipse)一個子元素。請注意,當我在文件的根/父元素中使用custom:
命名空間前綴時,它可以正常工作,而不是子元素。在佈局中的子元素中使用非android命名空間前綴
例如,這裏有一個簡單的佈局與自定義命名規定:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
custom:my_tag1="whatever"> <!-- compiles fine -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
custom:my_tag2="true"/> <!-- generates an error -->
</LinearLayout>
了Eclipse提供了(僅在秒嘗試使用custom:
前綴)的錯誤是:
爲標籤ImageView找到意外的命名空間前綴「custom」。
如果我讓我的根元素的ImageView
而不是LinearLayout
,前綴被接受。所以它似乎只是一個在子元素中使用名稱空間前綴的問題。
此外,如果我嘗試將另一個xmlns:custom="http://schemas.android.com/apk/res-auto"
屬性添加到ImageView,它也會抱怨。
如果有幫助,這裏是attrs.xml
文件我使用上述:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="my_tag1" format="string"/>
<attr name="my_tag2" format="boolean"/>
</resources>
我已經看到了一些網上的東西,導致我相信我所想要做的應該是可能的。例如,在接受的答案here中,Qberticus在子類中使用前綴「whatever」。同樣在帖子here。
我不明白。是使用一個非android的命名空間前綴只是不允許的子元素,或者我做錯了什麼?
找到解決方案? –
@Xylian不幸的是,沒有。抱歉。我最終通過使用'styles.xml'中的自定義屬性來解決它。 – Turix
看到它可能有所幫助http://stackoverflow.com/questions/4568632/custom-attributes-in-android – Mehdiway