The Absolutelayout, as you started to mention, allows you to specify the exact location of your children. The location of views can be specified using attributes layout x
and layout y
both values are mentioned in dip
or dp
(Density Independent pixels). Absolute positioning is not very useful in the world of millions of screen resolutions and aspect relations, perhaps for this reason has become obsolete.
Perhaps the most viable alternative to replace the AbsoluteLayout
would even be the Framelayout, where in general, you can add a view in a specific position containing as attributes the leftMargin
and topMargin
, which would basically serve as your coordinates. Let’s say you need to use a size image 40x40
in position 80x100
, then you’d have something like this:
Java
FrameLayout frame = (FrameLayout)findViewById(R.id.frame);
ImageView iv = new ImageView(this);
iv.setBackgroundColor(Color.BLUE);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(40, 40);
params.leftMargin = 100;
params.topMargin = 80;
frame.addView(iv, params);
XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frame"
android:background="#FFFF"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
The image below illustrates the view regarding ImageView
and to the spacing. See:
I found that answer in the global OS suggesting as an alternative RelativeLayout
, but particularly perhaps, I say perhaps, it is not a good option because there are resources RelativeLayout
, let’s just say, it would basically be useless, making it not the best alternative. Analogically you would be taking Swiss Army knife to the bar just to use beer opener.