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:
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.
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 开发者_如何学Pythonto use deprecated apis ?
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).
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.
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.
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?
Thanks.
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.
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 - 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 - 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.
Both SurfaceView and opengl (GLSurfaceView) are views, so they can be added to a layout as any other view
This is a FANTASTIC question. I am programming for 3.0 and above currently, and have decided for the most part to stick with AbsoluteLayouts in many cases. While it is set to be deprecated, compiling for 3.0ish and below most likely will still be able to run on 4 and beyond systems for a bit. Im searching for this answer myself (for future versions of the OS that cut it out). The reason they are cutting it out is because of there being no standardization of screen resolution on Android.
My suggestion would be to use AbsoluteLayouts for AS LONG AS WE CAN. I also think we should start a push to let Google know that it is still a Very necessary API to their platform.
Great Question :-)
There is a nice example in: http://www.google.com/events/io/2009/sessions/TurboChargeUiAndroidFast.html
Minute 44
And even better at: http://www.google.com/events/io/2009/sessions/SupportingMultipleDevicesBinary.html
The available documentation on custom layouts seems rather vague to me, but I finally found something useful: Here is a nice simple sample layout that shows how to connect XML and Java code: How to layout a 'grid' of images in the center of the screen And here is a post which explains one key issue I was missing: http://www.arpitonline.com/blog/2012/07/01/creating-custom-layouts-for-android/. Apparently one has to override onMeasure and call measure() for each child.
精彩评论