开发者

Can I find out width of screen programmatically in android app? [duplicate]

This question already has answers here: Get screen width and height in Android (36 answers) Closed 7 years ago.

Can I find out the width of screen programmatically in android app? I need to paint canvas but its width should be almost like screen and I could not set match_parent in java开发者_JS百科 part program.


You can get default Display instance and then read width/height from it:

int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight();


You can do this:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

dm.widthPixels contains the width and dm.heightPixels contains the height.


You could also overwrite onSizeChanged(int,int,int,int) in any view and set it in there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜