第一步:

res文件夹下,建立不同尺寸的valuse配置文件夹。并在不同的文件夹下建立不同的dimens.xml文件。valuse为默认的工程配置,其余的为根据不同的尺寸适配用户自己新建。

第二步:插入自动生成分辨率工具类   

DimenTool.java




import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * Created by cdy on 2016/2/3.
 * 快速生成适配工具类
 */
public class DimenTool {


    public static void gen() {
        //以此文件夹下的dimens.xml文件内容为初始值参照
        File file = new File("./app/src/main/res/values/dimens.xml");


        BufferedReader reader = null;
        StringBuilder sw240 = new StringBuilder();
        StringBuilder sw480 = new StringBuilder();
        StringBuilder sw600 = new StringBuilder();


        StringBuilder sw720 = new StringBuilder();


        StringBuilder sw800 = new StringBuilder();


        StringBuilder w820 = new StringBuilder();


        try {


            System.out.println("生成不同分辨率:");


            reader = new BufferedReader(new FileReader(file));


            String tempString;


            int line = 1;


            // 一次读入一行,直到读入null为文件结束


            while ((tempString = reader.readLine()) != null) {




                if (tempString.contains("")) {


                    //tempString = tempString.replaceAll(" ", "");


                    String start = tempString.substring(0, tempString.indexOf(">") + 1);


                    String end = tempString.substring(tempString.lastIndexOf("<") - 2);
                    //截取标签内的内容,从>右括号开始,到左括号减2,取得配置的数字
                    Double num = Double.parseDouble
                            (tempString.substring(tempString.indexOf(">") + 1, 
                                    tempString.indexOf("") - 2));


                    //根据不同的尺寸,计算新的值,拼接新的字符串,并且结尾处换行。
                    sw240.append(start).append( num * 0.75).append(end).append("\r\n");


                    sw480.append(start).append(num * 1.5).append(end).append("\r\n");


                    sw600.append(start).append(num * 1.87).append(end).append("\r\n");


                    sw720.append(start).append(num * 2.25).append(end).append("\r\n");


                    sw800.append(start).append(num * 2.5).append(end).append("\r\n");


                    w820.append(start).append(num * 2.56).append(end).append("\r\n");






                } else {
                    sw240.append(tempString).append("");


                    sw480.append(tempString).append("");


                    sw600.append(tempString).append("");


                    sw720.append(tempString).append("");


                    sw800.append(tempString).append("");


                    w820.append(tempString).append("");


                }


                line++;


            }


            reader.close();
            System.out.println("");


            System.out.println(sw240);


            System.out.println("");


            System.out.println(sw480);


            System.out.println("");


            System.out.println(sw600);


            System.out.println("");


            System.out.println(sw720);


            System.out.println("");


            System.out.println(sw800);


            String sw240file = "./app/src/main/res/values-sw240dp-land/dimens.xml";


            String sw480file = "./app/src/main/res/values-sw480dp-land/dimens.xml";


            String sw600file = "./app/src/main/res/values-sw600dp-land/dimens.xml";


            String sw720file = "./app/src/main/res/values-sw720dp-land/dimens.xml";


            String sw800file = "./app/src/main/res/values-sw800dp-land/dimens.xml";


            String w820file = "./app/src/main/res/values-w820dp/dimens.xml";
            //将新的内容,写入到指定的文件中去
            writeFile(sw240file, sw240.toString());


            writeFile(sw480file, sw480.toString());


            writeFile(sw600file, sw600.toString());


            writeFile(sw720file, sw720.toString());


            writeFile(sw800file, sw800.toString());


            writeFile(w820file, w820.toString());


        } catch (IOException e) {


            e.printStackTrace();


        } finally {


            if (reader != null) {


                try {


                    reader.close();


                } catch (IOException e1) {


                    e1.printStackTrace();


                }


            }


        }


    }




    /**
     * 写入方法
     *
     */


    public static void writeFile(String file, String text) {


        PrintWriter out = null;


        try {


            out = new PrintWriter(new BufferedWriter(new FileWriter(file)));


            out.println(text);


        } catch (IOException e) {


            e.printStackTrace();


        }






        out.close();


    }
    public static void main(String[] args) {


        gen();


    }


}


第三步: 配置初始默认的dimens.xml文件,然后运行工具类,就自动生成适应各种分辨率的xml文件了。

转载网址:http://blog.csdn.net/hnzcdy/article/details/50628993

更多相关文章

  1. 使用FileProvider共享文件
  2. react-native修改android包名
  3. Android中文联系人排序及检索补丁的原理(090819更新)
  4. 通过Android(安卓)Studio 导出Jar包
  5. android内存泄露 mat
  6. Application Resources
  7. 『原』在Linux下反编译Android(安卓).apk文件 使用apktool dex2j
  8. Android(安卓)查看蓝牙日志
  9. android 的反编译与防反编译

随机推荐

  1. android的logcat详细用法
  2. Android进程与内存及内存泄露
  3. AsyncTask 很好
  4. android:layout_weight让layout自动调整
  5. Android消息处理机制:源码剖析Handler、Lo
  6. Android华为推送低版本兼容问题
  7. 【Android】EventBus 3.0 源码分析
  8. Linux 下Android 开发环境搭建 ---CentOS
  9. Linux 学习 step by step (1)
  10. 【Android中Broadcast Receiver组件具体