Alternative to AbsoluteLayout in Android?
Asked Answered
B

5

45

If AbsoluteLayout is deprecated what can I use instead of it?

I've done an app that uses AbsoluteLayout but it doesn't work well with the different screen resolutions. I use because I can set the X and Y position of a button. Can I set the position of a button using another layout?

Bushbuck answered 21/8, 2010 at 22:48 Comment(4)
You will be better served describing what you are trying to achieve. Part of the reason AbsoluteLayout is deprecated is because it does not support multiple screen sizes well. Any direct corollary to AbsoluteLayout (e.g., FrameLayout with margins) will suffer the same problem. You should start by seeking other solutions your problem, ones that will work better with a wide range of devices.Ingenue
Thank you. I've added some info now.Bushbuck
I've just released a library that would have been of interest for the OP: github.com/ManuelPeinado/ImageLayoutFlagg
I like AbsoluteLayout, it puts things where I tell it. I have an example using a button that is twice the width of another button but both come out the same size because some smart-ass layout thinks it knows what I am trying to do.Solifluction
E
24

you can use RelativeLayouts as described here: Set the absolute position of a view

Embay answered 22/8, 2010 at 15:37 Comment(2)
The big problem with RelativeLayout is that if you nest them, the number of calls to OnMeasure doubles at every step. So if you need a deep hierarchy, this is not a good answer.Giro
@WilliamJockusch I found this out the hard way myself. The solution (best sofar) is to use LinearLayout whenever it is possible (and it usually is), and to nest RelativeLayouts inside of LinearLayouts. I had a table view which I made with relative layout with nested RelativeLayouts - on changing only the top view to LinearLayout, I got a normal execution time. I would speculate that you could hack an "AbsoluteLayout" by creating a LinearLayout with RelativeLayout inside of it, but the sound of that is terrible.Asmara
B
12

Use RelativeLayout and set left and right margins like

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
lp.leftMargin = x;
lp.topMargin =  y;
Bullis answered 23/1, 2013 at 6:13 Comment(0)
Q
6

Consider using FrameLayout. If you set your child gravity to TOP|LEFT then you can use leftMargin and topMargin as the positions. As a bonus you get a few other useful positioning mechanisms by changing the gravity.

Quattlebaum answered 7/8, 2015 at 17:31 Comment(0)
V
1

I will suggest you guys if you want to have full control over the positions of your views on the screen just to extend ViewGroup and make your own implementation of AbsoluteLayout. The easiest solution will be to use one of the existing Layouts and play with margins, paddings, gravity and so on, but the control is not gonna be so powerful and can cost some problems on the diff device screens.

Valorievalorization answered 12/1, 2017 at 17:16 Comment(0)
B
0

I would just use a relative layout and set it based off of the other items/edges of the screen. Otherwise your layout appears different on different devices.

Birr answered 2/8, 2011 at 18:32 Comment(1)
I'm doing a graphic where I have to put text views in EXACT positions on a STATIC image. I think the only way would be (in effect if not technically) an absolute layout. Then I could do ones for various buckets but as it is I'm expecting a pain in the ass with it.Aleph

© 2022 - 2024 — McMap. All rights reserved.