AbsoluteLayout , as you started to mention, allows you to specify the exact location of your children. The location of the views can be specified using the and % attributes attributes with both% and% x
( density independent pixels ). Absolute positioning is not very useful in the world of millions of screen resolutions and aspect ratios, perhaps for this reason it has become obsolete.
The perhaps more viable alternative to replacing y
would be FrameLayout , where in general, you can add a view in a specific position containing as attributes dip
and dp
, which would basically serve as your coordinates. Let's say you need to use an image of size AbsoluteLayout
in position leftMargin
, so you would 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 topMargin
and spacing. See:
Ifound this answer in the global OS that suggests as 40x40
, but particularly perhaps, say maybe, not is a good option because there are resources of 80x100
, so to say, that basically would be unusable, making it not the best alternative. Analogously you would be taking Swiss Army Knife to the bar just to use beer opener.