最佳实践:在Android布局(编程VS XML) [英] Best practices: Layouts on Android (Programmatic vs XML)

查看:129
本文介绍了最佳实践:在Android布局(编程VS XML)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题已被窃听了我一段时间。我已经开发了几个应用程序在Android平台上,不知怎么总能找到自己诉诸的Java code,以构建布局。以专业的开发环境,这是可以接受的?还是应该XML文件是去到接近?我经常发现XML一个比较繁琐的做法,往往,这些布局看起来不一样的所有设备。我只是不明白这一点。从专业的角度来看,有没有人真的能够开发复杂的观点纯粹是使用XML文件的应用程序?这个问题是我的命,因为谷歌建议使用XML,但用户界面永远看起来是一样的所有的设备,除非进行编程。还是我做错了什么?

This question has been bugging me for some time. I've already developed a couple of apps on the Android platform and somehow always find myself resorting to Java code in order to construct the layouts. In a professional development environment, is this acceptable? Or should XML files be the go-to approach? I usually find XML a more tedious approach and often, these layouts don't look the same on all devices. I just don't get it. From a professional viewpoint, has anyone really been able to develop apps with complex views purely using XML files? This question is killing me because Google recommends using XML but the UI never looks the same on all devices unless done programmatically. Or am I doing something wrong?

请注意,我指的是Android的2.2和2.3,其中大部分用户的使用。

Note that I'm referring to Android 2.2 and 2.3, which majority of the users use.

推荐答案

我使用的每一个应用程序我写的每一个很多片段与活动在pretty的XML布局。我很少看到任何需要动态地创建视图,寿列表视图的配置,显示/隐藏意见等需要做的code。对于我的XML的优点是:

I use XML layouts on pretty much every fragment and activity of every app I write. I very rarely see any need to create Views dynamically, tho configuration of ListViews, showing/hiding views, etc needs doing in code. For me the advantages of XML are:

  • 能够使用版图编辑器(Eclipse中)
  • 更容易preVIEW布局
  • 可以从自动本地化布局受益
  • 轻松地维护不同的平行布局的差异设备(屏幕)
  • 可以得到的布局感由看它(比code更容易)
  • 易于向下突破布局成片(片段,包括等),以消除重复
  • 在保持视觉设计之间的分离,以及其背后的功能

我想不出任何理由把我所有的布局为code - 这听起来像地狱

I can't think of any good reasons to put all my layouts into code - that sounds like hell.

我期待你的布局看起来不一样的原因是因为你的XML是不正确定义的布局。记住了Android工具转换XML布局为code,所以没有固有的问题,使用XML布局与动态 - 无论最终为code

I expect the reason your layouts don't look the same is because your XML is not defining the layouts correctly. Bear in mind the Android tools convert XML layouts into code, so there's no inherent problem with using XML layouts versus dynamic - both end up as code.

这篇关于最佳实践:在Android布局(编程VS XML)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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