public class Main {
        //定义文件本地存储路径,可按照需求更改
        private final static String rootPath = "D:\\workfiles\\layoutroot\\values-{0}x{1}";
    
        /** 横轴方向分为320份 */
        private final static float dw = 320f;
        /** 纵轴方向分为480份 */
        private final static float dh = 480f;
    
        /** 宽度的模板 */
        private final static String WTemplate = "<dimen name=\"x{0}\">{1}px</dimen>\n";
        /** 高度的模板 */
        private final static String HTemplate = "<dimen name=\"y{0}\">{1}px</dimen>\n";
    
        public static void main(String[] args) {
            //就适配了三个分辨率的手机屏幕
            makeString(320, 480);
            makeString(720, 1280);
            makeString(1080, 1920);
        }
        
        /**
         * 生成对应分辨率的dimen文件
         * 
         * @param width
         *            手机屏幕x方向的像素数量
         * @param height
         *            手机屏幕y方向的像素数量
         */
        private static void makeString(int width, int height) {
            StringBuffer sbWidth = new StringBuffer();
            sbWidth.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            sbWidth.append("<resources>");
            float cellw = width / dw;
            for (int i = 0; i < dw; i++) {
                sbWidth.append(WTemplate.replace("{0}", i + "").replace("{1}",
                        change(cellw * i) + ""));
            }
            sbWidth.append(WTemplate.replace("{0}", "320").replace("{1}",
                    width + ""));
            sbWidth.append("</resources>");
    
            StringBuffer sbHeight = new StringBuffer();
            sbHeight.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            sbHeight.append("<resources>");
            float cellh = height / dh;
            for (int i = 0; i < dh; i++) {
                sbHeight.append(HTemplate.replace("{0}", i + "").replace("{1}",
                        change(cellh * i) + ""));
            }
            sbHeight.append(HTemplate.replace("{0}", "480").replace("{1}",
                    height + ""));
            sbHeight.append("</resources>");
    
            String path = rootPath.replace("{0}", height + "").replace("{1}",
                    width + "");
            File rootFile = new File(path);
            if (!rootFile.exists()) {
                rootFile.mkdirs();
            }
    
            File layxFile = new File(path + "\\lay_x.xml");
            File layyFile = new File(path + "\\lay_y.xml");
    
            try {
                PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile));
                pw.print(sbWidth.toString());
                pw.close();
                pw = new PrintWriter(new FileOutputStream(layyFile));
                pw.print(sbHeight.toString());
                pw.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        /**
         * 将浮点数小数点后保留两位小数
         */
        private static float change(float a) {
            int temp = (int) (a * 100);
            return temp / 100f;
        }

}

将生成的values文件复制到工程的res下,

使用用例:
android:layout_width="@dimen/x160"

最新文章

  1. Kafka设计解析(一)- Kafka背景及架构介绍
  2. 使用Apache Ambari管理Hadoop
  3. Java并发之CyclicBarrier 可重用同步工具类
  4. 【131031】rel 属性 -- link标签中的rel属性,定义了文档与链接的关系
  5. 8.3 H5学习笔记
  6. 利用appscan进行自动化定期安全测试
  7. PostgreSQL/bin
  8. C中的基本数据类型和变量
  9. 14.5.5.2 Deadlock Detection and Rollback Deadlock 检测和回滚
  10. Selenium2+Python自动化测试实战
  11. openssl ans.1编码规则分析及证书密钥编码方式
  12. 老李分享:Android -自动化埋点 2
  13. linux系统命令学习系列-用户切换命令su,sudo
  14. python-复杂生成式
  15. linux下svn(subversion)服务端添加工程及配置权限
  16. 如何把if-else代码重构成高质量代码
  17. [PHP] 数据结构-反转链表PHP实现
  18. E. Binary Numbers AND Sum
  19. 部分手机(如三星)的Listview列表会自动加上黑线解决办法
  20. expect命令自动登录ssh

热门文章

  1. 华为oj - 统计大写字母个数
  2. 基于webrtc的多人视频会话的demo运行程序
  3. 引用 exit、return、_exit、_Exit这几个函数的区别
  4. js修改window对象中的url历史记录
  5. php的一些小笔记-文件函数(1)
  6. win8(64位)下memcache安装时报错“ failed to install service or service already installed” 与安装
  7. Unity3D中使用KiiCloud总结一
  8. java设计模式--行为型模式--模板方法
  9. 制作安装包工具NSIS
  10. 2014.11.12模拟赛【最小公倍数】| vijos1047最小公倍数