用Java渲染Android XML布局 [英] Rendering an Android XML layout in Java

查看:122
本文介绍了用Java渲染Android XML布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Java应用程序,它是某种Android应用程序(APK文件)分析器.

I'm building a Java application that is some sort of Android applications (APK files) analyzer.

应用程序将提供的主要功能之一是Android布局的预览",因此我需要一个API,该API接收Android布局XML和一些配置参数(例如屏幕分辨率和主题),并返回渲染的布局(与在运行应用程序的设备上显示的布局(与真实Android平台的图形一致性很重要))以及View对象的位置数据(以便允许用户通过单击来选择视图).在第一阶段,我不希望该功能反映通过编程方式进行的布局更改,而仅反映XML中定义的View对象和资源图形.

One of the main features that the app will offer is a "preview" of an Android layout, hence I need an API that receives an Android layout XML and a few configuration arguments such as screen resolution and theme, and returns the rendered layout as it would appear on a device running the application (graphical consistency with the real Android platform is important) along with position data of the View objects (in order to allow the user to select a view by clicking it). At the first stage, I don't expect the feature to reflect layout changes that are made programmatically, but only the View objects and resource graphics defined in the XML.

我想到的想法是使用布局编辑器(例如ADT的编辑器或DroidDraw)的源代码,并将其集成到我的框架中,但后来我想知道-也许更好的方法是使用android API本身可以为我呈现布局(这更好,主要是因为我不需要为更高版本的OS重写代码).

The idea I have in mind is to use the source code of a layout editor, such as ADT's editor or DroidDraw, and integrate it into my framework, but then I was wondering - maybe a better way would be to use the android API itself to render the layout for me (this is better mainly because I won't need to rewrite my code for later versions of the OS).

所以我的问题是:API是否允许此类操作?还是有更好的方法?

So my question is: does the API allows such operations? Or is there an even better way?

欢迎任何建议和见解:)

Any suggestions and insights are welcomed :)

推荐答案

此任务肯定是可能的,但是,它并非一帆风顺.我建议您看一下Android Studio的源代码,更具体地说,有一个名为 LayoutLib的工具.

This task is definitely possible, however, it's not straightforward at all. I would suggest taking a look and Android Studio's source code, more specifically there is a tool called LayoutLib.

这是IDE的布局预览/编辑器用来渲染布局的工具.您可以使用它来呈现您具有源代码的布局和视图.不幸的是,它的文档还不够完善,因此您必须从IDE的源中找出用法.

This is the tool that the IDE's layout preview/editor uses to render layouts. You can use this to render layouts and views that you have the source code for. Unfortunately, it's not very well documented, so you have to figure out the usage from IDE's sources.

这篇关于用Java渲染Android XML布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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