最近在研究andorid自定义控件和分项目编写,以下链接给了我很大帮助,感谢这些乐于分享的人。不敢独享,分享一下:
在res\values建立ids.xml文件,格式和系统一致
2.Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用
7.Managing Projects from Eclipse with ADT 这个事官方的
9.Issue 9656 Library projects don't support custom XML attributes for custom classes.
10.Help with a custom View attributes inside a Android Library Project
8-10 提供解决Android-library自定义属性的杯具
8中的
注意,上面的lfchart应用的是主工程的包名,而不是控件的包名。
这句话很有用,以下两种分案,都是围绕着它。
下面英文比较简单,我就不翻译了啊
9中的
, Jul 27, 2011
I might have just found quite a simple work-arround: Steps: 1. define a String resource in the library project with your library package: <string name="NS">http://schemas.android.com/apk/res/com.company.yourLIBRARYproject</string> 2. define the same resource for your app project with your app package: <string name="NS">http://schemas.android.com/apk/res/com.company.yourAPPproject</string> 3. replace all custom namespace definitions in library and app project with xmlns:custom="@string/NS" (instead of xmlns:custom="http://schemas.android.com/apk/res/com.company.yourLIBRARYproject" and xmlns:custom="http://schemas.android.com/apk/res/com.company.yourAPPproject" ) Attributes can now be called the normal way custom:attribute ="value"可以,我试过。
10中的
After two hours of work with my coworker, we figure out how to make this work : Library : com.library mywidget.java with a class MyWidget
attrs.xml :
<declare-styleable name="MyWidget">
and put your attr
Instead of having your layout (main.xml for example) with your widget declared in it, juste create a my_widget.xml in the layout foler of your library. my_widget.xml
<?xml version="1.0" encoding="utf-8"?>
<com.library.MyWidget
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:widget="http://schemas.android.com/apk/res/com.library"
android:id="@+id/your_id"
android:layout_width="fill_parent" android:layout_height="fill_parent"
widget:your_attr>
</com.library.MyWidget>
for including it in your layout, juste put
<include layout="@layout/my_widget"></include>
App : com.myapp
You juste have to duplicate my_widget.xml here, and change the namespace :
<?xml version="1.0" encoding="utf-8"?>
<com.library.MyWidget
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:widget="http://schemas.android.com/apk/res/com.myapp" <---- VERY IMPORTANT
android:id="@+id/your_id"
android:layout_width="fill_parent" android:layout_height="fill_parent"
widget:your_attr>
</com.library.MyWidget>
And normally it will work !
可以,我试过。
Android-library真是个杯具啊,以上两种方法,都很dt。不知道有谁还有更好的解决方法,和大家分享一下。