When is/was Absolutelayout used?

Asked

Viewed 422 times

0

I know with this layout it is possible to define manually the positions x and y of the components inserted in layout.

In which hypothesis is/was recommended the use of AbsoluteLayout, since it works with fixed coordinates in the plane, which makes it impossible to have a good view of the Activity in other resolutions?

In the version of Android Studio I use (Windows/2.3), when I try to use this layout, the IDE brings me a message that it is considered obsolete. There is some alternative to this layout?

1 answer

1


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:

inserir a descrição da imagem aqui

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.