是否可以从 asset 目录中的 xml 文件创建视图?

发布于 2024-10-28 03:27:57 字数 271 浏览 1 评论 0原文

我有一个应用程序,将所有布局放在 res/layout 文件夹中变得非常大并且难以管理。我想要所有布局的文件夹。我读到布局文件夹内不能有子目录,但资产文件夹中可能有子目录。所以我的问题是,如何从资产目录中的文件访问文件并将其设置为我的视图?类似下面的内容

int assetId = getAssets().open("main.xml");
setContentView(assetId)

上面的代码可以工作吗?我如何为我的视图设置该 xml 文件? 谢谢。

I have an application and putting all of the layouts inside of the res/layout folder is getting pretty large and hard to manage. I would like to have folders for all the layouts. I have read that there cannot be subdirectories inside the layout folder but that there could be in the assets folder. So my question is, how do I access a file and set it as my view from a file in the assets directory? Something like the following

int assetId = getAssets().open("main.xml");
setContentView(assetId)

Would the above code work? How would I set that xml file for my view?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡淡绿茶香 2024-11-04 03:27:57

您可以通过使用自定义脚本并在构建执行之前运行它来实现此目的。 Android 似乎会忽略布局子目录中的任何内容,因此您可以安全地将文件放入其中。下面的 ruby​​ 脚本(为 Linux 编写,但可以轻松转换到其他平台)将删除 res/layout/ 中不属于目录的所有内容,并将子目录中的每个文件复制到 res/layout/ 中


#!/usr/bin/ruby

require "fileutils"

def collect_files(directory)
    FileUtils.cd(directory)
    FileUtils.rm(Dir.entries(directory).reject{|x| File.directory?(x)}) #Remove all layout files in base dir
    files_to_copy=Dir.glob("**/*").reject{|x| File.directory?(x)}
    files_to_copy.each{|x| print "Copying #{x} to #{directory}\n"}
    FileUtils.cp(files_to_copy, directory) #Copy all files in subdir into base dir
end

if ARGV[0]!=nil && File.directory?(ARGV[0])
    xml_dir=ARGV[0]
    layout_dir="#{xml_dir}/layout"
    collect_files(layout_dir)
else
    puts("Must specify a valid directory!")
end


请注意,上述脚本并不健壮,并且实际上会删除子目录中的任何布局文件。如果您愿意,您可以随时删除删除步骤,但是从子目录中删除的任何文件都将保留在主目录中以供后续构建使用。

如果您正在运行 Eclipse,则可以配置外部工具,稍后可以将其添加到构建器中。只需打开“运行”->外部工具 ->外部工具配置,并在“程序”下创建一个新工具。这是我的设置的屏幕截图:

外部工具配置

注意:工作目录是一个红鲱鱼,不会'不被使用。您需要指定放置脚本的位置,而不是此处显示的位置

现在,您可以将该工具添加到项目的构建器中。选择您的项目并打开 Project ->特性。现在选择“Builders”项目并单击“导入”。如果您定义成功,您应该在那里看到您的工具。它需要在构建过程的其余部分之前运行,因此请确保将其移至列表顶部。完成后,它应该如下所示:

Project Builders configuration

现在,您只需将布局文件移动到子目录中(但注意避免名称冲突,请记住这些文件最终将位于构建的同一目录中!)并构建您的项目。当您执行此操作时,您会看到它们神奇地出现在 /res/layout/ 的根目录中,然后您的应用程序应该可以正常构建。

警告脚本编写者:如果您指定多个布局,或者使用 /res/layout/ 目录以外的其他内容,则需要扩展此脚本或多次添加该工具以用于不同的布局。目录来处理它。我个人没有使用这种技术,所以还没有看到它失败的地方,但已经使用基本的 Android Hello World 应用程序进行了测试,并在一些子目录中使用了几个布局。

< strong>此外,如果与包含空格的路径一起使用,我的脚本将会中断!

You can achieve this by using a custom script and having it run before the build executes. Android seems to ignore anything in layout subdirectories, so you can safely put your files into them. The following ruby script (written for Linux, but easily convertible to other platforms) will then delete everything that's not a directory in res/layout/ and copy every file from the subdirs into res/layout/:


#!/usr/bin/ruby

require "fileutils"

def collect_files(directory)
    FileUtils.cd(directory)
    FileUtils.rm(Dir.entries(directory).reject{|x| File.directory?(x)}) #Remove all layout files in base dir
    files_to_copy=Dir.glob("**/*").reject{|x| File.directory?(x)}
    files_to_copy.each{|x| print "Copying #{x} to #{directory}\n"}
    FileUtils.cp(files_to_copy, directory) #Copy all files in subdir into base dir
end

if ARGV[0]!=nil && File.directory?(ARGV[0])
    xml_dir=ARGV[0]
    layout_dir="#{xml_dir}/layout"
    collect_files(layout_dir)
else
    puts("Must specify a valid directory!")
end


Be warned that the above script is not robust, and will actually delete any layout files not in a subdirectory. You can always remove the deletion step if you like, but then any files you remove from the subdirectories will remain in the main directory for subsequent builds.

If you're running Eclipse, you can then configure an external tool, which you can add to your builders later. Just open up Run -> External Tools -> External Tools Configurations, and create a new tool under 'Programs'. Here a screenie of my settings:

External tool configuration

Note: The working directory is a red herring, and won't be used. You'll need to specify the location where you drop the script, not the one shown here

Now you can add the tool to the builders for your project. Select your project and open up Project -> Properties. Now Select the 'Builders' item and click 'Import'. You should see your tool there if you defined it successfully. It needs to run before the rest of the build process, so make sure to move it up to the top of the list. Here's what it should look like when you're done:

Project Builders configuration

Now you just move layout files into subdirectories (but watch out for name collisions, remember the files will all end up in the same directory for the build!) and build your project. You'll see them magically appear in the root of /res/layout/ when you do this and your app should then build normally.

Caveat Scriptor: If you're specifying multiple layouts, or anything else which uses more than just the /res/layout/ directory, you'll need to extend this script or add the tool multiple times for the different directories to handle it. I don't personally use this technique, and so haven't seen where it falls down, but have performed a test with a basic android Hello World app with a couple of layouts in some subdirectories.

Also, my script will break if used with paths containing spaces!

蓦然回首 2024-11-04 03:27:57

简短的回答:不能那样做。
主要原因:

  • res/layout文件夹中,所有.xml文件都经过预编译,因此Android可以将它们用作资源。在 assets 文件夹中,所有文件都保持不变,因此应用程序可以将它们作为常规文件读取。 (在您的代码示例中,您获得的是InputStream,而不是资源ID)。
  • Android 自动管理位于 res/layout 文件夹中的布局,搜索与当前屏幕分辨率、方向、区域设置等最匹配的布局。

The short answer: it can't be done in that way.
The main reasons:

  • In the res/layout folder all .xml files are precompiled, so Android can use them as resources. In assets folder all files remain intact, so the app can read them as regular files. (In your code example you get InputStream, not resource ID).
  • Android automatically manages layouts located in the res/layout folder, searching the best matched to current screen resolution, orientation, locale, etc.
生生漫 2024-11-04 03:27:57

布局充气机可能会帮助你
看这里
Android 中的 LayoutInflater 是做什么的?

layoutinflator might help you
look here
What does LayoutInflater in Android do?

橘香 2024-11-04 03:27:57

我认为这不是一个好主意,而且可能行不通。例如,如果框架认为需要布局的 hdpi 版本,它会做什么?

您可能会尝试追寻布局激增的根本原因:

  • 您能否为布局开发更好的命名约定?
  • 您能否重构某些布局,以便更多组件可以重用相同的布局?
  • 您是否手动处理方向而不是依赖 -portrait-landscape

I don't think this is a good idea, and probably just doesn't work. What would the framework do if it thought it needed an hdpi version of your layout, for example?

You might try to go after the root cause of your layout proliferation:

  • Could you develop better naming conventions for your layouts?
  • Could you refactor certain layouts so that more components can reuse the same layouts?
  • Are you manually handling orientations instead of relying on -portrait and -landscape?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文