在xml布局中引用build.gradle versionName属性 [英] Reference build.gradle versionName attribute in xml layout

查看:191
本文介绍了在xml布局中引用build.gradle versionName属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的gradle文件中有一个引用versionName属性的布局文件:

I'd like to have a layout file which references the versionName attribute in my gradle file:

...
defaultConfig {
    applicationId "se.test.myapp"
    minSdkVersion 14
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}
....

类似

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/versionName"
    />

是否有一种巧妙的方法,而不必在我的代码中设置布局?

Is there a neat way to to this, without having to set up the layout in my code?

推荐答案

根据 http://tools.android.com/tech-docs/new-build-system 可以直接从gradle创建资源,因此放置

According to http://tools.android.com/tech-docs/new-build-system you can create resources directly from gradle, so putting

android {
...
    defaultConfig {
        applicationId "se.test.myapp"
        minSdkVersion 14
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    ...
    applicationVariants.all { variant ->    
        variant.resValue "string", "versionName", variant.versionName
    }
    ...
}

在您的build.gradle中可以解决问题

in your build.gradle will do the trick

它在编译过程中在generated/res文件夹中创建资源文件generated.xml,该文件与您在values文件夹中提供的资源一起提供.因此,您可以使用android:text="@string/versionName"引用此值.不幸的是,有时IDE无法解析此引用,因此它看起来像是布局资源中的错误(尽管它是有效的语句,将在运行时解决).

It creates resource file generated.xml during compilation in generated/res folder which is included alongside with resources provided by you in values folder. So you can use android:text="@string/versionName" to reference this value. Unfortunately, sometimes IDE can't resolve this reference, so it'll look like an error in your layout resource (while it's a valid statement and will be resolved at runtime).

您可以通过单击"@ string/versionName"内部,然后按Alt + Enter来抑制错误,在菜单中选择创建字符串值资源'versionName",然后选择禁止标记".

You can suppress the error by clicking inside the "@string/versionName", then Alt+Enter, in the menu select "Create string value resource 'versionName", then "Suppress for tag".

这篇关于在xml布局中引用build.gradle versionName属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆