如何写一个Android的应用程序以同样的方式作为的JFrame [英] How to write a app for Android in the same way as JFrame

查看:402
本文介绍了如何写一个Android的应用程序以同样的方式作为的JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装Android的ADT插件为Eclipse的,我不喜欢它。

I installed the ADT plugin for Android for Eclipse, and I don't like it.

我想用我自己的网格布局我想要的方式和设计窗口由code(同样的方式的JFrame在Java中,因为我已经知道如何做到这一点),不单击并拖动。

I would like use my own Grid Layout the way I want and design windows by code (the same way as JFrame in Java, because I already know how to do this) not by click and drag.

如何做到这一点在Android中?

How to do this in Android?

推荐答案

虽然我不认为这是一个伟大的想法写在Java UI code,这里就是你会怎么做。

While I don't think it's a great idea to write the UI code in java, here's how you would do it.

首先,你要看看 android.view。* android.widget。* Android的参考文档的包。

First, you'll want to look at the android.view.* and the android.widget.* packages in the Android Reference docs.

在一切在Xml对应一个查看(或子类)。所以,如果你看到的LinearLayout 在XML,然后会有一个的的LinearLayout Java组件以及

Everything the the Xml corresponds to a View (or Subclass). So, if you see LinearLayout in the Xml, then there will be a LinearLayout java component as well.

所以,在你的活动的 的onCreate()方法,在这里通常你刚才设置的内容资源XML视图中,可以实例化一个Widget,并直接进行设置。

So, in your Activity's onCreate() method, where normally you'd just set the content resource xml view, you can instantiate a Widget and set it directly.

public void onCreate(Bundle state) {
   TextView tv = new TextView();
   tv.setText("Hellow World);
   setContentView(tv);
}

您可能还需要到设置 LayoutPararms 为好,因为大多数部件需要知道他们是否应该换量或填充母。

You might also need to the set the LayoutPararms as well, since most widgets need to know if they should wrap content or fill the parent.

许多布局小部件类似于Swing容器,也就是说,他们接受孩子,所以你可以调用的addChild()与另一视图中创建一个小部件的层次结构。

Many of the Layout widgets are analogous to Swing Containers, ie, they accept children, so you can call addChild() with another view to create a widget hierarchy.

这应该足以让你开始。

最后一点,我想重申,虽然您可以构建的Java code完整的UI,XML结构确实提供了一些其他的好处。例如在Xml布局允许支持多种屏幕尺寸,或不同的布局(横向VS纵向,等),这是在Java中更难做(但不是不可能)。此外,您可能会认为,由于视图是在XML中,那么他们将是慢要在设备上创建。但是,Android的优化的XML资源,而事实上,我不认为他们甚至在的xml它们被编译到您的应用程序的时间。因此,资源的XML文件是非常有效的,写Java直接code的意见可能不会在性能方面得到你多少额外的。

As a final note, I'd like to reiterate that while you can build the complete UI in Java code, the Xml layout does offer some other benefits. For example the Xml layouts allow you to support multiple screen sizes, or different layouts (landscape vs portrait, etc), which is much harder to do (but not impossible) in Java. Also, you might be tempted to think that because the Views are in Xml, then they will be "slow" to be created on the device. But, Android optimizes the Xml resources, and in fact, I don't think they are even Xml at the time they are compiled into your App. So the resource Xml files are very efficient, and writing direct Java code for the Views probably won't get your much extra in terms of performance.

这篇关于如何写一个Android的应用程序以同样的方式作为的JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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