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)

Related Posts by Categories

0 comments:

Post a Comment