Android: adaptive to different screens, android: Adaptive
Because the screen sizes and resolutions of Android devices vary greatly, If you want our applications to run on Android devices with different screen sizes or resolutions, that is to say, after the Android device is changed, the interface and font will not become chaotic, so we need to consider the screen's adaptability. Concepts: (1) Screen size: refers to the diagonal line length of the Screen. The Screen size can be divided into small (small Screen), normal (medium Screen), and large (large Screen), xlarge (ultra-large screen); (2) Resolution (dp): The number of pixels on the screen, which can be divided into ldpi (low resolution), mdpi (Medium Resolution), hdpi (High Resolution), xhdpi (Super High Resolution);> xlarge screen size resolution requires at least 960dp * 720dp> large screen size resolution requires at least 640dp * 480dp> normal screen size resolution requires at least 470dp * 320dp> small screen size resolution requires at least 450dp * 320dp (3) pixel density (dpi): The number of pixels contained in the screen per inch (android also divides the pixel density into four levels: low, medium, high, and extra high); (4) direction (Orientation): horizontal and vertical separation. If the application performs well, both directions should be considered. android classifies different sizes and pixel density levels:1. Use the layout_weight attribute MethodCurrently, the Android multi-screen adaptive solution is the most recommended. This attribute determines the display weight of the control in its parent layout. It is generally used in linear layout. The smaller the value of layout_weight, the higher the priority of the corresponding layout_width or layout_height. In general horizontal layout, the priority of layout_width is determined. In vertical layout, the priority of layout_height is determined. Currently, there are two methods: (1) traditional use: Set layout_width and layout_height to fill_parent, and control the display ratio of the control through the layout_weight attribute. When layout_weight is smaller, the display ratio of the control is larger. However, if there are many controls in the layout and the display ratio is different, the traditional layout_weight attribute method will be more troublesome. (2) 0px Value Method: Even if layout_width = "0dp" or layout_height = "0dp", the display of the control is controlled based on the positive ratio of the layout_weight attribute, this method effectively solves one of the current fragmentation issues in Android development.Source code example:1. res/values/style_layout.xmlFunction: encapsulate the layout_width and layout_height attributes of the control into four styles. <? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <! -- Full screen stretch --> <style name = "layout_full"> <item name = "android: layout_width"> fill_parent </item> <item name = "android: layout_height "> fill_parent </item> </style> <! -- Fixed its own size --> <style name = "layout_wrap"> <item name = "android: layout_width"> wrap_content </item> <item name = "android: layout_height "> wrap_content </item> </style> <! -- Horizontal distribution --> <style name = "layout_horizontal" parent = "layout_full"> <item name = "android: layout_width"> 0px </item> </style> <! -- Vertical Distribution --> <style name = "layout_vertical" parent = "layout_full"> <item name = "android: layout_height "> 0px </item> </style> </resources>2. res/layout/weight_layout.xml<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" style = "@ style/layout_full" android: orientation = "vertical"> <LinearLayout style = "@ style/layout_vertical" android: layout_weight = "1" android: orientation = "horizontal"> <View style = "@ style/layout_horizontal" android: background = "# aa0000" android: layout_weight = "1"/> <View style = "@ style/layout_horizontal" android: background = "#00aa00" android: layout_weight = "4"/> <View style = "@ style/layout_horizontal" android: background = "# tianaa" android: layout_weight = "3"/> <View style = "@ style/layout_horizontal" android: background = "# aaaaaa" android: layout_weight = "2"/> </LinearLayout> <LinearLayout style = "@ style/layout_vertical" android: layout_weight = "2" android: orientation = "vertical"> <View style = "@ style/layout_vertical" android: background = "# ffffff" android: layout_weight = "4"/> <View style = "@ style/layout_vertical" android: background = "# aa0000" android: layout_weight = "3"/> <View style = "@ style/layout_vertical" android: background = "#00aa00" android: layout_weight = "2"/> <View style = "@ style/layout_vertical" android: background = "# tianaa" android: layout_weight = "1"/> </LinearLayout>3. effect demonstration480*800 and 320*480 respectively.
Ii. Custom Dimension MethodThe Custom size method defines a set of interface sizes for each screen interface.
1. res/values-480x320/dimen_height.xmlFunction: defines the sizes of two sets applied to different interfaces.Dimen name = "height_000080"> 6dp </dimen> <dimen name = "height_3_80"> 18dp </dimen> <dimen name = "height_5_80"> 30dp </dimen> <dimen> name = "height_7_80"> 42dp </dimen> <dimen name = "height_9_80"> 54dp </dimen> <dimen name = "height_11_80"> 66dp </dimen> <dimen name = "height_13_80"> 78px </dimen> <dimen name = "height_15_80"> 90px </dimen> <dimen name = "height_17_80"> 102px </dimen> .... ....... </resources>2. dimen_layout.xmlFunction: different sizes are used for screens with different resolutions.<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <View android: layout_width = "@ dimen/width_76_80" android: layout_height = "@ dimen/height_10_80" android: background = "# ffcccc" android: layout_margin = "@ dimen/width_2_80"/> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <View android: layout_width = "@ dimen/width_30_80" android: layout_height = "@ dimen/height_50_80" android: background = "# ccccff" android: layout_margin = "@ dimen/height_5_80"/> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <Button android: layout_width = "@ dimen/width_30_80" android: layout_height = "@ dimen/Hangzhou" android: background = "# ccffcc" android: layout_marginBottom = "@ dimen/height_000080" android: text = "5"/> <Button android: layout_width = "@ dimen/width_30_80" android: layout_height = "@ dimen/height_10_80" android: background = "# ccffcc" android: layout_marginBottom = "@ dimen/height_000080" android: text = "10"/> <Button android: layout_width = "@ dimen/width_30_80" android: layout_height = "@ dimen/height_15_80" android: background = "# ccffcc" android: layout_marginBottom = "@ dimen/height_000080" android: text = "15"/> <Button android: layout_width = "@ dimen/width_30_80" android: layout_height = "@ dimen/height_20_80" android: background = "# ccffcc" android: text = "20"/> </LinearLayout>3. effect demonstration
3. Multi-LayoutMultiple la s are used to design separate la s for screens of different sizes. To provide adaptive resources for different screens, we can provide corresponding layout resources and Drawable resources for different screen sizes and different screen resolutions. Note that the following code must be added to the configuration file to use the multi-layout method. Otherwise, an exception occurs.
1. How to support multiple screens(1) display the list of screen sizes supported by your application (AndroidMnifest. xml)
- <Supports-screens
- Android: resizeable = ["true" | "false"]
- Android: smallScreens = ["true" | "false"]
- Android: normalScreens = ["true" | "false"]
- Android: largeScreens = ["true" | "false"]
- Android: xlargeScreens = ["true" | "false"]
- Android: anyDensity = ["true" | "false"]/>
2. Text adaptive(1) dip: device independent pixels (device independent pixel ). different devices have different display effects, which are related to the hardware. Generally, to support WVGA, HVGA, and QVGA (WVGA = 800x480, HVGA = 480x320, QVGA = 320x240) This is recommended and does not depend on pixels. (2) px: pixels (pixels). Different devices have the same display effect. Generally, we use HVGA to represent 320x480 pixels, which is usually used. (3) pt: point, a standard unit of length, 1pt = 1/72 inch, used in the printing industry, very easy to use; (4) sp: scaled pixels (zoom in pixels ). it is mainly used to display the best for textsize in the font. You can scale the font size according to the user's font size preferences based on the above-mentioned unit analysis. Using sp as the unit, you can achieve adaptive font size. Create a folder in the res folder called values-320x240. Where 320x240 is the resolution of your phone screen, according to your phone screen do different names, such as values-800x480. Create a dimens. xml file in the folder to define the font size. Then the system will automatically call the response folder based on the resolution of your mobile phone screen. In addition, remember the dimens under your default values file. the corresponding font size should also be written in the xml file, because when the system cannot recognize the screen size of your mobile phone, it will automatically find the content in your default file. If it is not written, the program will crash.
3. Adaptive Layout(1) provide different la s based on different screen sizes. By default, android will automatically adjust the layout of the application. However, in most cases, the layout varies according to the general size, which is small, normal, and large, larger to add different layout resources. For example, to support a screen with a size of large, you need to create a folder named layout-large/In the res directory and provide layout. Of course, you can also create the layout-port and layout-land directories under the res directory, which place two layout files, vertical screen and horizontal screen, respectively, to automatically switch the portrait screen. Layout: Default medium screen layout-small: small screen layout-large: large Screen layout-xlarge: special screen layout-land: horizontal screen layout-port: vertical screen note: android3.2 and later versions are recommended to add-sw <N> dp to the layout and values directories. That is, the resource can be used only when the screen size is at least N DSPs in width. (2) provide different screen density and different bitmap drawables. By default, the system automatically adjusts and scales the bitmap, but it is inevitable to stretch the bitmap. To ensure that your bitmap is the best-looking, add different bitmap resources based on the broad density, low, medium, high, and extra height. For example, to provide a suitable picture for a screen with a low density, you need to create a folder named drawable-ldpi /. Try to use images in the format of. The image size is determined as follows: low: medium: high: xhigh: 3: 4: 6: 8. For example, for a medium screen, if the pixel size of your image is 48 × 48, the low-density screen size should be 36 × 36, and the high) is 72 × 72, extra high is 96 × 96. The Android system supports multiple configuration resource files. We can append a new resource directory to your Android project. Differences between drawable-hdpi, drawable-mdpi, and drawable-ldpi:> drawable-hdpi stores high-resolution images, such as WVGA (480x800), FWVGA (480x854)> drawable-mdpi stores medium-resolution images, such as HVGA (320x480)> drawable-ldpi stores low-resolution images, such as QVGA (240x320)
Reference: http://www.eoeandroid.com/thread-173973-1-1.htmlhttp://blog.csdn.net/lonely_fireworks/article/details/7233575http://blog.csdn.net/kangyaping/article/details/7400280