屏幕适配问题

来源:互联网 发布:极品飞车ol车辆数据 编辑:程序博客网 时间:2024/06/05 03:12

以下根据谷歌官方的文档
Android categorizes device screens using two general properties: size and density. You should expect that your app will be installed on devices with screens that range in both size and density. As such, you should include some alternative resources that optimize your app’s appearance for different screen sizes and densities.

There are four generalized sizes: small, normal, large, xlargeAnd four generalized densities: low (ldpi), medium (mdpi), high (hdpi), extra high (xhdpi)

android 适配:为不同的屏幕大小设置不同的布局,为不同的屏幕设置不同的图片大小;

Each layout should be saved into the appropriate resources directory, named with a -<screen_size> suffix. For example, a unique layout for large screens should be saved under res/layout-large/

布局的格式一般是layout-large,layout-small等,但要求放置在里面的xml文件的名字是一致的,同时还可以设置横竖屏时的布局,横屏的标志是-land,竖屏protrait是默认的

MyProject/    res/        layout/              # default (portrait)            main.xml        layout-land/         # landscape            main.xml        layout-large/        # large (portrait)            main.xml        layout-large-land/   # large landscape            main.xml

Create Different Bitmaps

To generate these images, you should start with your raw resource in vector format and generate the images for each density using the following size scale:    xhdpi: 2.0    hdpi: 1.5    mdpi: 1.0 (baseline)    ldpi: 0.75This means that if you generate a 200x200 image for xhdpi devices, you should generate the same resource in 150x150 for hdpi, 100x100 for mdpi, and 75x75 for ldpi devices.Then, place the files in the appropriate drawable resource directory:MyProject/    res/        drawable-xhdpi/            awesomeimage.png        drawable-hdpi/            awesomeimage.png        drawable-mdpi/            awesomeimage.png        drawable-ldpi/            awesomeimage.pngAny time you reference @drawable/awesomeimage, the system selects the appropriate bitmap based on the screen's density.

我们在bitmap这个实体类图片时,要在drawable的不同dpi像素密度中放置不同大小的图片,转换的倍数关系在文档中有说明如上;
在Google官方开发文档中,说明了 mdpi:hdpi:xhdpi:xxhdpi:xxxhdpi=2:3:4:6:8 的尺寸比例进行缩放。例如,一个图标的大小为48×48dp,表示在mdpi上,实际大小为48×48px,在hdpi像素密度上,实际尺寸为mdpi上的1.5倍,即72×72px,以此类推。
这里写图片描述

这里写图片描述

Note: Low-density (ldpi) resources aren’t always necessary. When you provide hdpi assets, the system scales them down by one half to properly fit ldpi screens.

ldpi中的图片一般是不用的,如果用到,系统会默认选择hdpi的一半大小去适配

官方API提供的一些方法

Use “wrap_content” and “match_parent”//
Use RelativeLayout//使用相对布局
Use Size Qualifiers//针对不同布局把大小固定死
Use Nine-patch Bitmaps//使用点九图
使用dp sp不使用px//sp:Scale-Independent Pixels的缩写,可以根据文字大小首选项自动进行缩放。Google推荐我们使用12sp以上的大小,通常可以使用12sp,14sp,18sp,22sp,最好不要使用奇数和小数。

现在又有了百分比布局,谷歌官方提供的PercentRelativeLayout,直接设置控件占据的百分比大小,不用考虑适配的问题

<android.support.percent.PercentRelativeLayout         xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:app="http://schemas.android.com/apk/res-auto"         android:layout_width="match_parent"         android:layout_height="match_parent"/>     <ImageView         app:layout_widthPercent="50%"         app:layout_heightPercent="50%"         app:layout_marginTopPercent="25%"         app:layout_marginLeftPercent="25%"/> </android.support.percent.PercentFrameLayout/>
2 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 高中的孩子不好好上学怎么办 和老公消费观念不合拍怎么办 去医院没带现金怎么办 微信读书下架了怎么办 24岁血压有点高怎么办 吃鸡鼠标弹出来怎么办 电脑分辨率调错了怎么办 猎豹sc9打不开门怎么办 苹果手机卡顿反应慢怎么办 金立手机卡顿反应慢怎么办 20天宝宝黄疸219怎么办? 智慧树选修挂科怎么办 军人被纠察抓了怎么办 我家的小孩很凶怎么办 10岁儿童老挤眼怎么办? 脚趾长鸡眼好痛怎么办 月加班超过36小时怎么办 月经老提前怎么办才好 20岁例假不规律怎么办 农村的医保门诊封顶了怎么办 保研夏令营被拒怎么办 小学生去英国游学怎么办签证 医生说我湿气重怎么办 喉咙痛有来医生感冒喉咙痛怎么办 暴马丁香叶子卷怎么办 房产权40年以后怎么办 面试回答不了考官的问题怎么办 轻伤事故对方要钱太多我怎么办 退休后有房子住公积金怎么办 江苏副高评过不聘怎么办 抗环瓜氨酸肽抗体高怎么办 脚被磨破了好痛怎么办 脚磨破皮肿了怎么办 脚磨破皮发炎了怎么办 3个月婴儿流口水怎么办 beats耳机被偷了怎么办 beats X耳机丢了怎么办 头戴耳机戴着热怎么办 长时间戴耳机耳朵疼怎么办 手表秒针走得声音很大怎么办 顾客总找老板剪头发怎么办