java android 布局,用Java渲染Android XML布局

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

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.

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).

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

Any suggestions and insights are welcomed :)

解决方案

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值