简体   繁体   English

Android GUI XML vs Code

[英]Android GUI XML vs Code

I have a short question according to creating GUIs in android. 根据在android中创建GUI,我有一个简短的问题。 What way is better xml or coding? 什么方式更好的xml或编码? I always read that xml is way better but imagine you have a scrollview. 我总是读到xml更好,但想象你有一个scrollview。 Inside the scrollview is a relativelayout. 在scrollview中是一个相对布局。 Inside that there shall be several rows with an ImageView next to a TextView next to a RadioButton. 在里面,应该有几行,在RadioButton旁边的TextView旁边有一个ImageView。 The number of rows can vary. 行数可以变化。 Is it really better to make lets say 50 Views in the xml or a loop in code where these views are created? 让我们说xml中的50个视图或创建这些视图的代码中的循环真的更好吗?

Each has its pros and cons. 每个都有其优点和缺点。 Just to name a few: 仅举几个:

XML XML

  • pros -> fast GUI development, keep code clean 专业 - >快速GUI开发,保持代码清洁
  • cons -> static 缺点 - >静态

Dynamic (code) 动态(代码)

  • pros -> able to react to runtime conditions 专业 - >能够对运行时条件做出反应
  • cons -> more code, which means poorer maintainability and potentially buggier 缺点 - >更多代码,这意味着较差的可维护性和潜在的buggier

If you need to add components dynamically, only way is go with code (or) mixed approach (define layout in XML and add components in code). 如果您需要动态添加组件,那么只能使用代码(或)混合方法(在XML中定义布局并在代码中添加组件)。 If your components are static XML may be best. 如果您的组件是静态的,则XML可能是最佳的。

Dynamic content is, of course, added dynamically. 当然,动态内容是动态添加的。 So your example would require some java code. 所以你的例子需要一些java代码。 You should only add the dynamic part programmatically though, so you'd still use an xml document for the static parts (it's very unusual for a layout to be completely dynamic). 您应该只以编程方式添加动态部分,因此您仍然使用xml文档作为静态部分(布局完全是动态的非常不寻常)。

If you have a fixed number of views then yes, I'd write 50 of them in xml rather than with a loop. 如果您有一定数量的视图然后是,我会用xml而不是循环编写50个。 I guess you're wondering about code duplication and, as far as I know, you'll get some when using xml. 我猜你想知道代码重复,据我所知,你在使用xml时会得到一些。

(One way to minimize code duplication within xmls' is with the usage of styles and themes) (在xmls中最小化代码重复的一种方法是使用样式和主题)

I agree with the above. 我同意以上所述。 XML is a better approach to this even when you require dynamic updates you can still use XML bits and pieces to render the content. XML是一种更好的方法,即使您需要动态更新,您仍然可以使用XML比特来呈现内容。 your code will be based on XML elements but XML files will be independent. 您的代码将基于XML元素,但XML文件将是独立的。 hence if you break a funcitonality in the code you know that its your business logic thats broken not the UI part, which will make it easier to develop and find problems easily. 因此,如果你破坏了代码中的功能,你就会知道它的业务逻辑不会破坏UI部分,这样可以更容易地开发和轻松发现问题。

Why you do not use a ListView instead of a ScrollView. 为什么不使用ListView而不是ScrollView。 It will be simplier to implement and performances must be better with it. 实现起来会更简单,性能必须更好。

Create a XML file with a ListView and in your activity implements your own adapter to instanciate the rows. 使用ListView创建XML文件,并在您的活动中实现您自己的适配器以实现行。 You can find a lot of tutorials on internet talking about that, I'm sure you will find what you need ! 你可以在互联网上找到很多关于这个的教程,我相信你会找到你需要的东西! Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM