The previous one we studied (1.[ A brief introduction to WebView's learning]:web apps), let's continue our study today.
(blog address: http://blog.csdn.net/developer_jiangqq), reproduced please specify.
Author:hmjiangqq
Email:[email protected]
because Android the device has very many resolutions, different screens, and pixel densities. So in the Web page design process, should consider the page always display the appropriate size.
when you develop Web pages for Android devices, we need to consider the following two factors :
1.: view window ( the ViewPort)
The Viewport is a rectangular shape that can be scaled. Web page. You can set multiple Viewport properties, such as size and initialization scale. The most important is the view port width, which defines the horizontal pixel size of the entire Web page.
2: screen Density (the screen Density)
in theAndroidEquipment aboveWebViewcontrols and mostweb The browser is able to put css web page on a medium-density screen ( for example :160dpi) 300px 320 dpi
( a ): Specify form Properties (specifying Viewport Properties )
The window is the area of a Web page, and the Visual window can match the size of the screen as we zoom in. Like:Instant when the physical width of our device is480px (Pixel), VieWPortthe width is800px (Pixel),when the viewport is the normal scaling ratio is(1.0)the based -px Device'sWeThe b page will also be fully displayed on the screen. Most Web browsers in Android(like: Chrome)The default setting is a large size view(should be called"Wide view mode"-generally for980px). Very many browsers also shrink as much as possible to show the full size by default(should be called:Profile Mode)
[ note ]: When you use WebView to display a page, the default does not enable wide view mode ( This will make the page fully enlarged.) Does not fit the screen ), of course you can call setusewideviewport () to enable wide view mode
you are able to HTML of the document < Head > used in <metaname= "Viewport" ...> Label for your Web the page defines the properties of the view, such as : width and initialization scale.
Below are Viewport all supported properties and values that can be set
<span style= "FONT-SIZE:18PX;" ><meta name= "viewport" content= "height = [Pixel_value | "Device-height"], width = [Pixel_value | "Device-width"], Initial-scale = float_value, Minimum-scale = float_value, Maximum-scale = f Loat_value, user-scalable = ["Yes" | "No"] "/></span>
like : in the following < Meta > tags are defined Viewport width fully fits the width of the screen, and setting users cannot zoom
<span style= "FONT-SIZE:18PX;" >
when optimizing Web pages for mobile devices. By we should set the width so as far as possible to fit the entire screen. Then use CSS style files to adjust the layout for different screen resolutions.
[ note ]: when you are sure that your Web page to display above the small resolution screen, you should call the SE T Usewideviewport (false) To disable wide view mode.
( two ) use for different screens CSS style (targeting Device Density with CSS)
AndroidBrowser andWebViewall supportCSSStyle file,This allows you to create a special screen density with"-webkit-device-pixel-ratio"of theCSSstyle file. The desired value of this property is"0.75", "1"or"1.5",the distinction represents low density, medium density. and high-density screens.
like : you can create a separate style file for each density :
<span style= "FONT-SIZE:18PX;" ><link rel= "stylesheet" media= "screen and (-webkit-device-pixel-ratio:1.5)" href= "Hdpi.css"/><link rel= " Stylesheet "media=" screen and (-webkit-device-pixel-ratio:1.0) "href=" Mdpi.css "/></span>
or be able to create different styles in a style file .
<span style= "FONT-SIZE:18PX;" > #header { background:url (medium-density-image.png);} @media screen and (-webkit-device-pixel-ratio:1.5) {/ * CSS for high-density screens */ #header { background : URL (high-density-image.png);} } @media screen and (-webkit-device-pixel-ratio:0.75) {/ * CSS for low-density screens */ #header { background : URL (low-density-image.png);} } </span>
many others deal with different screen resolutions. Information about a special resource picture. You can view :highDPI Images forvariable Pixel densities.
( three ): use for different screen resolutions JavaScript
AndroidBrowser andWebViewall supportDomproperty so that we can use thewindow.devicepixelrationthe DoMproperty to query the density of the current device. The value of this property represents the scale factor that is currently set. Like:AssumptionsWindow.devicepixelratiothe value is1.0, the device is considered to be a medium-density screen and is not scaled by default. Assume that the value is1.5, then the device is thought to be a high-density screen and will1.5magnification.
Assuming the value is 0.75, the device is considered a low-density screen and will be 0.75 scaled down. Of course, the scale of Android browser and WebView is based on the density of real-world web page devices, which are generally medium density by default, but you can change the density of different screens.
For example : The following is the use of Javascript to query device density
<span style= "FONT-SIZE:18PX;" >if (Window.devicepixelratio = = 1.5) {alert ("This was a high-density screen");} else if (Window.devicepixelratio = = 0. {Alert ("This was a low-density screen");} </span>
[WebView Learning II]: Support different resolution screens using Web Apps