Android的自定义布局 [英] Android custom layout

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

问题描述

我花了一段时间昨天与Android布局(...或更好的自定义布局)苦苦挣扎。对于一般用途形成开箱布局,机器人提供足够多的。但是,当涉及到更复杂的用例(让我们用图片被放在一个椭圆形的路径重叠的转盘的例子),我很快就意识到的Andr​​oid的API的限制和复杂性。这是我的方法:

I spent sometime yesterday struggling with android layouts (... or better custom layouts). For general purposes forms the out of the box layouts that androids offers are more than enough. But when it comes to more complex use cases (let's take the example of the carousel with overlapping of images that are laid on an elliptical path) I quickly realized the limitation and complexity of android APIs. This was my approach:

  1. 使用开箱布局不是一个选项(对许多难以控制的问题来处理),可以被添加到容器图形对象然而需要(比方说ImageView的例如)是必要的,以便简单地进行转换,旋转等..通过内置的动画。

  1. Using out of the box layouts is not an option (to many unmanageable issues to handle) , however the need of graphical objects that can be added to a container (let's say ImageView for instance) is necessary in order to easily perform translation, rotations, etc... through built in animation.

在AbsoluteLayout似乎是最合理的布局可为宗旨。它允许添加派生视图对象和编写您自己的算法来定位。但是......这是去precated。真是可惜!谁愿意去使用precated的API?

The AbsoluteLayout seems the most reasonable layout available for the purpose. It allows to add derived view objects and write your own algorithm to position them. But... it is deprecated. That's a shame! Who wants to use deprecated apis ?

下一个最合理的做法是写你自己的布局子类神秘的ViewGroup ..这里是那里的所有东西土崩瓦解(至少它为我做的)。它是复杂的,误导性的和棘手做尽可能多的,因为它是写自己的自定义布局摆动(其实我做到了年前,我记得比较简单)。

The next most reasonable thing to do is to write your own layout sub-classing the mystic ViewGroup.. and here is where the all thing fall apart (at least it did for me). It is complicated, misleading and tricky to do it as much as it is to write your own custom layout for swing (I actually did it years ago, and I remember to be simpler).

然后(由绝望驱动)子类视图,覆盖的OnDraw方法,并直接在画布绘制似乎只有简单的解决方案(虽然这让我想起的Java2D的日子......花了大量时间来实现项目的高1%),但你放弃得心应手内置的,你想申请在画布上绘制图形动画的API。

Then (driven by desperation) sub-classing a View, overriding the onDraw method and drawing directly on a Canvas seems the only straightforward solution (although this reminds me the days of java2D... long hours spent to achieve the high 1% of your project), but then you are giving up the handy built in animation APIs that you wish to apply to the graphics drawn on the canvas.

结论是,除非我错过了一些东西,目前的Andr​​oid架构是有点远从其他一些UI技术如闪存或JavaFX的提供的编程范式(我可以轻松地实现在JavaFX和闪存上述旋转木马)。 其原因在于,在JavaFX中,例如,在默认情况下Scene对象(堪比查看机器人的概念)提供了一个绝对布局。然后可以,如果你想使用其他开箱布局。但对于场景的自定义布局是必要的,你不必做任何事情。

The conclusion is that, unless I have missed something, the current android architecture is a bit far from the programming paradigm offered by some other UI technologies such as flash or javafx (I can easily implement the above described carousel in javafx and flash). The reason is that, in javafx for instance, the Scene object (comparable to the View concept of android) offers by default an absolute layout. You can then apply other out of the box layouts if you want to. But for scenarios where a custom layout is necessary you don't have to do anything.

我有点吃惊,这个选项是不是在Android的使用,毕竟是一个相当普遍的需求是由其他UI技术处理。我还是喜欢认为这是在android的第5选择,我还没有找到。 任何专家在那里,可以提供关于该主题的一些信息?

I am a little bit surprise that this option is not available in android, after all is a quite common need that is addressed by other ui technologies. I still like to think there is a 5th option in android that I haven't found. Any expert out there that could provide some information on the topic?

感谢。

推荐答案

我觉得Android的布局是非常适合的的用例与用户交互,但(大但)布局机制非常娇嫩,是棘手正确地扩展。对于小tweeks它的罚款,但如果你想要做一些与众不同的(如动画旋转木马),你可能做免费的图形无论如何,所以你不需要额外的扩展布局的复杂性。这只是更多的约束住在一起,没有合理的附加价值。

I think that Android Layout are great for most use cases for interacting with users, BUT (a big But) the Layout mechanism is very delicate and is tricky to extend correctly. For small tweeks it's fine, but if you want to do something out of the ordinary (such as an animated carousel), you probably do free graphics anyway, so you don't need the extra complexity of extending Layout. It's just more constraints to live with, with no justifiable added value.

大多数应用程序有一些标准的东西(动作条,按键,preferences,对话框等),以及一个或多个唯一的元素。所以我的建议是使用标准布局为标准的东西,但你的独特的元素使用其中的一个:

Most apps have some standard stuff (ActionBar, buttons, preferences, dialogs, etc), and one or more "unique" elements. So my suggestion is to use standard layout for the standard stuff, but for your unique elements use one of these:

1)SurfaceView - 非常容易使用。看到官方月球着陆 code例子。 要查看SurfaceView在行动,你也可以看看我的应用程序视频,它使用SurfaceView的动画元素,一切都铺有RelativeLayout的。

1) SurfaceView - very easy to use. See the official Lunar Lander code example. To see SurfaceView in action, you can also take a look at my app video, which uses SurfaceView for the animated element, and everything is laid out with RelativeLayout.

2)的OpenGL - 这是最有力的方式做图形在Android上。它运行在GPU上,让CPU可以自由地做其他的事情。而且它的3D(或2D,如果你只是用一个平面)。 我使用 libgdx ,它的伟大,而不是困难的一个很短的学习后,

2) opengl - It's the most powerful way to do graphics on Android. It runs on the GPU, so CPU can be free to do other things. And it's 3D (or 2D, if you just use one plane). I'm using libgdx, and it's great, and not difficult after a short learning.

两者SurfaceView和OpenGL(GLSurfaceView)是视图,所以它们可以被添加到布局的任何其他视图

Both SurfaceView and opengl (GLSurfaceView) are views, so they can be added to a layout as any other view

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

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