这个程序是生成dimen文件夹进行不同屏幕的适配的,请看
XMLGenerator.Java
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.PrintWriter;
- public class XMLGenerator {
- //将来生成values文件的目录
- private final static String rootPath = "D:\\layoutroot\\values-{0}x{1}\\";
- //以480x320分辨率的x1作为1px
- //<dimen name="x1">1.0px</dimen>
- private final static float dw = 320f;
- 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(280, 280);
- makeString(320, 320);
- makeString(320, 480);
- makeString(480, 800);
- makeString(480, 854);
- makeString(540, 960);
- makeString(600, 1024);
- makeString(720, 1184);
- makeString(720, 1196);
- makeString(720, 1280);
- makeString(768, 1024);
- makeString(768, 1280);
- makeString(800, 1280);
- makeString(1080, 1812);
- makeString(1080, 1920);
- makeString(1200, 1920);
- makeString(1440, 2560);
- makeString(2048, 1536);
- makeString(2060, 1600);
- makeString(2560, 1600);
- }
- public static void makeString(int w, int h) {
- StringBuffer sb = new StringBuffer();
- sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
- sb.append("<resources>");
- float cellw = w / dw;
- for (int i = 1; i < 320; i++) {
- sb.append(WTemplate.replace("{0}", i + "").replace("{1}",
- change(cellw * i) + ""));
- }
- sb.append(WTemplate.replace("{0}", "320").replace("{1}", w + ""));
- sb.append("</resources>");
- StringBuffer sb2 = new StringBuffer();
- sb2.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
- sb2.append("<resources>");
- float cellh = h / dh;
- for (int i = 1; i < 480; i++) {
- sb2.append(HTemplate.replace("{0}", i + "").replace("{1}",
- change(cellh * i) + ""));
- }
- sb2.append(HTemplate.replace("{0}", "480").replace("{1}", h + ""));
- sb2.append("</resources>");
- String path = rootPath.replace("{0}", h + "").replace("{1}", w + "");
- 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(sb.toString());
- pw.close();
- pw = new PrintWriter(new FileOutputStream(layyFile));
- pw.print(sb2.toString());
- pw.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- public static float change(float a) {
- int temp = (int) (a * 100);
- return temp / 100f;
- }
- }
怎么样,一键生成的感觉是不是很爽啊,我们生成的文件在刚开始设置的rootPath目录里。
这是我生成的截图:
文件目录在这个位置:
打开layoutroot里边有你makeString产生的各种values文件:
打开其中一个布局,里边是以px为单位的各种demen:
- <TextView
- android:id="@+id/person_tel"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:layout_marginBottom="@dimen/y5"
- android:layout_marginLeft="@dimen/x10"
- android:layout_marginTop="@dimen/y5"
- android:text="拨打客服电话" />
注意:
@dimen/x和@dimen/y要区分开,分别适用于x方向和y方向
如果你还想生成其他分辨率的values,不妨makeString(int w, int h)试试。
源码:链接:https://pan.baidu.com/s/1YRfXpnvqouCHsbfKAUxbMg 密码:psa3