The previous one we studied (1.[ WebView Learn one]:web apps introduction), today we will continue to learn.
(blog address: http://blog.csdn.net/developer_jiangqq), reproduced please specify.
Author:hmjiangqq
Email:[email protected]
because Android The device has a lot of resolution, different screen and pixel density, 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 Form ( the ViewPort)
the Viewport web Page. You can set multiple vie wport properties such as : View port width ( width of the view )
2: screen density (The screen Density)
android device above webview controls and most web The browser will be able to put css The pixel value of web page on a medium-density screen ( for example Span lang= "en-US" style= "Font-family:calibri" >:160dpi) The same size is present on the above. If your Web design Picture is the most important, then you should pay more attention to the conversion of different resolution densities. Because The 300px width of the soil will be magnified on the abovedpi screen.
( a ): Specify window Properties (specifying Viewport Properties )
a form is the area of a Web page, and the visual form can match the size of the screen instantly when we zoom in. For example: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(For example: Chrome)The default setting is a large size view(should be called"Wide view mode"-generally for980px). 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 and will not fit the screen ) and of course you can call SE T Usewideviewport () to enable wide view mode
you can Span lang= "en-US" style= "Font-family:calibri" >html documents < Head > <metaname= "viewport" ...; label for your web page defines properties of the view, such as :
Below is 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>
For example : in the following < Meta > tags are defined Viewport width fully fits the width of the screen and sets the user not to scale
<span style= "FONT-SIZE:18PX;" >
when optimizing Web pages for mobile devices, By we should set the width "Device-width"
[ 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.
For example : 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 you can create a different style 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>
more information on handling different screen resolutions, special resource images, you can view : High DPI 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. For example:ifWindow.devicepixelratiothe value is1.0, the device is considered to be a medium-density screen and is not scaled by default. If the value is1.5, then the device is considered a high-density screen and will be1.5magnification. If the value is0.75,at this point the device is considered a low-density screen and will be0.75Zoom out. 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>
2.[webview Learning: Using Web Apps to support different resolution screens