2013-01-16 138 views
0

說我有一個有效的自定義視圖,命名爲com.package.MyCustomView(MyCustomView擴展的LinearLayout或任何View)Android和自定義視圖和自定義XML屬性

我可以這樣

在活動佈局使用
<com.package.MyCustomView 
     android:id="@+id/myView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 

但我想有這樣的事情:

<com.package.MyCustomView 
     android:id="@+id/myView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     MyCustomView_attribute1="100" 
     MyCustomView_attribute2="fantastic"/> 

我想象中的有效的Android XML屬性類似情況android:layout_width將關聯的value (match_parent, wrap_content)設置爲父視圖對象的特定屬性(通過使用反射或其他..)。我也是知道的命名空間(該行:xmlns:android="http://schemas.android.com/apk/res/android")的存在..

在任何情況下,我想同一種東西 - 爲我定製了我的看法的自定義XML屬性...

這可能嗎?如果是 - 如何?請給一些細節

回答

0

是的,你可以這樣執行...它會工作

<com.example.application.Drawer 
     xmlns:my="http://schemas.android.com/apk/res/com.example.application" 
     android:id="@+id/drawer" 
     android:layout_width="301dp" 
     android:layout_height="wrap_content" 
     my:content="@+id/content" 
     my:handle="@+id/handle" > 

但有一件事是,你應該在SRC