android:如何处理屏幕适配问题?
2016-04-19 16:29
597 查看
在安卓开发中,会经常遇到屏幕适配问题,下面总结一下,处理这问题的方法.解决方法:手机选择: 首先在项目开始时候,应该选择什么屏幕大小的手机,进行开发呢?用当前市场上主流屏幕的手机,比如 1280*720 项目后期:还需要用不同分辨率手机进行测试:比如:480*800 1920*1000图片适配: 做法:在工程目录:drawable目录中的不同目录,目录不同可以优先对应适配的手机 注释:480*800代表分辨率,括号中1.5代表屏幕密度通常情况下,把图片放在drawalbe-hdpi目录,如果每个目录都放一份图片,那样会造成整个应用会占用太多资源布局适配: 做法:不要绝对布局,多用相对布局或线性布局权重,用dp,不要px权重适配: 做法:是在线性布局中处理 android:weihtSum<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:weightSum="5"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:background="#0f0" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="3"android:background="#00f" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:background="#f0f" /></LinearLayout>效果:尺寸适配: dx和dp关系:dp = px/设备密度//设备密度,和手机分辨率有关Int density = getResouces().getDisplayMetris().density;问题:当遇到dp解决不了的问题,比如下面代码:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:layout_width="match_parent"android:layout_height="100dp"android:background="#00f" /><TextViewandroid:layout_width="320dp"android:layout_height="100dp"android:background="#0f0" /></LinearLayout>这代码在720*1280 和 320*480中界面显示如下: 解决方法:利用values目录下创建dimens.xml,这个目录定义了320*480的尺寸 创建values-720*1280目录,在这个目录也创建dimens.xml,这个文件定义了720*1280的尺寸values目录下的dimens.xml<resources><dimen name="textWith">160dp</dimen></resources>value-720*1280目录下的dimens.xml<resources><dimen name="textWith">320dp</dimen></resources>代码适配:Int width = getWindowManager().getDefaultDisplay().getWidth();Int height = getWindowManager().getDefaultDisplay().getHeight();TextView tv1 = (TextView)findViewById(R.id.tv1);TextView tv2 = (TextView)findViewById(R.id.tv2);LayoutParams params = new LayoutParams(width /3,height*0.2);tv1.setLayoutParams(params);tv2.setLayoutParams(params);
相关文章推荐
- 利用ViewFlipper实现实际应用中banner的自动滑动和手动滑动结合。
- Android 创建Library Project(库项目)与引用操作
- Android自定义ViewGroup:onMeasure与onLayout(1)
- Android加载SO库UnsatisfiedLinkError错误的原因及解决方案
- android自带拍照软件调用
- Android:View事件分发机制详解
- Android Intent实现页面之间跳转
- Android应用中仿今日头条App制作ViewPager指示器
- android 命令行的形式生成keystore,生成签名
- Android使用按钮实现ListView的上下分页
- Android的Touch事件处理
- Android 将布局文件转成图片(将View 转换成BitMap)
- android下单元测试
- 基于android的Socket通信
- Android知识梳理之ContentProvider内容提供者的使用
- Android简单自定义圆形和水平ProgressBar
- Android--Fragment基本介绍
- Android驱动入门系列(一)
- Android天气预报程序(六)
- Android M 动态权限获取