Android Universal resolution adaptation method

Source: Internet
Author: User

(1) Get the size of the screen
WindowManager WindowManager = (windowmanager) getsystemservice (context.window_service);D isplay d = Windowmanager.getdefaultdisplay (); mwidth = D.getwidth (); mheight = D.getheight ();D isplaymetrics dm = getResources (). Getdisplaymetrics (); mscreendensity = dm.density;
(2) Dimensions of art design drawings
Uiwidth,uiheight
(3) Get zoom ratio

(4) The dimensions of all layouts are implemented in code
public static int getwidthsize (int size) {return (int) (size * scalewidth);} public static int gethightsize (int size) {return (int) (size * scaleheight);} public static float gettextsize (int pxsize) {return (pxsize*scaleheight)/mscreendensity;}    public static void Setviewsize (int width, int height, View v) {int paramwidth = getwidthsize (width);    int paramheight = gethightsize (height);    Viewgroup.marginlayoutparams params = (viewgroup.marginlayoutparams) v.getlayoutparams ();    if (width = INVALID) {params.width = Paramwidth;    } if (height! = INVALID) {params.height = Paramheight; } v.setlayoutparams (params);}    public static void setviewpadding (int left, int top, int. right, int. Bottom, View v) {left = Getwidthsize (left);    top = gethightsize (top);    right = Getwidthsize (right);    Bottom = getwidthsize (bottom); V.setpadding (left, top, right, bottom);} public static void Setviewmargin (int left, int top, int. right, int bottom,        View v) {int paramleft = Getwidthsize (left);    int paramtop = gethightsize (top);    int paramright = Getwidthsize (right);    int parambottom = gethightsize (bottom);    Viewgroup.marginlayoutparams params = (viewgroup.marginlayoutparams) v.getlayoutparams ();    if (left! = INVALID) {params.leftmargin = Paramleft;    } if (right! = INVALID) {params.rightmargin = Paramright;    } if (top! = INVALID) {params.topmargin = Paramtop;    } if (bottom! = INVALID) {params.bottommargin = Parambottom; } v.setlayoutparams (params);}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.