If there is no getSize, you can use the getWidth and getHeight tools as follows:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
Answer: Balaji.K
I think it can be solved like this:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
If it doesn’t work, you can try the following code: The first two lines are about DisplayMetrics code, which includes the content of width pixels and height pixels:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
metrics.heightPixels;
metrics.widthPixels;
Answer: Francesco Feltrinelli
What I'm talking about may not be the answer you need, but I think it's useful to know. If you need the size of the View, but the layout has not been set yet, and the relevant code is executed, you can use View.getViewTreeObserver().addOnGlobalLayoutListener() to create a ViewTreeObserver.OnGlobalLayoutListener, and then enter the code related to the size of the View, so that The relevant parameters of the layout can be corrected in time.
Answer: Crbreingan
To accomplish dynamic scaling using XML, you can use the attribute android:layout_weight. The following example is an improved version of synic's answer on this thread, which expresses the following: the button occupies 75% of the screen (weight = .25), and the text view occupies 25% (weight = .75).
Answer: Josef Pfleger
(Best answer)
If you want to get the pixel size of the screen, you can use getSize:
If it is not in Activity, you can get the default value of Display through WINDOW_SERVICE:
If there is no getSize, you can use the getWidth and getHeight tools as follows:
Answer: Balaji.K
I think it can be solved like this:
If it doesn’t work, you can try the following code: The first two lines are about DisplayMetrics code, which includes the content of width pixels and height pixels:
Answer: Francesco Feltrinelli
What I'm talking about may not be the answer you need, but I think it's useful to know. If you need the size of the View, but the layout has not been set yet, and the relevant code is executed, you can use View.getViewTreeObserver().addOnGlobalLayoutListener() to create a ViewTreeObserver.OnGlobalLayoutListener, and then enter the code related to the size of the View, so that The relevant parameters of the layout can be corrected in time.
Answer: Crbreingan
To accomplish dynamic scaling using XML, you can use the attribute android:layout_weight. The following example is an improved version of synic's answer on this thread, which expresses the following: the button occupies 75% of the screen (weight = .25), and the text view occupies 25% (weight = .75).