添加库项目时将android R.java映射到资源布局
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了添加库项目时将android R.java映射到资源布局,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2539字,纯文字阅读大概需要4分钟。
内容图文
![添加库项目时将android R.java映射到资源布局](/upload/InfoBanner/zyjiaocheng/695/47b310e38a0545f98d3acf32c8ebd022.jpg)
我在
android R.java behavior when a library project is added
我观察到,将库项目添加到任何android项目时,都会创建两个R.java文件.
project.R.java
public static final class layout {
public static int capture=0x7f030000;
public static int main=0x7f030001;
}
lib.R.java
public static final class layout {
public static final int add=0x7f030000;
public static final int capture=0x7f030001;
public static final int main=0x7f030002;
}
并且被设置为库的项目具有自己的R.java,看起来像
public static final class layout {
public static int capture=0x7f030000;
public static int main=0x7f030001;
}
该示例库只有一个活动,我从我的应用程序开始,该活动设置了布局主程序.
现在,如果我们在应用程序和库项目中看到R.java中“ main”的id不同.我尝试从库中打印id的值,并给出0x7f030002,这是我的应用程序R.java文件中的值.
现在,当我将内容设置为smain时,我的应用程序没有主布局,并且在库中,它从库项目中设置了main.xml!
如果我将主布局添加到我的应用程序项目中,则lib将把这个主布局设置为其布局!
即main的id是从我的应用程序的R.java中获取的,此id与库中main的id不同,但布局是从库中正确选择的.
这是怎么回事
请帮忙
我的应用程式活动:
import com.idg.test.lib.TestLibActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class TestProjectActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i("starting","oncraete main id "+ R.layout.main);
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
startActivity(new Intent(this,TestLibActivity.class));
}
}
lib活动:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class TestLibActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("Library","Library main id" +R.layout.main );
setContentView(R.layout.main);
}
}
解决方法:
When you build an application that depends on a library project, the
SDK tools compile the library into a temporary JAR file and uses it in
the main project, then uses the result to generate the .apk. In cases
where a resource ID is defined in both the application and the
library, the tools ensure that the resource declared in the
application gets priority and that the resource in the library project
is not compiled into the application .apk. This gives your application
the flexibility to either use or redefine any resource behaviors or
values that are defined in any library.
希望它能回答您的问题
内容总结
以上是互联网集市为您收集整理的添加库项目时将android R.java映射到资源布局全部内容,希望文章能够帮你解决添加库项目时将android R.java映射到资源布局所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。