开发者

Building my custom compound view for Android. How to expose properties?

开发者 https://www.devze.com 2023-03-16 20:14 出处:网络
Doing something similar to 开发者_JAVA百科this: http://developer.android.com/guide/topics/ui/custom-components.html

Doing something similar to 开发者_JAVA百科this: http://developer.android.com/guide/topics/ui/custom-components.html

Now I want to do something like this:

<com.me.activities.MyCompoundControl android:id="@+id/someName" android:layout_height="wrap_content" android:layout_width="wrap_content" customproperty="12345"/>

So, my question is how do I declare/code

customproperty

So I can reference it in XML when insert my custom view?


See http://www.infidian.com/2008/05/02/android-tutorial-42-passing-custom-variables-via-xml-resource-files/ for how to use the /res/values/attrs.xml file to specify the custom attributes you want to attach to your custom view and then access in the component initialization.


You need to define custom attributes in res/values/attrs.xml and read their values in the attribute-aware constructor of your view. Then you refer to these attributes using your custom namespace.

You can see an example I have done in one f my libraries:

Attributes are defined here: http://code.google.com/p/android-flip3d/source/browse/res/values/attrs.xml

And parsed here: http://code.google.com/p/android-flip3d/source/browse/src/pl/polidea/androidflip3d/Flip3DView.java#117

And referred here: http://code.google.com/p/android-flip3d/source/browse/res/layout/main.xml

There is no good documentation on available attribute types though and you need to browse android source code (look for attrs.xml there)

0

精彩评论

暂无评论...
验证码 换一张
取 消