是否可以在Android Studio中分离对资源的访问 [英] Is it possible to separate resources access in Android Studio

查看:290
本文介绍了是否可以在Android Studio中分离对资源的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从中学到了质疑在Android Studio项目中全局共享资源.

I have learned from this question that resources are globally shared in an Android Studio project.

我有一个包含3个活动的项目(并且还会有更多活动进行),并且我已经很混乱,无法访问任何给定活动中的所有资源.

I have a project with 3 activities (and more are coming), and I already find pretty messy to have access to every resources in any given activity.

是否有一种方法允许活动仅从关联的布局访问ID?

Is there a way to allow an activity to access ids only from the associated layout ?

例如:我有MainActivity.java,activity_main.xml,Main2Activity.java,activity_main2.xml.

eg : I have MainActivity.java, activity_main.xml, Main2Activity.java, activity_main2.xml .

我想将MainActivity.java可以访问的资源(主要是ID)限制为activity_main.xml(这样就不能从activity_main2.xml访问资源)

I would like to restrict the resources (mainly ids) accessible by MainActivity.java to activity_main.xml (so no access to resources from activity_main2.xml)

推荐答案

没有办法.您可以具有基于Build变体的源集和资源集,但不能将单个活动绑定到资源文件.但是,为什么需要它.有了良好的命名约定,您很容易将两者相互关联,并且仅当您将活动布局用作setContentView方法的参数时,布局才会附加到活动中.

No there is no way to do this. You can have source and resource sets based on Build variants but cannot tie up individual activities to resource files. But why do you need it. With a good naming convention, it would be pretty easy for you to co-relate the two and a layout gets attached to an activity only you use it as an argument to the setContentViewmethod.

这篇关于是否可以在Android Studio中分离对资源的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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