Android: How to inject a element into another element in XML?

后端 未结 4 624

I would like to know whether there is a way to insert/inject a element defined in an XML file into another element, d

4条回答
  •  温柔的废话
    2020-12-14 05:35

    Gradle plugin 0.10 brings what they call manifestPlaceholders which basically does what you need but this feature currently only work in the AndroidManifest. There is although an issue opened targeting the next Android build version 1.4 (1.3 is currently in beta4 so should be near RC1 and could see a 1.4 beta1 soon hopefully).

    This issue should expand the placeholders in xml configurations files (I just pray this will include strings file and not only basic xml configuration).

    From: http://tools.android.com/tech-docs/new-build-system

    For custom placeholders replacements, use the following DSL to configure the placeholders values :

    android {
            defaultConfig {
                manifestPlaceholders = [ activityLabel:"defaultName"]
            }
            productFlavors {
                free {
                }
                pro {
                    manifestPlaceholders = [ activityLabel:"proName" ]
                }
            }
    

    will substitute the placeholder in the following declaration :

    android:label="${activityLabel}" >

    Can't wait to try it out. This way you could put a placeholder in multiple occurence in the strings file and define the value only in one place instead of modifying all java files to add an argument with %1$s

    For now the only clean solution is although the entity trick but this will not work if you want to override the value in flavors since the entity must be defined in the same xml file.

提交回复
热议问题