Android: AbsoluteLayout?

I am new to Android. I like to have a free range of drawing objects wherever I want. Therefore, I use Absolute Layout. I get a message saying that I am using a different layout. And I read that this is due to the different resolution of different phones. My question is that this is the only reason you are not using Absolute Layout? I created a method that uses metrics to adjust pixels.

public int widthRatio(double ratioIn){ DisplayMetrics dm = new DisplayMetrics(); //gets screen properties getWindowManager().getDefaultDisplay().getMetrics(dm); double screenWidth = dm.widthPixels; //gets screen height double ratio = screenWidth/100; //gets the ratio in terms of % int displayWidth = (int)(ratio*ratioIn); //multiplies ratio desired % of screen return displayWidth; } //method to get height or Ypos that is a one size fits all public int heightRatio(double ratioIn){ DisplayMetrics dm = new DisplayMetrics(); //gets screen properties getWindowManager().getDefaultDisplay().getMetrics(dm); double screenHeight = dm.heightPixels; //gets screen height double ratio = screenHeight/100; //gets the ratio in terms of % int newHeight = (int)(ratio*ratioIn); //multiplies ratio by desired % of screen return newHeight; } //sets size of any view (button, text, ...) to a one size fits all screens public void setSizeByRatio(View object, int width, int height){ LayoutParams params = object.getLayoutParams(); params.width = widthRatio(width); params.height = heightRatio(height); } 

So, if I say setSizeByRatio (Button, 10, 25); It will set the width of the buttons to 10% of the width of the screen and the height to 25% of the screen.

Are there any phones that Absolute layouts do not work on? Does this layout cause any other problems?

+6
source share
2 answers

The reason AbsoluteLayout is deprecated is because you have alternatives in LinearLayout or GridLayout that do the same and much more. It seems that you are trying to calculate positions based on absolute positions and the number of pixels. This is an approach that should generally be avoided due to problems with screen sizes and screen sizes.

Read the link provided by @amiekuser and focus on understanding how best practice is. Some tooltips create images for the ldpi, mdpi and hdpi folders using the dpi block (without pixel density) instead of raw pixels and how to test your application on multiple screen sizes and densities using an emulator.

Edit:

To set the x- and y-position of the View, you must use LayoutParams. See this question on how to install TopMargin and LeftMargin for viewing using LayoutParams.

+6
source
Android phones have many form factors, that is, not only it varies greatly depending on the screen size (2.7 ", 3.2", 3.7 "...), but also in terms of resolution (480X800, 480X848 etc.). Google itself suggests not using AbsoluteLayout. in fact, it is deprecated in newer versions of api.

The link below details all of this:

http://developer.android.com/guide/practices/screens_support.html

Check out the "Best Practices" section.

+3
source

Source: https://habr.com/ru/post/910773/


All Articles