设为首页 加入收藏

TOP

Android屏幕适配讲解与实战(原创)(三)
2019-09-01 23:12:47 】 浏览:63
Tags:Android 屏幕 适配 讲解 实战 原创
ate.replace("{0}", baseH + "").replace("{1}", h + "")); sbForHeight.append("</resources>"); File fileDir = new File(dirStr + File.separator + VALUE_TEMPLATE.replace("{0}", h + "")// .replace("{1}", w + "")); fileDir.mkdir(); File layxFile = new File(fileDir.getAbsolutePath(), "lay_x.xml"); File layyFile = new File(fileDir.getAbsolutePath(), "lay_y.xml"); try { PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile)); pw.print(sbForWidth.toString()); pw.close(); pw = new PrintWriter(new FileOutputStream(layyFile)); pw.print(sbForHeight.toString()); pw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } public static float change(float a) { int temp = (int) (a * 100); return temp / 100f; } public static void main(String[] args) { //基准大小,比如320.480,其他则以这个基准进行放大缩小 int baseW = 320; int baseH = 480; String addition = ""; try { if (args.length >= 3) { baseW = Integer.parseInt(args[0]); baseH = Integer.parseInt(args[1]); addition = args[2]; } else if (args.length >= 2) { baseW = Integer.parseInt(args[0]); baseH = Integer.parseInt(args[1]); } else if (args.length >= 1) { addition = args[0]; } } catch (NumberFormatException e) { System.err .println("right input params : java -jar xxx.jar width height w,h_w,h_..._w,h;"); e.printStackTrace(); System.exit(-1); } new Generateva lueFiles(baseW, baseH, addition).generate(); } }

运行代码,结果会在项目的res文件夹中生成对应的内容,如下图所示:

 
 

温馨提示:上图每个文件夹是以320*480作为基准进行放大缩小后的px值

将上面生成的文件夹复制到实际项目中
复制所有文件夹,右击studio中的res文件夹进行粘贴

 

xml布局中进行引用

activity_main4.xml代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:layout_width="@dimen/x200" android:layout_height="@dimen/y30" android:text="按钮1"/> <Button android:layout_width="@dimen/x200" android:layout_height="@dimen/y30" android:layout_marginTop="@dimen/y30" android:text="按钮2"/> </LinearLayout> 

运行结果如下图所示:

 

温馨提示:

  1. 如果运行后发现某个尺寸的屏幕没有是配到,那么可以在生成器中添加对应屏幕尺寸,重新生成文件夹,之后拷贝到项目中即可
  2. 图片适配则可以采用图片放在不同文件夹里面,系统会自动选图,但最好每次切图都包含多种分辨率的图片,例如某一测试的机器是xxhdpi密度的,所以当把图片放在xxhdpi时候(其他文件夹没放),加载时候占用内存是较小的。默认是没有加载任何东西(运行一个空白app)占用的内存。如果加载其他像素下的图片,则会占用很多内容。所以总结来说就是要各个文件夹都放有图片,可以减少占用内存的加载。
 

四、项目源码下载

链接:https://pan.baidu.com/s/1xDRVaSS9Kk9OGLzloBeneA
提取码:0zyt

 
首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android 里的adb命令 下一篇Android路由框架-ARouter详解(非..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目