Android library project uses declare-styleable — how to compile?

前端 未结 6 785
春和景丽
春和景丽 2020-12-18 21:09

I have a custom preferences control that I have defined a few attributes for in values/attrs.xml. Just to focus the conversation, here is an example of attributes that coul

6条回答
  •  醉酒成梦
    2020-12-18 21:52

    I actually played a lot with styleable and libraries and have the following observations:

    Imagine that you have a project that has main project and included library:

    main
       |--- library
    
    • there is no problem with compiling an application with included library even when the library contains styleable. You can for example include one of my libraries ( http://code.google.com/p/tree-view-list-android/ ). You will find a number of styleable attributes there (like collapsible) and some XMLs using it inside the library.. All compiles and works well whether you compile library alone or as included library... So here it looks good.....
    • now, you might experience strange problems with compilation of the main project when you have resources with the same name in both - library and main project. I initially did not understood it, but now I know how it works. For example if you have in your library main.xml which has android:id="@+id/some_id" and then in your main project you have another main.xml (without some_id id), then the R.id.some_id will be generated when you compile the library, but will NOT be generated in the main project - this might lead to compilation errors of the main project because included library .java files most likely will use R.id.some_id (which will be missing). I initially misunderstood the problem and tried to get id dynamically, but it was not needed at the end - it was enough to make sure your layout xmls in library use some kind of privateish namespace.
    • and now the most interesting part - how to refer to the styleable parameters in layout.xmls of the project? As you noticed, the following won't work: xmlns:custom="http://schemas.android.com/apk/res/YOUR_LIBRARY_PACKAGE" Surprisingly what works instead is xmlns:custom="http://schemas.android.com/apk/res/YOUR_MAIN_APP_PACKAGE" (!) It indeed looks like an overlook from android team, but once you know it, it works flawlessly.

提交回复
热议问题