这个程序是生成dimen文件夹进行不同屏幕的适配的,请看

XMLGenerator.Java

[java] view plain copy
  1. import java.io.File;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.FileOutputStream;  
  4. import java.io.PrintWriter;  
  5.   
  6. public class XMLGenerator {  
  7.     //将来生成values文件的目录  
  8.     private final static String rootPath = "D:\\layoutroot\\values-{0}x{1}\\";  
  9.   
  10.     //以480x320分辨率的x1作为1px  
  11.     //<dimen name="x1">1.0px</dimen>  
  12.     private final static float dw = 320f;  
  13.     private final static float dh = 480f;  
  14.   
  15.     private final static String WTemplate = "<dimen name=\"x{0}\">{1}px</dimen>\n";  
  16.     private final static String HTemplate = "<dimen name=\"y{0}\">{1}px</dimen>\n";  
  17.   
  18.     public static void main(String[] args) {  
  19.         //生成常用的几种分辨率  
  20.         makeString(280280);  
  21.         makeString(320320);  
  22.         makeString(320480);  
  23.         makeString(480800);  
  24.         makeString(480854);  
  25.         makeString(540960);  
  26.         makeString(6001024);  
  27.         makeString(7201184);  
  28.         makeString(7201196);  
  29.         makeString(7201280);  
  30.         makeString(7681024);  
  31.         makeString(7681280);  
  32.         makeString(8001280);  
  33.         makeString(10801812);  
  34.         makeString(10801920);  
  35.         makeString(12001920);  
  36.         makeString(14402560);  
  37.         makeString(20481536);  
  38.         makeString(20601600);  
  39.         makeString(25601600);  
  40.     }  
  41.   
  42.     public static void makeString(int w, int h) {  
  43.   
  44.         StringBuffer sb = new StringBuffer();  
  45.         sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");  
  46.         sb.append("<resources>");  
  47.         float cellw = w / dw;  
  48.         for (int i = 1; i < 320; i++) {  
  49.             sb.append(WTemplate.replace("{0}", i + "").replace("{1}",  
  50.                     change(cellw * i) + ""));  
  51.         }  
  52.         sb.append(WTemplate.replace("{0}""320").replace("{1}", w + ""));  
  53.         sb.append("</resources>");  
  54.   
  55.         StringBuffer sb2 = new StringBuffer();  
  56.         sb2.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");  
  57.         sb2.append("<resources>");  
  58.         float cellh = h / dh;  
  59.         for (int i = 1; i < 480; i++) {  
  60.             sb2.append(HTemplate.replace("{0}", i + "").replace("{1}",  
  61.                     change(cellh * i) + ""));  
  62.         }  
  63.         sb2.append(HTemplate.replace("{0}""480").replace("{1}", h + ""));  
  64.         sb2.append("</resources>");  
  65.   
  66.         String path = rootPath.replace("{0}", h + "").replace("{1}", w + "");  
  67.         File rootFile = new File(path);  
  68.         if (!rootFile.exists()) {  
  69.             rootFile.mkdirs();  
  70.         }  
  71.         File layxFile = new File(path + "lay_x.xml");  
  72.         File layyFile = new File(path + "lay_y.xml");  
  73.         try {  
  74.             PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile));  
  75.             pw.print(sb.toString());  
  76.             pw.close();  
  77.             pw = new PrintWriter(new FileOutputStream(layyFile));  
  78.             pw.print(sb2.toString());  
  79.             pw.close();  
  80.         } catch (FileNotFoundException e) {  
  81.             e.printStackTrace();  
  82.         }  
  83.   
  84.     }  
  85.   
  86.     public static float change(float a) {  
  87.         int temp = (int) (a * 100);  
  88.         return temp / 100f;  
  89.     }  
  90. }  

怎么样,一键生成的感觉是不是很爽啊,我们生成的文件在刚开始设置的rootPath目录里。 
这是我生成的截图:

文件目录在这个位置: 
几行代码帮你搞定屏幕适配

打开layoutroot里边有你makeStrin*生的各种values文件: 
几行代码帮你搞定屏幕适配

打开其中一个布局,里边是以px为单位的各种demen: 
几行代码帮你搞定屏幕适配

代码使用:代码使用:

[java] view plain copy
  1. <TextView  
  2.      android:id="@+id/person_tel"  
  3.      android:layout_width="wrap_content"  
  4.      android:layout_height="wrap_content"  
  5.      android:layout_gravity="center_vertical"  
  6.      android:layout_marginBottom="@dimen/y5"  
  7.      android:layout_marginLeft="@dimen/x10"  
  8.      android:layout_marginTop="@dimen/y5"  
  9.      android:text="拨打客服电话" />  

注意: 
@dimen/x和@dimen/y要区分开,分别适用于x方向和y方向

如果你还想生成其他分辨率的values,不妨makeString(int w, int h)试试。

源码:链接:https://pan.baidu.com/s/1YRfXpnvqouCHsbfKAUxbMg 密码:psa3

分类:

技术点:

相关文章: