Friday, February 19, 2010

Absolute layout

Note: the absolute layout class is deprecated, you are encouraged to use Frame Layout or Relative layout.

The reason of that is that it won’t be compatible with all the android phones as they have different screen sizes and resolutions.

absolute layout lays widgets by specifying their exact X and Y positions. In android the origin (0,0) coordinate is located at the top left of the screen


absolute layout is defiend in XML as <AbsoluteLayout>

by default, if you define any control in absolute layout without defining it’s x,y coordinates, it will be placed in the origin point at (x,y)=(0,0)

if you define x,y values that are too large, the widget will not appear on the screen

you can specify the values of x and y by many units as shown


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="placed at 100,130 pixels (px)"
android:layout_x="100px"
android:layout_y="130px"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="placed at 0,150 points (pt)"
android:layout_x="0pt"
android:layout_y="150pt"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="placed at 0.7,0.5 inches (in)"
android:layout_x="0.7in"
android:layout_y="0.5in"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="placed at 3,3 millimeters (mm)"
android:layout_x="3mm"
android:layout_y="3mm"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="placed at 0,270 density independant pixels (dp)"
android:layout_x="0dp"
android:layout_y="270dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="placed at 0,180 scale independant pixels (sp)"
android:layout_x="0sp"
android:layout_y="180sp"
/>

To define absolute layout from the code you can use the following code:
AbsoluteLayout abslay=new AbsoluteLayout(this);
Button btn=new Button(this);
btn.setText("Hello");
abslay.addView(btn, new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT,AbsoluteLayout.LayoutParams.WRAP_CONTENT,10,100));
setContentView(abslay);
the absolutelayout.layoutparams function has the following constructor:
AbsoluteLayout.LayoutParams(width,height,position X,position Y)

No comments:

Post a Comment