├── .classpath ├── .gitignore ├── .project ├── .settings └── org.eclipse.jdt.core.prefs ├── LICENSE ├── README.MD ├── pom.xml ├── screen ├── screen_xmltocode_findviewbyid.png ├── screen_xmltocode_flutter.png ├── screen_xmltocode_java.png └── screen_xmltocode_swift.png └── src └── main └── java ├── XmlParserMain.java └── com └── xl ├── game └── math │ └── Str.java ├── util ├── ClipBoard.java ├── DomParser.java ├── FileUtils.java ├── FindViewByIdParser.java ├── FlutterDomParser.java ├── SwiftDomParser.java ├── UIUtil.java ├── XmlUtil.java └── flutter │ ├── AppbarLayout.java │ ├── FlutterWidget.java │ ├── FrameLayout.java │ ├── LinearLayout.java │ ├── RelativeLayout.java │ ├── TextView.java │ ├── Toolbar.java │ └── Widget.java └── window ├── TextWindow.java ├── Toast.java └── XmlToCodeWindow.java /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | !**/src/main/**/target/ 4 | !**/src/test/**/target/ 5 | 6 | ### IntelliJ IDEA ### 7 | .idea/modules.xml 8 | .idea/jarRepositories.xml 9 | .idea/compiler.xml 10 | .idea/libraries/ 11 | *.iws 12 | *.iml 13 | *.ipr 14 | 15 | ### Eclipse ### 16 | .apt_generated 17 | .classpath 18 | .factorypath 19 | .project 20 | .settings 21 | .springBeans 22 | .sts4-cache 23 | 24 | ### NetBeans ### 25 | /nbproject/private/ 26 | /nbbuild/ 27 | /dist/ 28 | /nbdist/ 29 | /.nb-gradle/ 30 | build/ 31 | !**/src/main/**/build/ 32 | !**/src/test/**/build/ 33 | 34 | ### VS Code ### 35 | .vscode/ 36 | 37 | ### Mac OS ### 38 | .DS_Store 39 | 40 | /bin 41 | /.gradle 42 | /gradle 43 | /local.properties 44 | /.idea/caches/build_file_checksums.ser 45 | /.idea/libraries 46 | /.idea/modules.xml 47 | /.idea/workspace.xml 48 | /.idea 49 | .DS_Store 50 | /build 51 | /captures 52 | .externalNativeBuild 53 | *.apk 54 | *.dex 55 | /MyApplication 56 | /mylibrary 57 | 58 | *.iml 59 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | XmlToCode 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | 19 | 1697519940918 20 | 21 | 30 22 | 23 | org.eclipse.core.resources.regexFilterMatcher 24 | node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This software is provided under the MIT license. 2 | Alessandro Crugnola - alessandro.crugnola@gmail.com 3 | 4 | The MIT License (MIT) 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 9 | 10 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | # XmlToCode 2 | 3 | 将安卓xml转换成代码,此项目仅为测试项目,还不够完善,有什么建议或意见欢迎提出。 4 | 5 | > 注:本项目仅支持部分xml属性及安卓控件转换 6 | 7 | 目前已初步实现xml布局转安卓java/swift/flutter代码 8 | 9 | ### xml转findViewById代码 10 | 11 | ![image-findviewbyid](screen/screen_xmltocode_findviewbyid.png) 12 | 13 | #### xml转java 14 | 15 | ![image-java](screen/screen_xmltocode_java.png) 16 | 17 | 18 | 19 | #### xml转iOS代码(swift) 20 | 21 | ![image-swift](screen/screen_xmltocode_swift.png) 22 | 23 | 24 | 25 | #### xml转flutter 26 | 27 | ![image-flutter](screen/screen_xmltocode_flutter.png) 28 | 29 | 30 | 31 | ### jar下载 32 | 33 | https://github.com/fengdeyingzi/XmlToCode/releases/download/1.0/XmlToCode.jar 34 | 35 | 36 | 37 | #### 相关资料 38 | 39 | xmlflutter:https://github.com/fengdeyingzi/xmlflutter -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.example 8 | XmlToCode 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 8 13 | 8 14 | UTF-8 15 | 16 | 17 | -------------------------------------------------------------------------------- /screen/screen_xmltocode_findviewbyid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengdeyingzi/XmlToCode/bcaf40d02f4ec81bc21cdfc6699ee4bf55f040cb/screen/screen_xmltocode_findviewbyid.png -------------------------------------------------------------------------------- /screen/screen_xmltocode_flutter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengdeyingzi/XmlToCode/bcaf40d02f4ec81bc21cdfc6699ee4bf55f040cb/screen/screen_xmltocode_flutter.png -------------------------------------------------------------------------------- /screen/screen_xmltocode_java.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengdeyingzi/XmlToCode/bcaf40d02f4ec81bc21cdfc6699ee4bf55f040cb/screen/screen_xmltocode_java.png -------------------------------------------------------------------------------- /screen/screen_xmltocode_swift.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengdeyingzi/XmlToCode/bcaf40d02f4ec81bc21cdfc6699ee4bf55f040cb/screen/screen_xmltocode_swift.png -------------------------------------------------------------------------------- /src/main/java/XmlParserMain.java: -------------------------------------------------------------------------------- 1 | import com.xl.util.DomParser; 2 | import com.xl.util.SwiftDomParser; 3 | import com.xl.util.UIUtil; 4 | import com.xl.window.XmlToCodeWindow; 5 | 6 | public class XmlParserMain { 7 | public static void main(String[] args) { 8 | SwiftDomParser parser = new SwiftDomParser(); 9 | parser.parseSwift(); 10 | UIUtil.setWindowsStyle(); 11 | XmlToCodeWindow window = new XmlToCodeWindow(); 12 | window.show(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/xl/game/math/Str.java: -------------------------------------------------------------------------------- 1 | package com.xl.game.math; 2 | import java.util.*; 3 | import java.io.*; 4 | 5 | 6 | /* 7 | 8 | 字符处理 9 | */ 10 | 11 | 12 | public class Str 13 | { 14 | //检测gbk编码 15 | /* 16 | GBK应该是更简单的,因为汉字的GBK编码也是把一个汉字用两个字节来表示,其首字节对应0x81-0xFE(即129-224), 17 | 尾字节对应除掉(0x7F)的0x40-oxFE(即64-126和128-224)。 18 | */ 19 | 20 | public static boolean isGBK(byte[] buf,int size) 21 | { 22 | boolean isGBK=true; 23 | int start = 0; 24 | int end = size; 25 | int c=0; 26 | int c2=0; 27 | int type=0; 28 | StringBuffer bufer=new StringBuffer(); 29 | while (start < end) 30 | { 31 | c = buf[start]&0xff; 32 | bufer.append(c); 33 | switch(type) 34 | { 35 | case 0: 36 | if(c>=0x81 && c<=0xfe) 37 | { 38 | type=1; 39 | } 40 | else if(c<128) 41 | { 42 | 43 | } 44 | else 45 | { 46 | isGBK=false; 47 | break; 48 | } 49 | break; 50 | case 1: 51 | if(c!=0x7f && c>=0x40 && c<=(0xfe)) 52 | { 53 | type=0; 54 | } 55 | else 56 | { 57 | isGBK=false; 58 | break; 59 | } 60 | } 61 | 62 | 63 | start++; 64 | } 65 | // Log.e("isGb",""+start); 66 | return isGBK; 67 | } 68 | 69 | /* 70 | a0 = 71 | a0+ = 72 | */ 73 | public static boolean isGB2312(byte[] buf,int size) 74 | { 75 | boolean isGBK=true; 76 | int start = 0; 77 | int end = size; 78 | int c=0; 79 | int c2=0; 80 | int type=0; 81 | StringBuffer bufer=new StringBuffer(); 82 | GBWHILE: 83 | while (start < end) 84 | { 85 | c = buf[start]&0xff; 86 | bufer.append(c); 87 | switch(type) 88 | { 89 | case 0: 90 | if(c>=0xa1 && c<=0xf7) 91 | { 92 | type=1; 93 | } 94 | else if(c<128) 95 | { 96 | 97 | } 98 | else 99 | { 100 | isGBK=false; 101 | break GBWHILE; 102 | } 103 | break; 104 | case 1: 105 | if(c>=0xa1 && c<=(0xa0+94)) 106 | { 107 | type=0; 108 | } 109 | else 110 | { 111 | isGBK=false; 112 | break GBWHILE; 113 | } 114 | } 115 | 116 | 117 | start++; 118 | } 119 | // Log.e("isGb",""+start); 120 | return isGBK; 121 | } 122 | 123 | //检测是否为UTF-8编码 124 | public static boolean isUTF8(byte[] pBuffer, int size) 125 | { 126 | boolean IsUTF8 = true; 127 | int start = 0; 128 | int end = size; 129 | int c=0; 130 | while (start < end) 131 | { 132 | c = pBuffer[start]&0xff; 133 | if(c==0xf0) //emoj表情 f09f 134 | { 135 | start+=4; 136 | } 137 | else if (c < 0x80) // (10000000): 值小于0x80的为ASCII字符 138 | { 139 | start++; 140 | } 141 | else if (c < (0xC0)) // (11000000): 值介于0x80与0xC0之间的为无效UTF-8字符 142 | { 143 | IsUTF8 = false; 144 | break; 145 | } 146 | 147 | else if (c < (0xE0)) // (11100000): 此范围内为2字节UTF-8字符 148 | { 149 | if (start >= end - 1) 150 | break; 151 | if ((pBuffer[start+1] & (0xC0)) != 0x80) 152 | { 153 | IsUTF8 = false; 154 | break; 155 | } 156 | start += 2; 157 | } 158 | else if (c < (0xF0)) // (11110000): 此范围内为3字节UTF-8字符 159 | { 160 | if (start >= end - 2) 161 | break; 162 | if ((pBuffer[start+1] & (0xC0)) != 0x80 || (pBuffer[start+2] & (0xC0)) != 0x80) 163 | { 164 | IsUTF8 = false; 165 | break; 166 | } 167 | start += 3; 168 | } 169 | else 170 | { 171 | IsUTF8 = false; 172 | break; 173 | } 174 | } 175 | return IsUTF8; 176 | } 177 | 178 | //检测ascii编码 179 | private boolean isAscii(byte[] buf,int len) 180 | { 181 | if(len>buf.length)len=buf.length; 182 | for(int i=0;i=128) 185 | { 186 | return false; 187 | } 188 | } 189 | return true; 190 | } 191 | 192 | //把一个字符串中的大写转为小写 193 | public static String toLower(String str) 194 | { 195 | StringBuffer sb = new StringBuffer(200); 196 | if (str != null) 197 | { 198 | for (int i=0;i < str.length();i++) 199 | { 200 | char c = str.charAt(i); 201 | if (Character.isUpperCase(c))//判断是否为大写 202 | { 203 | sb.append(Character.toLowerCase(c)); 204 | } 205 | else 206 | { 207 | sb.append(c); 208 | } 209 | } 210 | } 211 | 212 | return sb.toString(); 213 | } 214 | 215 | //把字符串中的小写转换为大写 216 | public static String toUpper(String str) 217 | { 218 | StringBuffer sb = new StringBuffer(200); 219 | if (str != null) 220 | { 221 | for (int i=0;i < str.length();i++) 222 | { 223 | char c = str.charAt(i); 224 | if (Character.isLowerCase(c))//判断是否为小写 225 | { 226 | sb.append(Character.toUpperCase(c)); 227 | } 228 | else 229 | { 230 | sb.append(c); 231 | } 232 | } 233 | } 234 | 235 | return sb.toString(); 236 | } 237 | 238 | //检测是否为中文 239 | public static boolean checkCh(String text) 240 | { 241 | if(text.getBytes().length==text.length())//这句就是来判断 String是否含有中文字符。 242 | { 243 | return false; 244 | } 245 | else 246 | { 247 | return true; 248 | } 249 | } 250 | //处理参数列表,生成int数组 251 | public static void atoiEx(char[] text,int ptr,int[] per) 252 | { 253 | 254 | int i =0; 255 | int math = 0; 256 | for(;ptr'9' || text.charAt(ptr)<'0') 353 | { 354 | ptr++; 355 | } 356 | */ 357 | while(ptr=0) 468 | { 469 | if(text[ptr]==c) 470 | { 471 | return ptr; 472 | } 473 | ptr--; 474 | } 475 | 476 | return -1 ; 477 | } 478 | 479 | public static int strrchr(String text,char c) 480 | { 481 | int ptr=text.length() -1; 482 | while(ptr>=0) 483 | { 484 | if(text.charAt(ptr)==c) 485 | { 486 | return ptr; 487 | } 488 | ptr--; 489 | } 490 | 491 | return -1 ; 492 | } 493 | //将text2以0结尾的内容复制到text 494 | void strcpy(char text[],int ptr,char text2[],int ptr2) 495 | { 496 | int i=0; 497 | while(itext.length-ptr) 552 | {return false;} 553 | while(itext.length()-ptr) 573 | {return false;} 574 | while(i getArray(ArrayList array) 625 | { 626 | ArrayList list = new ArrayList(); 627 | StringSorter fileSorter=new StringSorter(); 628 | TreeSet treeSet = new TreeSet(fileSorter); 629 | 630 | 631 | for (int i = 0; i < array.size(); ++i) 632 | { 633 | treeSet.add(array.get(i)); 634 | //Logcat.e(arrfile[i].getPath()); 635 | } 636 | Iterator iterator = treeSet.iterator(); 637 | while (iterator.hasNext()) 638 | { 639 | //String item = (String)iterator.next(); 640 | list.add((String)iterator.next()); 641 | } 642 | return list; 643 | } 644 | 645 | 646 | static class StringSorter 647 | implements Comparator 648 | { 649 | 650 | public StringSorter() 651 | { 652 | } 653 | 654 | //gbk编码比较 655 | public int compare(String str1,String str2){ 656 | try { 657 | byte[] b1 = str1.getBytes("GBK"); 658 | byte[] b2 = str2.getBytes("GBK"); 659 | int l1=b1.length; 660 | int l2=b2.length; 661 | int l=Math.min(l1, l2); 662 | int k=0; 663 | while(kstr2[i]) 698 | { 699 | num=1; break; 700 | } 701 | else 702 | continue; 703 | } 704 | if(num==0) 705 | if(len1 array) 725 | { 726 | //arraylist转数组 727 | String[] texts = new String[array.size()]; 728 | for(int n=0;n ArrayToArrayList(String[] array){ 737 | ArrayList list= new ArrayList(); 738 | for(int i=0;i\n" 34 | +"\n" 38 | +" \n" 42 | +" \n" 45 | +" \n" 52 | +" \n" 57 | +" \n" 62 | +" \n" 69 | +"\n" 70 | +" \n" 71 | +"\n" 72 | +" \n" 76 | +"\n" 77 | +"\n" 78 | +" \n" 79 | +"\n" 80 | +" \n" 92 | +" \n" 100 | +"\n" 101 | +" \n" 102 | +"\n" 103 | +"\n" 104 | +"\n" 105 | +" \n" 106 | +" \n" 110 | +"\n" 111 | +" \n" 116 | +"\n" 117 | +" \n" 118 | +"\n" 119 | +" \n" 126 | +"\n" 127 | +" \n" 134 | +"\n" 135 | +" \n" 140 | +"\n" 141 | +" \n" 148 | +"\n" 149 | +" \n" 150 | +"\n" 151 | +" \n" 159 | +"\n" 160 | +" \n" 165 | +"\n" 166 | +" \n" 173 | +"\n" 174 | +" \n" 175 | +"\n" 176 | +" \n" 177 | +" \n" 178 | +"\n" 179 | +"\n"; 180 | 181 | 182 | 183 | public DomParser(){ 184 | 185 | } 186 | 187 | public void setXmlText(String text){ 188 | this.text = text; 189 | } 190 | 191 | 192 | public void parseJava() { 193 | DocumentBuilderFactory factory = null; 194 | DocumentBuilder builder = null; 195 | Document document = null; 196 | InputStream inputStream = null; 197 | factory = DocumentBuilderFactory.newInstance(); 198 | try { 199 | // 惯例 取得document文件实例的过程 200 | builder = factory.newDocumentBuilder(); 201 | inputStream = new ByteArrayInputStream(text.getBytes("UTF-8")); 202 | ;// 以工程文件下assets文件夹为根目录 203 | document = builder.parse(inputStream); 204 | 205 | // 取得根Element 以此列出所有节点NodeList 206 | Element root = document.getDocumentElement(); 207 | if (root instanceof Node) { 208 | System.out.println("========"); 209 | setNodesId(root); 210 | // printfNodes(root); 211 | String layout_root = "layout_root"; 212 | String className = root.getNodeName(); 213 | buf_code = new StringBuffer(); 214 | buf_code.append(" Context context = this;\n"); 215 | buf_code.append(" "+className+" "+layout_root+" = "+"new "+className+"(context);\n"); 216 | printAndroidCode(className, layout_root, root); 217 | buf_code.append(" setContentView("+layout_root+");\n"); 218 | return; 219 | } 220 | 221 | 222 | } catch (Exception e) { 223 | // TODO Auto-generated catch block 224 | e.printStackTrace(); 225 | } 226 | } 227 | 228 | 229 | public void parseSwift() { 230 | DocumentBuilderFactory factory = null; 231 | DocumentBuilder builder = null; 232 | Document document = null; 233 | InputStream inputStream = null; 234 | factory = DocumentBuilderFactory.newInstance(); 235 | try { 236 | // 惯例 取得document文件实例的过程 237 | builder = factory.newDocumentBuilder(); 238 | inputStream = new ByteArrayInputStream(text.getBytes()); 239 | ;// 以工程文件下assets文件夹为根目录 240 | document = builder.parse(inputStream); 241 | 242 | // 取得根Element 以此列出所有节点NodeList 243 | Element root = document.getDocumentElement(); 244 | if (root instanceof Node) { 245 | System.out.println("========"); 246 | setNodesId(root); 247 | // printfNodes(root); 248 | String layout_root = "layout_root"; 249 | String className = root.getNodeName(); 250 | printiOSCode(className, layout_root, root); 251 | return; 252 | } 253 | 254 | 255 | } catch (Exception e) { 256 | // TODO Auto-generated catch block 257 | e.printStackTrace(); 258 | } 259 | } 260 | 261 | 262 | int count = 0; 263 | 264 | 265 | 266 | // 遍历node设置id 267 | void setNodesId(Node node) { 268 | NodeList nodelist = node.getChildNodes(); 269 | // System.out.println("node name=" + node.getNodeName() + " value=" + 270 | // node.getNodeValue() + " type=" + node.getNodeType()); 271 | if (node.getNodeType() == 1) { 272 | printfAttributes(node); 273 | NamedNodeMap map = node.getAttributes(); 274 | 275 | // node.setNamedItem(nodetag); 276 | if (node instanceof Element) { 277 | System.out.println("is Element"); 278 | Element element = (Element) node; 279 | String id = node.getNodeName(); 280 | if(id.indexOf('.')>0){ 281 | id = id.substring(Str.strrchr(id, '.')+1); 282 | } 283 | String view_name = id.toLowerCase() + "_" + count; 284 | String key_id = element.getAttribute("android:id"); 285 | if(key_id.length()!=0){ 286 | if(key_id.startsWith("@id/") || key_id.startsWith("@+id/")){ 287 | view_name = key_id.substring(key_id.indexOf('/')+1); 288 | } 289 | } 290 | ((Element) node).setAttribute("id", id.toLowerCase() + "_" + count); 291 | element.setAttribute("name", view_name); 292 | ((Element) node).setAttribute("layoutparams", "layoutParams_" + count); 293 | count++; 294 | } 295 | } 296 | for (int n = 0; n < nodelist.getLength(); n++) { 297 | Node nodeitem = nodelist.item(n); 298 | setNodesId(nodeitem); 299 | } 300 | } 301 | 302 | // 递归输出nodes 303 | void printfNodes(Node node) { 304 | NodeList nodelist = node.getChildNodes(); 305 | System.out.println( 306 | "node name=" + node.getNodeName() + " value=" + node.getNodeValue() + " type=" + node.getNodeType()); 307 | if (node.getNodeType() == 1) { 308 | printfAttributes(node); 309 | } 310 | for (int n = 0; n < nodelist.getLength(); n++) { 311 | Node nodeitem = nodelist.item(n); 312 | System.out.println("node item = " + nodeitem.getNodeName() + " value=" + nodeitem.getNodeValue() + " type=" 313 | + nodeitem.getNodeType() + " "); 314 | printfNodes(nodeitem); 315 | } 316 | } 317 | 318 | // 输出一个node的所有标签 android:layout_width android:layout_height等 319 | void printfAttributes(Node node) { 320 | NamedNodeMap map = node.getAttributes(); 321 | for (int ii = 0; ii < map.getLength(); ii++) { 322 | System.out.println("name = " + map.item(ii).getNodeName() + " value=" + map.item(ii).getNodeValue() 323 | + " type=" + map.item(ii).getNodeType()); 324 | } 325 | } 326 | 327 | //获取color 328 | String getColor(String value){ 329 | if(value.startsWith("#")){ 330 | return XmlUtil.getColorHex(value); 331 | } 332 | else if(value.startsWith("@color/")){ 333 | return "R.color."+value.substring(7); 334 | } 335 | else if(value.startsWith("?attr/")){ 336 | return "ResouseUtil.getColorAttr(context, R.attr."+value.substring(6)+")"; 337 | } 338 | return value; 339 | } 340 | 341 | //获取theme名字 342 | String getTheme(String value){ 343 | String temp = value; 344 | StringBuffer buf = new StringBuffer(); 345 | if(value.startsWith("@style/")){ 346 | buf.append("R.style."); 347 | temp = value.substring(7); 348 | for(int i=0;i=0){ 511 | 512 | } 513 | else if(key.equals("android:layout_margin")){ 514 | } 515 | else if(key.equals("android:layout_marginTop")){ 516 | 517 | } 518 | else if(key.equals("android:layout_marginBottom")){ 519 | 520 | } 521 | else if(key.equals("android:layout_marginLeft")){ 522 | 523 | } 524 | else if(key.equals("android:layout_marginRight")){ 525 | 526 | } 527 | else if (key.equals("android:layout_height")) { 528 | 529 | } else if (key.equals("android:theme")) { 530 | 531 | buf_code.append(" " + layout_name + ".setTheme(" + getTheme(value) + ");\n"); 532 | } 533 | else if(key.equals("app:popupTheme")){ 534 | buf_code.append(" " + layout_name + ".setPopupTheme(" + getTheme(value) + ");\n"); 535 | } 536 | else if(key.equals("app:title")){ 537 | buf_code.append(" " + layout_name + ".setTitle(" + XmlUtil.getString(value)+");"); 538 | } 539 | else if(key.equals("app:subtitle")){ 540 | buf_code.append(" " + layout_name + ".setSubtitle(" + XmlUtil.getString(value)+");"); 541 | } 542 | else if(key.equals("app:titleTextColor")){ 543 | buf_code.append(" " + layout_name + ".setTitleTextColor(" + XmlUtil.getColorHex(value) + ");"); 544 | } 545 | else if(key.equals("app:subtitleTextColor")){ 546 | buf_code.append(" " + layout_name + ".setSubtitleTextColor(" + XmlUtil.getColorHex(value) + ");"); 547 | } 548 | else if (key.equals("android:background")) { 549 | if (value.startsWith("#")) { 550 | buf_code.append(" " + layout_name + ".setBackgroundColor(" + XmlUtil.getColorHex(value) + ");\n"); 551 | } else if (value.startsWith("@color/")) { 552 | value = "R.color."+value.substring(7); 553 | buf_code.append(" " + layout_name + ".setBackgroundColor(getResources().getColor(" + value + "));\n"); 554 | } else if (value.startsWith("@drawable/")) { 555 | value = "R.drawable."+value.substring(10); 556 | buf_code.append(" " + layout_name + ".setBackground(getResources().getDrawable(" + value + "));\n"); 557 | 558 | } 559 | else if(value.startsWith("@drawable/") || value.startsWith("@mipmap/")){ 560 | buf_code.append(" " + layout_name + ".setBackground(getResources().getDrawable(" + XmlUtil.getDrawable(value) + "));\n"); 561 | } 562 | else if(value.startsWith("?attr/")){ 563 | buf_code.append(" " + layout_name + ".setBackgroundColor(" + getColor(value) + ");\n"); 564 | } 565 | else 566 | System.out.println(" " + layout_name + ".setBackground(" + value + ");"); 567 | } else if (key.equals("android:orientation")) { 568 | if (value.equals("vertical")) { 569 | value = "LinearLayout.VERTICAL"; 570 | } else if (value.equals("horizontal")) { 571 | value = "LinearLayout.HORIZONTAL"; 572 | } 573 | buf_code.append(" " + layout_name + ".setOrientation(" + value + ");\n"); 574 | } else if (key.equals("android:src") || key.equals("app:srcCompat")) { 575 | value = XmlUtil.getDrawable(value); 576 | buf_code.append(" " + layout_name + ".setImageDrawable(getResources().getDrawable(" + value + "));\n"); 577 | } else if (key.equals("android:text")) { 578 | if(value.startsWith("@string/")){ 579 | value = "R.string."+value.substring(8); 580 | } 581 | else{ 582 | value = "\""+value+"\""; 583 | } 584 | buf_code.append(" " + layout_name + ".setText(" + value + ");\n"); 585 | } else if (key.equals("android:textColor")) { 586 | if (value.startsWith("#")) { 587 | buf_code.append(" " + layout_name + ".setTextColor(" + XmlUtil.getColorHex(value) + ");\n"); 588 | } 589 | else if(value.startsWith("@color/")){ 590 | buf_code.append(" " + layout_name + ".setTextColor(getColor(R.color." + value.substring(7) + "));\n"); 591 | } 592 | else 593 | buf_code.append(" " + layout_name + ".setTextColor(" + value + ");\n"); 594 | } 595 | else if(key.equals("android:textSize")){ 596 | buf_code.append(" "+layout_name+".setTextSize("+XmlUtil.getFontSize(value)+");\n"); 597 | } 598 | else if(key.equals("android:ellipsize")){ 599 | String elipsize = value; 600 | if(elipsize.equals("end")){ 601 | elipsize = "TextUtils.TruncateAt.END"; 602 | } 603 | else if(elipsize.equals("start")){ 604 | elipsize = "TextUtils.TruncateAt.START"; 605 | } 606 | else if(elipsize.equals("moddle")){ 607 | elipsize = "TextUtils.TruncateAt.MIDDLE"; 608 | } 609 | else if(elipsize.equals("marquee")){ 610 | elipsize = "TextUtils.TruncateAt.MARQUEE"; 611 | } 612 | buf_code.append(" "+layout_name+".setEllipsize("+elipsize+");\n"); 613 | } 614 | else if (key.equals("android:layout_weight")) { 615 | 616 | } 617 | else if(key.equals("android:autoLink")){ 618 | if(value.equals("web")){ 619 | buf_code.append(" "+layout_name+".setAutoLinkMask("+"Linkify.WEB_URLS"+");\n"); 620 | } 621 | else if(value.equals("phone")){ 622 | buf_code.append(" "+layout_name+".setAutoLinkMask("+"Linkify.PHONE_NUMBERS"+");\n"); 623 | } 624 | else if(value.equals("map")){ 625 | buf_code.append(" "+layout_name+".setAutoLinkMask("+"Linkify.MAP_ADDRESSES"+");\n"); 626 | } 627 | else if(value.equals("email")){ 628 | buf_code.append(" "+layout_name+".setAutoLinkMask("+"Linkify.EMAIL_ADDRESSES"+");\n"); 629 | } 630 | } 631 | //date|textUri|textShortMessage|textAutoCorrect|none|numberSigned|textVisiblePassword|textWebEditText|textMultiLine|textNoSuggestions|textCapSentences| 632 | //textAutoComplete|textImeMultiLine|numberDecimal 633 | else if (key.equals("android:inputType")) { 634 | String items[] = value.split("\\|"); 635 | ArrayList list_items = new ArrayList(); 636 | for (String item : items) 637 | list_items.add(item); 638 | // System.out.println("........................."+items+ " 639 | // "+list_items); 640 | value = ""; 641 | if(list_items.contains("date")){ 642 | value += "|InputType.TYPE_CLASS_DATETIME"; 643 | } 644 | if(list_items.contains("textUri")){ 645 | value += "|InputType.TYPE_TEXT_VARIATION_URI"; 646 | } 647 | if(list_items.contains("textShortMessage")){ 648 | value += "|InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE"; 649 | } 650 | if(list_items.contains("textAutoCorrect")){ 651 | value += "|InputType.TYPE_TEXT_FLAG_AUTO_CORRECT"; 652 | } 653 | if(list_items.contains("none")){ 654 | value += "|InputType.TYPE_DATETIME_VARIATION_NORMAL"; 655 | } 656 | if(list_items.contains("numberSigned")){ 657 | value += "|InputType.TYPE_NUMBER_FLAG_SIGNED"; 658 | } 659 | if(list_items.contains("textVisiblePassword")){ 660 | value += "|InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD"; 661 | } 662 | if(list_items.contains("textWebEditText")){ 663 | value += "|InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT"; 664 | } 665 | if(list_items.contains("textNoSuggestions")){ 666 | value += "|InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS"; 667 | } 668 | if(list_items.contains("textCapSentences")){ 669 | value += "|InputType.TYPE_TEXT_FLAG_CAP_SENTENCES"; 670 | } 671 | if(list_items.contains("textImeMultiLine")){ 672 | value += "|InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE"; 673 | } 674 | 675 | if(list_items.contains("numberDecimal")){ 676 | value += "|InputType.TYPE_NUMBER_FLAG_DECIMAL"; 677 | } 678 | if (list_items.contains("text")) { 679 | value += "|InputType.TYPE_CLASS_TEXT"; 680 | } 681 | if (list_items.contains("number")) { 682 | value += "|InputType.TYPE_CLASS_NUMBER"; 683 | } 684 | if (list_items.contains("textCapCharacters")) { 685 | value += "|InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS"; 686 | } 687 | if (list_items.contains("textMultiLine")) { 688 | value += "|InputType.TYPE_TEXT_FLAG_MULTI_LINE"; 689 | } 690 | if(list_items.contains("textPassword")){ 691 | value += "|InputType.TYPE_TEXT_VARIATION_PASSWORD"; 692 | } 693 | if(value.length()>0){ 694 | value = value.substring(1); 695 | } 696 | buf_code.append(" " + layout_name + ".setInputType(" + value + ");\n"); 697 | } else if (key.equals("android:typeface")) { 698 | if (value.equals("monospace")) { 699 | value = "Typeface.MONOSPACE"; 700 | } else if (value.equals("serif")) { 701 | value = "Typeface.SERIF"; 702 | } else if (value.equals("bold")) { 703 | value = "Typeface.DEFAULT_BOLD"; 704 | } 705 | else if(value.equals("sans")) 706 | { 707 | value = "Typeface.SANS_SERIF"; 708 | } 709 | 710 | System.out.println(" " + layout_name + ".setTypeface(" + value + ");"); 711 | } 712 | else if(key.equals("android:gravity")){ 713 | String gravity = ""; 714 | if(value.indexOf("center")>=0) 715 | gravity+="|Gravity.CENTER"; 716 | if(value.indexOf("left")>=0) 717 | gravity+="|Gravity.LEFT"; 718 | if(value.indexOf("right")>=0) 719 | gravity+="|Gravity.RIGHT"; 720 | if(value.indexOf("top")>=0) 721 | gravity+="|Gravity.TOP"; 722 | if(value.indexOf("bottom")>=0) 723 | gravity+="|Gravity.BOTTOM"; 724 | buf_code.append(" " + layout_name+".setGravity("+gravity.substring(1)+");\n"); 725 | } 726 | else if(key.equals("android:layout_gravity")){ 727 | String gravity = ""; 728 | if(value.indexOf("center")>=0) 729 | gravity+="|Gravity.CENTER"; 730 | if(value.indexOf("left")>=0) 731 | gravity+="|Gravity.LEFT"; 732 | if(value.indexOf("right")>=0) 733 | gravity+="|Gravity.RIGHT"; 734 | if(value.indexOf("top")>=0) 735 | gravity+="|Gravity.TOP"; 736 | if(value.indexOf("bottom")>=0) 737 | gravity+="|Gravity.BOTTOM"; 738 | if(value.indexOf("end")>=0) 739 | gravity+="|Gravity.END"; 740 | if(value.indexOf("start")>=0) 741 | gravity+="|Gravity.START"; 742 | buf_code.append(" " + element.getAttribute("layoutparams")+".gravity = "+gravity.substring(1)+";\n"); 743 | 744 | } 745 | else if(key.equals("android:scrollbars")){ 746 | if(value.equals("vertical")) 747 | { 748 | buf_code.append(" " + layout_name+".setVerticalScrollBarEnabled(true);\n"); 749 | buf_code.append(" "+layout_name + ".setScrollBarStyle(View.SCROLLBARS_OUTSIDE_INSET);\n"); 750 | } 751 | else if(value.equals("none")) 752 | { 753 | buf_code.append(" "+ layout_name + ".setVerticalScrollBarEnabled(false);\n"); 754 | } 755 | else 756 | { 757 | buf_code.append(" " + layout_name + ".setScrollBarStyle(view.SCROLLBARS_INSIDE_OVERLAY);\n"); 758 | } 759 | } 760 | else if(key.equals("android:minHeight")){ 761 | value = XmlUtil.getSize(value); 762 | buf_code.append(" "+layout_name+".setMinHeight("+value+");\n"); 763 | } 764 | else if(key.equals("android:maxHeight")){ 765 | value = XmlUtil.getSize(value); 766 | buf_code.append(" "+layout_name+".setMaxHeight("+value+");\n"); 767 | } 768 | else if(key.equals("android:minWidth")){ 769 | value = XmlUtil.getSize(value); 770 | buf_code.append(" "+layout_name+".setMinWidth("+value+");\n"); 771 | } 772 | else if(key.equals("android:maxWidth")){ 773 | value = XmlUtil.getSize(value); 774 | buf_code.append(" "+layout_name+".setMaxWidth("+value+");\n"); 775 | } 776 | else if(key.equals("android:elevation")){ 777 | value = XmlUtil.getSize(value); 778 | buf_code.append(" "+layout_name+".setElevation("+value+");\n"); 779 | } 780 | else if(key.equals("android:shadowColor")){ 781 | String shadowDx = element.getAttribute("android:shadowDx"); 782 | String shadowDy = element.getAttribute("android:shadowDy"); 783 | String shadowColor = value; 784 | if(shadowColor.startsWith("#")){ 785 | shadowColor = XmlUtil.getColorHex(shadowColor); 786 | } 787 | else if(shadowColor.startsWith("@color/")){ 788 | shadowColor = "R.color."+shadowColor.substring(7); 789 | } 790 | buf_code.append(" "+layout_name+".setShadowLayer("+0+", "+XmlUtil.getSize(shadowDx)+", "+XmlUtil.getSize(shadowDy)+", "+shadowColor+");\n"); 791 | } 792 | else if(key.endsWith("android:scaleType")){ 793 | if(value.equals("matrix")) 794 | { 795 | buf_code.append(" "+layout_name+".setScaleType(ImageView.ScaleType.MATRIX);\n"); 796 | } 797 | else if(value.equals("fitXY")) 798 | { 799 | buf_code.append(" "+layout_name+".setScaleType(ImageView.ScaleType.FIT_XY);\n"); 800 | } 801 | else if(value.equals("fitStart")) 802 | { 803 | buf_code.append(" "+layout_name+".setScaleType(ImageView.ScaleType.FIT_START);\n"); 804 | 805 | } 806 | else if(value.equals("fitCenter")) 807 | { 808 | buf_code.append(" "+layout_name+".setScaleType(ImageView.ScaleType.FIT_CENTER);\n"); 809 | 810 | } 811 | else if(value.equals("fitEnd")) 812 | { 813 | buf_code.append(" "+layout_name+".setScaleType(ImageView.ScaleType.FIT_END);\n"); 814 | 815 | }else if(value.equals("center")) 816 | { 817 | buf_code.append(" "+layout_name+".setScaleType(ImageView.ScaleType.CENTER);\n"); 818 | 819 | } 820 | else if(value.equals("centerCrop")) 821 | { 822 | buf_code.append(" "+layout_name+".setScaleType(ImageView.ScaleType.CENTER_CROP);\n"); 823 | 824 | } 825 | else if(value.equals("centerInside")) 826 | { 827 | buf_code.append(" "+layout_name+".setScaleType(ImageView.ScaleType.CENTER_INSIDE);\n"); 828 | 829 | } 830 | } 831 | else if(key.equals("android:tint")){ 832 | String color = value; 833 | if(color.startsWith("#")){ 834 | color = XmlUtil.getColorHex(color); 835 | } 836 | else if(color.startsWith("@color/")){ 837 | color = "R.color."+color.substring(7); 838 | } 839 | buf_code.append(" "+layout_name+".setColorFilter("+color+");\n"); 840 | } 841 | else if(key.equals("android:paddingTop")){ 842 | // String padding_top = element.getAttribute("android:paddingTop"); 843 | // String padding_bottom = element.getAttribute("android:paddingBottom"); 844 | // String padding_left = element.getAttribute("android:paddingLeft"); 845 | // String padding_right = element.getAttribute("android:paddingRight"); 846 | // element.removeAttribute("android:paddingTop"); 847 | // element.removeAttribute("android:paddingBottom"); 848 | // element.removeAttribute("android:paddingLeft"); 849 | // element.removeAttribute("android:paddingRight"); 850 | // padding_top = XmlUtil.getSize(padding_top); 851 | // padding_left = XmlUtil.getSize(padding_left); 852 | // padding_right = XmlUtil.getSize(padding_right); 853 | // padding_bottom = XmlUtil.getSize(padding_bottom); 854 | // System.out.println(" "+layout_name+".setPadding("+padding_left+", "+padding_top+", "+padding_right+", "+padding_bottom+");"); 855 | } 856 | else if(key.equals("android:paddingBottom")){ 857 | // String padding_top = element.getAttribute("android:paddingTop"); 858 | // String padding_bottom = element.getAttribute("android:paddingBottom"); 859 | // String padding_left = element.getAttribute("android:paddingLeft"); 860 | // String padding_right = element.getAttribute("android:paddingRight"); 861 | // element.removeAttribute("android:paddingTop"); 862 | // element.removeAttribute("android:paddingBottom"); 863 | // element.removeAttribute("android:paddingLeft"); 864 | // element.removeAttribute("android:paddingRight"); 865 | // padding_top = XmlUtil.getSize(padding_top); 866 | // padding_left = XmlUtil.getSize(padding_left); 867 | // padding_right = XmlUtil.getSize(padding_right); 868 | // padding_bottom = XmlUtil.getSize(padding_bottom); 869 | // System.out.println(" "+layout_name+".setPadding("+padding_left+", "+padding_top+", "+padding_right+", "+padding_bottom+");"); 870 | 871 | } 872 | else if(key.equals("android:paddingLeft")){ 873 | // String padding_top = element.getAttribute("android:paddingTop"); 874 | // String padding_bottom = element.getAttribute("android:paddingBottom"); 875 | // String padding_left = element.getAttribute("android:paddingLeft"); 876 | // String padding_right = element.getAttribute("android:paddingRight"); 877 | // element.removeAttribute("android:paddingTop"); 878 | // element.removeAttribute("android:paddingBottom"); 879 | // element.removeAttribute("android:paddingLeft"); 880 | // element.removeAttribute("android:paddingRight"); 881 | // padding_top = XmlUtil.getSize(padding_top); 882 | // padding_left = XmlUtil.getSize(padding_left); 883 | // padding_right = XmlUtil.getSize(padding_right); 884 | // padding_bottom = XmlUtil.getSize(padding_bottom); 885 | // System.out.println(" "+layout_name+".setPadding("+padding_left+", "+padding_top+", "+padding_right+", "+padding_bottom+");"); 886 | 887 | } 888 | else if(key.equals("android:paddingRight")){ 889 | // String padding_top = element.getAttribute("android:paddingTop"); 890 | // String padding_bottom = element.getAttribute("android:paddingBottom"); 891 | // String padding_left = element.getAttribute("android:paddingLeft"); 892 | // String padding_right = element.getAttribute("android:paddingRight"); 893 | // element.removeAttribute("android:paddingTop"); 894 | // element.removeAttribute("android:paddingBottom"); 895 | // element.removeAttribute("android:paddingLeft"); 896 | // element.removeAttribute("android:paddingRight"); 897 | // padding_top = XmlUtil.getSize(padding_top); 898 | // padding_left = XmlUtil.getSize(padding_left); 899 | // padding_right = XmlUtil.getSize(padding_right); 900 | // padding_bottom = XmlUtil.getSize(padding_bottom); 901 | // System.out.println(" "+layout_name+".setPadding("+padding_left+", "+padding_top+", "+padding_right+", "+padding_bottom+");"); 902 | 903 | } 904 | else if(key.equals("android:padding")){ 905 | // buf_code.append(" "+layout_name+".setPadding("+value+", "+value+", "+value+", "+value+");\n"); 906 | } 907 | else if(key.equals("android:alpha")){ 908 | buf_code.append(" "+layout_name+".setAlpha("+value+");\n"); 909 | } 910 | else if(key.equals("android:textStyle")){ 911 | if(value.equals("bold")) 912 | { 913 | buf_code.append(" "+layout_name+".setTypeface("+layout_name+".getTypeface(), Typeface.BOLD);\n" ); 914 | } 915 | if(value.equals("italic")) 916 | { 917 | buf_code.append(" "+layout_name+".setTypeface("+layout_name+".getTypeface(), Typeface.ITALIC);\n" ); 918 | } 919 | if(value.equals("bold_italic")){ 920 | buf_code.append(" "+layout_name+".setTypeface("+layout_name+".getTypeface(), Typeface.BOLD_ITALIC);\n" ); 921 | } 922 | 923 | } 924 | else if(key.equals("android:visibility")){ 925 | 926 | if(value.equals("visible")){ 927 | buf_code.append(" "+layout_name+".setVisibility(View.VISIBLE);\n"); 928 | } 929 | 930 | else if(value.equals("invisible")){ 931 | buf_code.append(" "+layout_name+".setVisibility(View.INVISIBLE);\n"); 932 | } 933 | 934 | else if(value.equals("gone")){ 935 | buf_code.append(" "+layout_name+".setVisibility(View.GONE);\n"); 936 | } 937 | 938 | 939 | } 940 | else if(key.equals("android:singleLine")){ 941 | buf_code.append(" "+layout_name+".setSingleLine("+value+");\n"); 942 | } 943 | else if(key.equals("android:ems")){ 944 | buf_code.append(" "+layout_name+".setEms("+XmlUtil.getSize(value)+");\n"); 945 | } 946 | else if(key.equals("android:lines")) 947 | { 948 | buf_code.append(" "+layout_name+".setLines("+value+");\n"); 949 | } 950 | else if(key.equals("android:minLines")){ 951 | buf_code.append(" "+layout_name+".setMinLines("+value+");\n"); 952 | } 953 | else if(key.equals("android:maxLines")){ 954 | buf_code.append(" "+layout_name+".setMaxLines("+value+");\n"); 955 | } 956 | else if(key.equals("android:hint")) 957 | { 958 | if(value.startsWith("@string/")){ 959 | value = "R.string."+value.substring(8); 960 | } 961 | buf_code.append(" "+layout_name+".setHint(\""+value+"\");\n"); 962 | } 963 | else if(key.equals("android:drawable")){ 964 | if(value.startsWith("@drawable/")){ 965 | value = "R.drawable."+value.substring(10); 966 | } 967 | else if(value.startsWith("@mipmap/")){ 968 | value = "R.mipmap."+value.substring(8); 969 | } 970 | buf_code.append(" "+layout_name+".setImageDrawable(context.getDrawable("+value+"));\n"); 971 | } 972 | else if(key.equals("name")){ 973 | 974 | } 975 | else if (key.equals("id")) { 976 | 977 | } else if (key.equals("layoutparams")) { 978 | 979 | } else if (key.equals("xmlns:app")) { 980 | 981 | } else if (key.equals("xmlns:android")) { 982 | 983 | } else if (key.equals("xmlns:tools")) { 984 | 985 | } else if (key.equals("android:id")) { 986 | if (value.startsWith("@+id/") || value.startsWith("@id/")) { 987 | buf_code.append( 988 | " " + layout_name + ".setId(R.id." + value.substring(value.indexOf("/") + 1) + ");\n"); 989 | } else 990 | buf_code.append(" " + layout_name + ".setId(" + value + ");\n"); 991 | } else if (key.startsWith("android:")) { 992 | buf_code.append(""+layout_name+"."+key.substring(8)+" = "+value+"\n"); 993 | } 994 | else if(key.startsWith("app:")){ 995 | buf_code.append(""+layout_name+"."+key.substring(4)+" = "+value+"\n"); 996 | } 997 | else 998 | buf_code.append(" " + layout_name + "." + map.item(ii).getNodeName() + " = " 999 | + map.item(ii).getNodeValue() + ";\n"); 1000 | } 1001 | 1002 | for (int n = 0; n < nodelist.getLength(); n++) { 1003 | Node nodeitem = nodelist.item(n); 1004 | 1005 | // System.out.println("node item = " + nodeitem.getNodeName() + 1006 | // " value=" + nodeitem.getNodeValue() + " type=" 1007 | // + nodeitem.getNodeType() + " "); 1008 | printAndroidCode(element.getNodeName(), layout_name, nodeitem); 1009 | } 1010 | } 1011 | 1012 | if (node.getNodeType() == 1) { 1013 | Element element1 = (Element) node; 1014 | buf_code.append(" " + name + ".addView(" + element1.getAttribute("name") + "," 1015 | + element1.getAttribute("layoutparams") + ");\n"); 1016 | } 1017 | } 1018 | 1019 | 1020 | // 输出 ios swift 代码 1021 | void printiOSCode(String className, String name, Node node) { 1022 | NodeList nodelist = node.getChildNodes(); 1023 | // System.out.println("node name=" + node.getNodeName() + " value=" + 1024 | // node.getNodeValue() + " type=" + node.getNodeType()); 1025 | if (node.getNodeType() == 1) { 1026 | // 输出代码 1027 | Element element = (Element) node; 1028 | 1029 | String layout_name = element.getAttribute("name"); 1030 | String nodeName = element.getNodeName(); 1031 | // if(nodeName.equals("LinearLayout")){ 1032 | // nodeName = "TGLinearLayout"; 1033 | // } 1034 | // if(nodeName.equals("Button")){ 1035 | // nodeName = "UIButton"; 1036 | // } 1037 | // if(nodeName.equals("EditText")){ 1038 | // nodeName = "UITextField"; 1039 | // } 1040 | // if(nodeName.equals("ImageView")){ 1041 | // nodeName = "UIImageView"; 1042 | // } 1043 | //优先处理margin top layout_width layout_height layout_weight 1044 | String layout_width = element.getAttribute("android:layout_width"); 1045 | String layout_height = element.getAttribute("android:layout_height"); 1046 | String layout_weight = element.getAttribute("android:layout_weight"); 1047 | String orientation = element.getAttribute("android:orientation"); 1048 | if (layout_width.equals("match_parent") || layout_width.equals("fill_parent")) { 1049 | layout_width = ".fill"; 1050 | } 1051 | else if (layout_width.equals("wrap_content")) { 1052 | layout_width = ".wrap"; 1053 | } 1054 | else { 1055 | layout_width = XmlUtil.getSize(layout_width); 1056 | } 1057 | 1058 | if (layout_height.equals("match_parent") || layout_height.equals("fill_parent")) { 1059 | layout_height = ".fill"; 1060 | } 1061 | else if (layout_height.equals("wrap_content")) { 1062 | layout_height = ".wrap"; 1063 | } 1064 | else { 1065 | layout_height = XmlUtil.getSize(layout_height); 1066 | } 1067 | if(orientation.equals("vertical")){ 1068 | orientation = ".vert"; 1069 | } 1070 | else if(orientation.equals("horizontal")){ 1071 | orientation = ".horz"; 1072 | } 1073 | 1074 | 1075 | 1076 | 1077 | //创建layout 1078 | if(nodeName.equals("LinearLayout")){ 1079 | System.out.println(" var " + layout_name + ":" + "TGLinearLayout" + " = " 1080 | + "TGLinearLayout" + "(" + orientation+")"); 1081 | } 1082 | else if(nodeName.equals("FrameLayout")){ 1083 | System.out.println(" var " + layout_name + ":" + "TGFrameLayout" + " = " 1084 | + "TGFrameLayout" + "(" + orientation+")"); 1085 | } 1086 | else if(nodeName.equals("TextView")){ 1087 | System.out.println(" var " + layout_name + ":" + "UITextView" + " = " 1088 | + "UITextView" + "(" +")"); 1089 | } 1090 | else if(nodeName.equals("Button")){ 1091 | System.out.println(" var " + layout_name + ":" + "UIButton" + " = " 1092 | + "UIButton" + "(" +")"); 1093 | } 1094 | else if(nodeName.equals("ImageView")){ 1095 | System.out.println(" var " + layout_name + ":" + "UIImageView" + " = " 1096 | + "UIImageView" + "(" +")"); 1097 | } 1098 | else if(nodeName.equals("EditText")){ 1099 | System.out.println(" var " + layout_name + ":" + "UITextField" + " = " 1100 | + "UITextField" + "(" +")"); 1101 | } 1102 | else { 1103 | System.out.println(" var " + layout_name + ":" + nodeName + " = " 1104 | + nodeName + "(" +")"); 1105 | } 1106 | 1107 | System.out.println(" " + layout_name + ".width = "+layout_width); 1108 | System.out.println(" " + layout_name + ".height = "+layout_height); 1109 | 1110 | 1111 | String margin = element.getAttribute("android:layout_margin"); 1112 | String margin_top = element.getAttribute("android:layout_marginTop"); 1113 | String margin_bottom = element.getAttribute("android:layout_marginBottom"); 1114 | String margin_left = element.getAttribute("android:layout_marginLeft"); 1115 | String margin_right = element.getAttribute("android:layout_marginRight"); 1116 | if(margin.length()!=0){ 1117 | margin_top = margin; 1118 | margin_left = margin; 1119 | margin_right = margin; 1120 | margin_bottom = margin; 1121 | } 1122 | if(margin_left.length()!=0 || margin_top.length()!=0 || margin_right.length()!=0 || margin_bottom.length()!=0){ 1123 | margin_left = XmlUtil.getSize(margin_left); 1124 | margin_top = XmlUtil.getSize(margin_top); 1125 | margin_right = XmlUtil.getSize(margin_right); 1126 | margin_bottom = XmlUtil.getSize(margin_bottom); 1127 | System.out.println(" "+layout_name+ ".margin = UIEdgeInsets(top:"+margin_top+", left:"+margin_left+", bottom:"+margin_bottom+", right:"+margin_right+");"); 1128 | } 1129 | 1130 | String padding = element.getAttribute("android:padding"); 1131 | String padding_left = element.getAttribute("android:paddingLeft"); 1132 | String padding_right = element.getAttribute("android:paddingRight"); 1133 | String padding_top = element.getAttribute("android:paddingTop"); 1134 | String padding_bottom = element.getAttribute("android:paddingBottom"); 1135 | if(padding.length()!=0){ 1136 | padding_left = padding; 1137 | padding_top = padding; 1138 | padding_right = padding; 1139 | padding_bottom = padding; 1140 | } 1141 | if(padding_left.length()!=0 || padding_top.length()!=0 || padding_bottom.length()!=0 || padding_right.length()!=0){ 1142 | padding_left = XmlUtil.getSize(padding_left); 1143 | padding_top = XmlUtil.getSize(padding_top); 1144 | padding_right = XmlUtil.getSize(padding_right); 1145 | padding_bottom = XmlUtil.getSize(padding_bottom); 1146 | System.out.println(" "+layout_name+ ".padding = UIEdgeInsets(top:"+padding_top+", left:"+padding_left+", bottom:"+padding_bottom+", right:"+padding_right+");"); 1147 | 1148 | } 1149 | 1150 | NamedNodeMap map = node.getAttributes(); 1151 | for (int ii = 0; ii < map.getLength(); ii++) { 1152 | String key = map.item(ii).getNodeName(); 1153 | String value = element.getAttribute(key); 1154 | 1155 | if (key.equals("android:layout_width")) { 1156 | } 1157 | else if(key.indexOf("xmlns:")>=0){ 1158 | 1159 | } 1160 | else if(key.equals("android:layout_margin")){ 1161 | } 1162 | else if(key.equals("android:layout_marginTop")){ 1163 | 1164 | } 1165 | else if(key.equals("android:layout_marginBottom")){ 1166 | 1167 | } 1168 | else if(key.equals("android:layout_marginLeft")){ 1169 | 1170 | } 1171 | else if(key.equals("android:layout_marginRight")){ 1172 | 1173 | } 1174 | else if (key.equals("android:layout_height")) { 1175 | 1176 | } else if (key.equals("android:theme")) { 1177 | 1178 | } else if (key.equals("android:background")) { 1179 | if(value.startsWith("@color/")){ 1180 | System.out.println(" "+layout_name+".backgroundColor = UIColor."+value.substring(7)); 1181 | } 1182 | 1183 | } else if (key.equals("android:orientation")) { 1184 | 1185 | } else if (key.equals("android:src")) { 1186 | value = XmlUtil.getDrawable(value); 1187 | System.out.println(" "+layout_name+".setImage(UIImage(named:\""+value+"\"),for: UIControl.State.normal)"); 1188 | 1189 | } else if (key.equals("android:text")) { 1190 | if(value.startsWith("@string/")){ 1191 | System.out.println(" "+layout_name+".text = "+value.substring(8)); 1192 | } 1193 | else{ 1194 | System.out.println(" "+layout_name+".text = \""+value+"\""); 1195 | } 1196 | } else if (key.equals("android:textColor")) { 1197 | if (value.startsWith("#")) { 1198 | System.out.println(" " + layout_name + ".textColor = " + XmlUtil.getColorHex(value) + ""); 1199 | } 1200 | else if(value.startsWith("@color/")){ 1201 | System.out.println(" " + layout_name + ".textColor = " + value.substring(7) + ""); 1202 | } 1203 | else 1204 | System.out.println(" " + layout_name + ".textColor = " + value + ""); 1205 | } 1206 | else if(key.equals("android:textSize")){ 1207 | System.out.println(" "+layout_name+".font = UIFont.systemFont(ofSize: "+value+")"); 1208 | } 1209 | else if(key.equals("android:ellipsize")){ 1210 | String elipsize = value; 1211 | if(elipsize.equals("end")){ 1212 | elipsize = "NSLineBreakMode.byTruncatingHead"; 1213 | } 1214 | else if(elipsize.equals("start")){ 1215 | elipsize = "NSLineBreakMode.byTruncatingHead"; 1216 | } 1217 | else if(elipsize.equals("moddle")){ 1218 | elipsize = "NSLineBreakMode.byTruncatingMiddle"; 1219 | } 1220 | else if(elipsize.equals("marquee")){ 1221 | elipsize = "MSLineBreakMode.byWordWrapping"; 1222 | } 1223 | System.out.println(" "+layout_name+".lineBreakMode = "+elipsize+""); 1224 | } 1225 | else if (key.equals("android:layout_weight")) { 1226 | buf_code.append(" "+element.getAttribute("layoutparams")+".weight = "+value+";\n"); 1227 | } 1228 | //date|textUri|textShortMessage|textAutoCorrect|none|numberSigned|textVisiblePassword|textWebEditText|textMultiLine|textNoSuggestions|textCapSentences| 1229 | //textAutoComplete|textImeMultiLine|numberDecimal 1230 | else if (key.equals("android:inputType")) { 1231 | // String items[] = value.split("\\|"); 1232 | // ArrayList list_items = new ArrayList(); 1233 | // for (String item : items) 1234 | // list_items.add(item); 1235 | // // System.out.println("........................."+items+ " 1236 | // // "+list_items); 1237 | // value = ""; 1238 | // if(list_items.contains("date")){ 1239 | // value += "|UIKeybordType.asciiCapable"; 1240 | // } 1241 | // if(list_items.contains("textUri")){ 1242 | // value += "|UIKeybordType.URL"; 1243 | // } 1244 | // if(list_items.contains("textShortMessage")){ 1245 | // value += "|default"; 1246 | // } 1247 | // if(list_items.contains("textAutoCorrect")){ 1248 | // value += "|default"; 1249 | // } 1250 | // if(list_items.contains("none")){ 1251 | // value += "|default"; 1252 | // } 1253 | // if(list_items.contains("numberSigned")){ 1254 | // value += "|numberPad"; 1255 | // } 1256 | // if(list_items.contains("textVisiblePassword")){ 1257 | // value += "|"; 1258 | // } 1259 | // if(list_items.contains("textWebEditText")){ 1260 | // value += "|"; 1261 | // } 1262 | // if(list_items.contains("textNoSuggestions")){ 1263 | // value += "|"; 1264 | // } 1265 | // if(list_items.contains("textCapSentences")){ 1266 | // value += "|"; 1267 | // } 1268 | // if(list_items.contains("textImeMultiLine")){ 1269 | // value += "|"; 1270 | // } 1271 | // 1272 | // if(list_items.contains("numberDecimal")){ 1273 | // value += "|"; 1274 | // } 1275 | // if (list_items.contains("text")) { 1276 | // value += "|default"; 1277 | // } 1278 | // if (list_items.contains("number")) { 1279 | // value += "|numberPad"; 1280 | // } 1281 | // if (list_items.contains("textCapCharacters")) { 1282 | // value += "|"; 1283 | // } 1284 | // if (list_items.contains("textMultiLine")) { 1285 | // value += "|"; 1286 | // } 1287 | // if(list_items.contains("textPassword")){ 1288 | // value += "|"; 1289 | // } 1290 | // if(value.length()>0){ 1291 | // value = value.substring(1); 1292 | // } 1293 | // System.out.println(" " + layout_name + ".setInputType(" + value + ");"); 1294 | } else if (key.equals("android:typeface")) { 1295 | // if (value.equals("monospace")) { 1296 | // value = "Typeface.MONOSPACE"; 1297 | // } else if (value.equals("serif")) { 1298 | // value = "Typeface.SERIF"; 1299 | // } else if (value.equals("bold")) { 1300 | // value = "Typeface.DEFAULT_BOLD"; 1301 | // } 1302 | // else if(value.equals("sans")) 1303 | // { 1304 | // value = "Typeface.SANS_SERIF"; 1305 | // } 1306 | // 1307 | // System.out.println(" " + layout_name + ".setTypeface(" + value + ");"); 1308 | } 1309 | else if(key.equals("android:gravity")){ 1310 | String gravity_hor = ""; 1311 | String gravity_ver = ""; 1312 | if(value.indexOf("left")>=0) 1313 | gravity_hor+="TGGravity.horz.left"; 1314 | if(value.indexOf("right")>=0) 1315 | gravity_hor+="TGGravity.horz.right"; 1316 | 1317 | 1318 | 1319 | 1320 | if(value.indexOf("top")>=0) 1321 | gravity_ver+="TGGravity.vert.top"; 1322 | if(value.indexOf("bottom")>=0) 1323 | gravity_ver+="TGGravity.vert.bottom"; 1324 | 1325 | if(value.indexOf("center")>=0){ 1326 | if(gravity_hor.length()==0){ 1327 | gravity_hor = "TGGravity.horz.center"; 1328 | } 1329 | if(gravity_ver.length()==0){ 1330 | gravity_ver = "TGGravity.vert.center"; 1331 | } 1332 | } 1333 | 1334 | if(gravity_hor.length()==0){ 1335 | gravity_hor = "TGGravity.horz.left"; 1336 | } 1337 | if(gravity_ver.length()==0){ 1338 | gravity_ver = "TGGravity.vert.top"; 1339 | } 1340 | 1341 | 1342 | 1343 | System.out.println(" " + layout_name+".tg_aligment = ["+gravity_hor+", "+gravity_ver+"]"); 1344 | } 1345 | else if(key.equals("android:layout_gravity")){ 1346 | String gravity_hor = ""; 1347 | String gravity_ver = ""; 1348 | if(value.indexOf("left")>=0) 1349 | gravity_hor+="TGGravity.horz.left"; 1350 | if(value.indexOf("right")>=0) 1351 | gravity_hor+="TGGravity.horz.right"; 1352 | 1353 | 1354 | 1355 | 1356 | if(value.indexOf("top")>=0) 1357 | gravity_ver+="TGGravity.vert.top"; 1358 | if(value.indexOf("bottom")>=0) 1359 | gravity_ver+="TGGravity.vert.bottom"; 1360 | 1361 | if(value.indexOf("center")>=0){ 1362 | if(gravity_hor.length()==0){ 1363 | gravity_hor = "TGGravity.horz.center"; 1364 | } 1365 | if(gravity_ver.length()==0){ 1366 | gravity_ver = "TGGravity.vert.center"; 1367 | } 1368 | } 1369 | 1370 | if(gravity_hor.length()==0){ 1371 | gravity_hor = "TGGravity.horz.left"; 1372 | } 1373 | if(gravity_ver.length()==0){ 1374 | gravity_ver = "TGGravity.vert.top"; 1375 | } 1376 | 1377 | 1378 | 1379 | System.out.println(" " + layout_name+".tg_gravity = ["+gravity_hor+", "+gravity_ver+"]"); 1380 | 1381 | } 1382 | else if(key.equals("android:scrollbars")){ 1383 | // if(value.equals("vertical")) 1384 | // { 1385 | // System.out.println(" " + layout_name+".setVerticalScrollBarEnabled(true);"); 1386 | // System.out.println(" "+layout_name + ".setScrollBarStyle(View.SCROLLBARS_OUTSIDE_INSET);"); 1387 | // } 1388 | // else if(value.equals("none")) 1389 | // { 1390 | // System.out.println(" "+ layout_name + ".setVerticalScrollBarEnabled(false);"); 1391 | // } 1392 | // else 1393 | // { 1394 | // System.out.println(" " + layout_name + ".setScrollBarStyle(view.SCROLLBARS_INSIDE_OVERLAY);"); 1395 | // } 1396 | } 1397 | else if(key.equals("android:minHeight")){ 1398 | value = XmlUtil.getSize(value); 1399 | System.out.println(" "+layout_name+".tg_top.min("+value+");"); 1400 | } 1401 | else if(key.equals("android:maxHeight")){ 1402 | value = XmlUtil.getSize(value); 1403 | System.out.println(" "+layout_name+".tg_top.max("+value+");"); 1404 | } 1405 | else if(key.equals("android:minWidth")){ 1406 | value = XmlUtil.getSize(value); 1407 | System.out.println(" "+layout_name+".tg_top.min("+value+");"); 1408 | } 1409 | else if(key.equals("android:maxWidth")){ 1410 | value = XmlUtil.getSize(value); 1411 | System.out.println(" "+layout_name+".tg_left.max("+value+");"); 1412 | } 1413 | else if(key.equals("android:elevation")){ 1414 | // value = XmlUtil.getSize(value); 1415 | // System.out.println(" "+layout_name+".setElevation("+value+");"); 1416 | } 1417 | else if(key.equals("android:shadowColor")){ 1418 | String shadowDx = element.getAttribute("android:shadowDx"); 1419 | String shadowDy = element.getAttribute("android:shadowDy"); 1420 | String shadowColor = value; 1421 | if(shadowColor.startsWith("#")){ 1422 | shadowColor = XmlUtil.getColorHex(shadowColor); 1423 | } 1424 | else if(shadowColor.startsWith("@color/")){ 1425 | shadowColor = "R.color."+shadowColor.substring(7); 1426 | } 1427 | System.out.println(" "+layout_name+".layer.shadowColor = "+shadowColor+""); 1428 | System.out.println(" "+layout_name+".layer.shadowOffset = "+"CGSize(width:"+shadowDx+", height:"+shadowDy+")"); 1429 | } 1430 | else if(key.endsWith("android:scaleType")){ 1431 | if(value.equals("matrix")) 1432 | { 1433 | System.out.println(" "+layout_name+".contentMode = UIView.ContentMode.center"); 1434 | } 1435 | else if(value.equals("fitXY")) 1436 | { 1437 | System.out.println(" "+layout_name+".contentMode = UIView.ContentMode.scaleAspectFill"); 1438 | } 1439 | else if(value.equals("fitStart")) 1440 | { 1441 | System.out.println(" "+layout_name+".contentMode = UIView.ContentMode.scaleAspectFill"); 1442 | 1443 | } 1444 | else if(value.equals("fitCenter")) 1445 | { 1446 | System.out.println(" "+layout_name+".contentMode = UIView.ContentMode.center"); 1447 | 1448 | } 1449 | else if(value.equals("fitEnd")) 1450 | { 1451 | System.out.println(" "+layout_name+".contentMode = UIView.ContentMode.center"); 1452 | 1453 | }else if(value.equals("center")) 1454 | { 1455 | System.out.println(" "+layout_name+".contentMode = UIView.ContentMode.center"); 1456 | 1457 | } 1458 | else if(value.equals("centerCrop")) 1459 | { 1460 | System.out.println(" "+layout_name+".contentMode = UIView.ContentMode.center"); 1461 | 1462 | } 1463 | else if(value.equals("centerInside")) 1464 | { 1465 | System.out.println(" "+layout_name+".contentMode = UIView.ContentMode.center"); 1466 | 1467 | } 1468 | } 1469 | else if(key.equals("android:tint")){ 1470 | String color = value; 1471 | if(color.startsWith("#")){ 1472 | color = XmlUtil.getColorHex(color); 1473 | } 1474 | else if(color.startsWith("@color/")){ 1475 | color = "R.color."+color.substring(7); 1476 | } 1477 | System.out.println(" "+layout_name+".tintColor = "+color+""); 1478 | } 1479 | else if(key.equals("android:paddingTop")){ 1480 | 1481 | } 1482 | else if(key.equals("android:paddingBottom")){ 1483 | 1484 | } 1485 | else if(key.equals("android:paddingLeft")){ 1486 | 1487 | 1488 | } 1489 | else if(key.equals("android:paddingRight")){ 1490 | 1491 | 1492 | } 1493 | else if(key.equals("android:padding")){ 1494 | 1495 | } 1496 | else if(key.equals("android:alpha")){ 1497 | System.out.println(" "+layout_name+".layer.alpha = "+value+"f"); 1498 | } 1499 | else if(key.equals("android:textStyle")){ 1500 | // if(value.equals("bold")) 1501 | // { 1502 | // System.out.println(" "+layout_name+".setTypeface("+layout_name+".getTypeface(), Typeface.BOLD);" ); 1503 | // } 1504 | // if(value.equals("italic")) 1505 | // { 1506 | // System.out.println(" "+layout_name+".setTypeface("+layout_name+".getTypeface(), Typeface.ITALIC);" ); 1507 | // } 1508 | // if(value.equals("bold_italic")){ 1509 | // System.out.println(" "+layout_name+".setTypeface("+layout_name+".getTypeface(), Typeface.BOLD_ITALIC);" ); 1510 | // } 1511 | // 1512 | } 1513 | else if(key.equals("android:visibility")){ 1514 | 1515 | if(value.equals("visible")){ 1516 | System.out.println(" "+layout_name+".hidden = false"); 1517 | } 1518 | 1519 | else if(value.equals("invisible")){ 1520 | System.out.println(" "+layout_name+".hidden = true"); 1521 | } 1522 | 1523 | else if(value.equals("gone")){ 1524 | System.out.println(" "+layout_name+".hidden = true"); 1525 | } 1526 | // 1527 | 1528 | } 1529 | else if(key.equals("android:singleLine")){ 1530 | if(value == "true"){ 1531 | System.out.println(" "+layout_name+".numberOfLines = 1"); 1532 | } 1533 | else{ 1534 | System.out.println(" "+layout_name+".lineBreakMode = NSLineBreakMode.ByWordWrapping"); 1535 | System.out.println(" "+layout_name+".numberOfLines = 0"); 1536 | } 1537 | // System.out.println(" "+layout_name+".setSingleLine("+value+");"); 1538 | } 1539 | else if(key.equals("android:ems")){ 1540 | // System.out.println(" "+layout_name+".setEms("+XmlUtil.getSize(value)+");"); 1541 | } 1542 | else if(key.equals("android:lines")) 1543 | { 1544 | System.out.println(" "+layout_name+".lineBreakMode = NSLineBreakMode.ByWordWrapping"); 1545 | System.out.println(" "+layout_name+".numberOfLines = "+value); 1546 | } 1547 | else if(key.equals("android:minLines")){ 1548 | // System.out.println(" "+layout_name+".setMinLines("+value+");"); 1549 | } 1550 | else if(key.equals("android:maxLines")){ 1551 | // System.out.println(" "+layout_name+".setMaxLines("+value+");"); 1552 | } 1553 | else if(key.equals("android:hint")) 1554 | { 1555 | if(value.startsWith("@string/")){ 1556 | value = "R.string."+value.substring(8); 1557 | } 1558 | System.out.println(" "+layout_name+".placeholder = \""+value+"\""); 1559 | } 1560 | else if(key.equals("android:drawable")){ 1561 | if(value.startsWith("@drawable/")){ 1562 | value = "R.drawable."+value.substring(10); 1563 | } 1564 | else if(value.startsWith("@mipmap/")){ 1565 | value = "R.mipmap."+value.substring(8); 1566 | } 1567 | System.out.println(" "+layout_name+".setImage(UIImage(named:"+value+"),for: UIControl.State.normal)"); 1568 | } 1569 | else if(key.equals("name")){ 1570 | 1571 | } 1572 | else if (key.equals("id")) { 1573 | 1574 | } else if (key.equals("layoutparams")) { 1575 | 1576 | } else if (key.equals("xmlns:app")) { 1577 | 1578 | } else if (key.equals("xmlns:android")) { 1579 | 1580 | } else if (key.equals("xmlns:tools")) { 1581 | 1582 | } else if (key.equals("android:id")) { 1583 | // if (value.startsWith("@+id/") || value.startsWith("@id/")) { 1584 | // System.out.println( 1585 | // " " + layout_name + ".setId(R.id." + value.substring(value.indexOf("/") + 1) + ");"); 1586 | // } else 1587 | // System.out.println(" " + layout_name + ".setId(" + value + ");"); 1588 | } else if (key.startsWith("android:")) { 1589 | System.out.println(""+layout_name+"."+key.substring(8)+" = "+value); 1590 | } 1591 | else if(key.startsWith("app:")){ 1592 | System.out.println(""+layout_name+"."+key.substring(4)+" = "+value); 1593 | } 1594 | else 1595 | System.out.println(" " + layout_name + "." + map.item(ii).getNodeName() + " = " 1596 | + map.item(ii).getNodeValue() + ";"); 1597 | } 1598 | 1599 | for (int n = 0; n < nodelist.getLength(); n++) { 1600 | Node nodeitem = nodelist.item(n); 1601 | 1602 | // System.out.println("node item = " + nodeitem.getNodeName() + 1603 | // " value=" + nodeitem.getNodeValue() + " type=" 1604 | // + nodeitem.getNodeType() + " "); 1605 | printiOSCode(element.getNodeName(), layout_name, nodeitem); 1606 | } 1607 | } 1608 | 1609 | if (node.getNodeType() == 1) { 1610 | Element element1 = (Element) node; 1611 | System.out.println(" " + name + ".addSubView(" + element1.getAttribute("name") + ");"); 1612 | } 1613 | } 1614 | 1615 | // 输出Swift代码 1616 | void printSwiftCode(Node node) { 1617 | 1618 | } 1619 | 1620 | // 输出kotlin代码 1621 | void printKotlinCode(Node node) { 1622 | 1623 | } 1624 | 1625 | public String toString(){ 1626 | return buf_code.toString(); 1627 | } 1628 | 1629 | } 1630 | -------------------------------------------------------------------------------- /src/main/java/com/xl/util/FileUtils.java: -------------------------------------------------------------------------------- 1 | package com.xl.util; 2 | import java.io.*; 3 | import java.util.*; 4 | 5 | public class FileUtils 6 | { 7 | /* 8 | * 列出目录下所有文件 9 | */ 10 | public static Collection listFiles(File file,String[] miniType,boolean ischeck) 11 | { 12 | ArrayList filelist = new ArrayList(); 13 | File[] files = file.listFiles(); 14 | if(files==null){ 15 | return filelist; 16 | } 17 | for(int i=0;i filelist2 = listFiles(files[i],miniType,ischeck); 31 | for(File f:filelist2) 32 | filelist.add(f); 33 | } 34 | } 35 | return filelist; 36 | } 37 | 38 | public static Collection listFiles(File file,boolean ischeck) 39 | { 40 | ArrayList filelist = new ArrayList(); 41 | File[] files = file.listFiles(); 42 | for(int i=0;i filelist2 = listFiles(files[i],ischeck); 51 | for(File f:filelist2) 52 | filelist.add(f); 53 | } 54 | } 55 | return filelist; 56 | } 57 | 58 | 59 | //批量删除目录下指定格式文件 60 | public static void removeFiles(File path,String[] name) 61 | { 62 | Collection files = listFiles(path,name,true); 63 | for(File file:files) 64 | { 65 | file.delete(); 66 | } 67 | } 68 | 69 | public static void writeText(String filename,String info,String coding) { 70 | File file = new File(filename); 71 | 72 | 73 | try 74 | { 75 | if (!file.isFile())file.createNewFile(); 76 | } 77 | catch (Exception e) 78 | {} 79 | try 80 | { 81 | FileOutputStream fileOutputStream = new FileOutputStream(file, false); 82 | fileOutputStream.write(info.getBytes(coding)); 83 | fileOutputStream.close(); 84 | } catch (FileNotFoundException e) { 85 | e.printStackTrace(); 86 | } catch (IOException e) { 87 | e.printStackTrace(); 88 | } 89 | 90 | } 91 | 92 | public static String read(File file,String encoding) throws IOException 93 | { 94 | String content = ""; 95 | // File file = new File(path); 96 | 97 | if(file.isFile()) 98 | { 99 | FileInputStream input= new FileInputStream(file); 100 | 101 | byte [] buf=new byte[input.available()]; 102 | input.read(buf); 103 | content = new String(buf,encoding); 104 | } 105 | return content; 106 | } 107 | 108 | 109 | } 110 | -------------------------------------------------------------------------------- /src/main/java/com/xl/util/FindViewByIdParser.java: -------------------------------------------------------------------------------- 1 | package com.xl.util; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.InputStream; 5 | import java.util.ArrayList; 6 | 7 | import javax.xml.parsers.DocumentBuilder; 8 | import javax.xml.parsers.DocumentBuilderFactory; 9 | 10 | import org.w3c.dom.Document; 11 | import org.w3c.dom.Element; 12 | import org.w3c.dom.NamedNodeMap; 13 | import org.w3c.dom.Node; 14 | 15 | import com.xl.game.math.Str; 16 | import com.xl.util.flutter.FlutterWidget; 17 | 18 | public class FindViewByIdParser { 19 | String text; 20 | StringBuffer buf_code; 21 | StringBuffer buf_var; 22 | 23 | public void setXmlText(String text) { 24 | this.text = text; 25 | } 26 | 27 | public void parse() { 28 | buf_code = new StringBuffer(); 29 | buf_var = new StringBuffer(); 30 | DocumentBuilderFactory factory = null; 31 | DocumentBuilder builder = null; 32 | Document document = null; 33 | InputStream inputStream = null; 34 | factory = DocumentBuilderFactory.newInstance(); 35 | try { 36 | // 惯例 取得document文件实例的过程 37 | builder = factory.newDocumentBuilder(); 38 | inputStream = new ByteArrayInputStream(text.getBytes("UTF-8")); 39 | ;// 以工程文件下assets文件夹为根目录 40 | document = builder.parse(inputStream); 41 | 42 | // 取得根Element 以此列出所有节点NodeList 43 | Element root = document.getDocumentElement(); 44 | if (root instanceof Node) { 45 | System.out.println("========"); 46 | 47 | String layout_root = "layout_root"; 48 | String className = root.getNodeName(); 49 | printFindCode(new FindClass(), root, 1); 50 | 51 | return; 52 | } 53 | 54 | } catch (Exception e) { 55 | e.printStackTrace(); 56 | } 57 | } 58 | 59 | // 遍历element输出findViewById 60 | void printFindCode(FindClass findClass, Element element, int leve) { 61 | 62 | String name = element.getNodeName(); 63 | if (name.indexOf(".") >= 0) { 64 | findClass.setName(name.substring(Str.strrchr(name, '.') + 1)); 65 | } else { 66 | findClass.setName(name); 67 | } 68 | findClass.setLeve(leve); 69 | 70 | NamedNodeMap map = element.getAttributes(); 71 | for (int ii = 0; ii < map.getLength(); ii++) { 72 | String key = map.item(ii).getNodeName(); 73 | String value = element.getAttribute(key); 74 | if (key.equals("android:id")) { 75 | 76 | if (value.indexOf("/") != -1) { 77 | value = value.substring(value.indexOf("/") + 1); 78 | } 79 | System.out.println(" " + name + " " + value + " = findViewById(R.id." + value + ");"); 80 | buf_var.append(" " + name + " " + value + ";" + "\n"); 81 | buf_code.append(" " + value + " = findViewById(R.id." + value + ");" + "\n"); 82 | } 83 | } 84 | 85 | ArrayList nodelist = XmlUtil.getChildElement(element); 86 | for (int n = 0; n < nodelist.size(); n++) { 87 | Element nodeitem = nodelist.get(n); 88 | 89 | printFindCode(findClass, nodeitem, leve + 1); 90 | } 91 | 92 | } 93 | 94 | public String toString() { 95 | if (buf_code != null && buf_var != null) { 96 | return buf_var.toString() + "\n" + buf_code.toString(); 97 | } 98 | 99 | return ""; 100 | } 101 | 102 | public class FindClass { 103 | String name; 104 | int leve; 105 | 106 | public void setName(String name) { 107 | this.name = name; 108 | } 109 | 110 | public String getName() { 111 | return this.name; 112 | } 113 | 114 | public void setLeve(int leve) { 115 | this.leve = leve; 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /src/main/java/com/xl/util/FlutterDomParser.java: -------------------------------------------------------------------------------- 1 | package com.xl.util; 2 | 3 | 4 | import java.awt.Paint; 5 | import java.awt.event.KeyEvent; 6 | import java.io.ByteArrayInputStream; 7 | import java.io.InputStream; 8 | import java.util.ArrayList; 9 | import java.util.Calendar; 10 | import java.util.HashMap; 11 | 12 | import javax.swing.plaf.basic.BasicBorders.MarginBorder; 13 | import javax.xml.XMLConstants; 14 | import javax.xml.parsers.DocumentBuilder; 15 | import javax.xml.parsers.DocumentBuilderFactory; 16 | 17 | import org.w3c.dom.DOMException; 18 | import org.w3c.dom.Document; 19 | import org.w3c.dom.Element; 20 | import org.w3c.dom.NamedNodeMap; 21 | import org.w3c.dom.Node; 22 | import org.w3c.dom.NodeList; 23 | import org.w3c.dom.UserDataHandler; 24 | 25 | import com.xl.game.math.Str; 26 | import com.xl.util.flutter.FlutterWidget; 27 | import com.xl.util.flutter.LinearLayout; 28 | import com.xl.util.flutter.Widget; 29 | 30 | 31 | 32 | public class FlutterDomParser { 33 | 34 | StringBuffer buf_code; 35 | 36 | String text = ""; 37 | FlutterWidget flutter_widget; 38 | 39 | 40 | 41 | public FlutterDomParser(){ 42 | buf_code = new StringBuffer(); 43 | } 44 | 45 | public void setXmlText(String text){ 46 | this.text = text; 47 | } 48 | 49 | 50 | 51 | 52 | 53 | public void parseFlutter() { 54 | DocumentBuilderFactory factory = null; 55 | DocumentBuilder builder = null; 56 | Document document = null; 57 | InputStream inputStream = null; 58 | factory = DocumentBuilderFactory.newInstance(); 59 | try { 60 | // 惯例 取得document文件实例的过程 61 | builder = factory.newDocumentBuilder(); 62 | inputStream = new ByteArrayInputStream(text.getBytes("UTF-8")); 63 | ;// 以工程文件下assets文件夹为根目录 64 | document = builder.parse(inputStream); 65 | 66 | // 取得根Element 以此列出所有节点NodeList 67 | Element root = document.getDocumentElement(); 68 | if (root instanceof Node) { 69 | System.out.println("========"); 70 | setNodesId(null,root); //初始化 71 | // printfNodes(root); 72 | String layout_root = "layout_root"; 73 | String className = root.getNodeName(); 74 | buf_code = new StringBuffer(); 75 | 76 | flutter_widget = new FlutterWidget(className); 77 | printFlutterCode(flutter_widget, root,1); 78 | System.out.println(flutter_widget.toString()); 79 | buf_code.append(flutter_widget.toString()); 80 | return; 81 | } 82 | 83 | 84 | } catch (Exception e) { 85 | // TODO Auto-generated catch block 86 | e.printStackTrace(); 87 | } 88 | } 89 | 90 | 91 | int count = 0; 92 | 93 | 94 | 95 | // TODO: 遍历node设置id 96 | void setNodesId(Element eleParent, Node node) { 97 | NodeList nodelist = node.getChildNodes(); 98 | // System.out.println("node name=" + node.getNodeName() + " value=" + 99 | // node.getNodeValue() + " type=" + node.getNodeType()); 100 | if (node.getNodeType() == 1) { 101 | printfAttributes(node); 102 | NamedNodeMap map = node.getAttributes(); 103 | 104 | // node.setNamedItem(nodetag); 105 | if (node instanceof Element) { 106 | System.out.println("is Element"); 107 | Element element = (Element) node; 108 | String id = node.getNodeName(); 109 | if(id.indexOf('.')>0){ 110 | id = id.substring(Str.strrchr(id, '.')); 111 | } 112 | String view_name = id.toLowerCase() + "_" + count; 113 | String key_id = element.getAttribute("android:id"); 114 | if(key_id.length()!=0){ 115 | if(key_id.startsWith("@id/") || key_id.startsWith("@+id/")){ 116 | view_name = key_id.substring(key_id.indexOf('/')+1); 117 | } 118 | } 119 | ((Element) node).setAttribute("id", id.toLowerCase() + "_" + count); 120 | element.setAttribute("name", view_name); 121 | ((Element) node).setAttribute("layoutparams", "layoutParams_" + count); 122 | count++; 123 | 124 | String layout_weight = element.getAttribute("android:layout_weight"); 125 | if(layout_weight.length()!=0){ 126 | 127 | String orientation = eleParent.getAttribute("android:orientation"); 128 | Float X = 100f; 129 | //获取父控件之下的所有子控件 130 | NodeList views = eleParent.getChildNodes(); 131 | ArrayList list_weight = new ArrayList(); 132 | ArrayList list_width = new ArrayList(); 133 | ArrayList list_height = new ArrayList(); 134 | int type = 0; //根据控件的 layout_width 和 layout_height 判断权重的计算方式 135 | if(orientation.length()!=0){ 136 | if(orientation.equals("horizontal")){ 137 | System.out.println("horizontal"); 138 | // element.setAttribute("width_percentage", ); 139 | int fill_index = -1; //第一次出现fill的控件index 140 | int ele_index = 0; 141 | System.out.println("======== "+views.getLength()); 142 | for(int ii=0;ii list_child = XmlUtil.getChildElement(eleParent); 235 | for(int ii=0;ii list_child = XmlUtil.getChildElement(eleParent); 250 | for(int ii=0;ii 0 && zero_num > 0){ 259 | type = 2; 260 | // 第一个控件的宽度为 X+(1/(1+2+2))0=X 261 | // 第二个控件的宽度为 0+(2/(1+2+2))0=0 262 | // 第三个控件的宽度为 0+(2/(1+2+2))*0=0 263 | System.out.println(">>>>>>>>>>> 算法3 "+fill_index); 264 | if(fill_index>=0){ 265 | ArrayList list_child = XmlUtil.getChildElement(eleParent); 266 | for(int ii=0;ii>>>>>>>>> fill_index<0"); 280 | } 281 | } 282 | else{ 283 | 284 | } 285 | 286 | 287 | } 288 | else if(orientation.equals("vertical")){ 289 | // element.setAttribute("height_percentage", value); 290 | int fill_index = -1; //第一次出现fill的控件index 291 | ArrayList list_views = XmlUtil.getChildElement(eleParent); 292 | for(int ii=0;ii 0 && zero_num > 0){ 348 | type = 2; 349 | // 第一个控件的宽度为 X+(1/(1+2+2))0=X 350 | // 第二个控件的宽度为 0+(2/(1+2+2))0=0 351 | // 第三个控件的宽度为 0+(2/(1+2+2))*0=0 352 | if(fill_index>=0){ 353 | for(int ii=0;ii=0){ 416 | widget.setWidgetName(name.substring(Str.strrchr(name, '.')+1)); 417 | } 418 | else{ 419 | widget.setWidgetName(name); 420 | } 421 | widget.setLeve(leve); 422 | 423 | 424 | NamedNodeMap map = element.getAttributes(); 425 | for (int ii = 0; ii < map.getLength(); ii++) { 426 | String key = map.item(ii).getNodeName(); 427 | String value = element.getAttribute(key); 428 | widget.addParamItem(key, value); 429 | } 430 | 431 | ArrayList nodelist = XmlUtil.getChildElement(element); 432 | for (int n = 0; n < nodelist.size(); n++) { 433 | Element nodeitem = nodelist.get(n); 434 | FlutterWidget child = new FlutterWidget(element.getNodeName()); 435 | widget.addChild(child); 436 | printFlutterCode(child, nodeitem,leve+1); 437 | } 438 | 439 | 440 | 441 | } 442 | 443 | // 输出Swift代码 444 | void printSwiftCode(Node node) { 445 | 446 | } 447 | 448 | // 输出kotlin代码 449 | void printKotlinCode(Node node) { 450 | 451 | } 452 | 453 | public String toString(){ 454 | return buf_code.toString(); 455 | } 456 | 457 | } 458 | 459 | -------------------------------------------------------------------------------- /src/main/java/com/xl/util/SwiftDomParser.java: -------------------------------------------------------------------------------- 1 | package com.xl.util; 2 | 3 | 4 | import java.awt.Paint; 5 | import java.awt.event.KeyEvent; 6 | import java.io.ByteArrayInputStream; 7 | import java.io.InputStream; 8 | import java.util.ArrayList; 9 | import java.util.Calendar; 10 | import java.util.HashMap; 11 | 12 | import javax.swing.plaf.basic.BasicBorders.MarginBorder; 13 | import javax.xml.XMLConstants; 14 | import javax.xml.parsers.DocumentBuilder; 15 | import javax.xml.parsers.DocumentBuilderFactory; 16 | 17 | import org.w3c.dom.DOMException; 18 | import org.w3c.dom.Document; 19 | import org.w3c.dom.Element; 20 | import org.w3c.dom.NamedNodeMap; 21 | import org.w3c.dom.Node; 22 | import org.w3c.dom.NodeList; 23 | import org.w3c.dom.UserDataHandler; 24 | 25 | import com.xl.game.math.Str; 26 | 27 | 28 | 29 | public class SwiftDomParser { 30 | 31 | StringBuffer buf_code; 32 | 33 | String text = 34 | "\n" 35 | +"\n" 39 | +" \n" 43 | +" \n" 46 | +" \n" 53 | +" \n" 58 | +" \n" 63 | +" \n" 70 | +"\n" 71 | +" \n" 72 | +"\n" 73 | +" \n" 77 | +"\n" 78 | +"\n" 79 | +" \n" 80 | +"\n" 81 | +" \n" 93 | +" \n" 101 | +"\n" 102 | +" \n" 103 | +"\n" 104 | +"\n" 105 | +"\n" 106 | +" \n" 107 | +" \n" 111 | +"\n" 112 | +" \n" 117 | +"\n" 118 | +" \n" 119 | +"\n" 120 | +" \n" 127 | +"\n" 128 | +" \n" 135 | +"\n" 136 | +" \n" 141 | +"\n" 142 | +" \n" 149 | +"\n" 150 | +" \n" 151 | +"\n" 152 | +" \n" 160 | +"\n" 161 | +" \n" 166 | +"\n" 167 | +" \n" 174 | +"\n" 175 | +" \n" 176 | +"\n" 177 | +" \n" 178 | +" \n" 179 | +"\n" 180 | +"\n"; 181 | 182 | 183 | 184 | public SwiftDomParser(){ 185 | buf_code = new StringBuffer(); 186 | } 187 | 188 | public void setXmlText(String text){ 189 | this.text = text; 190 | } 191 | 192 | 193 | 194 | 195 | 196 | public void parseSwift() { 197 | DocumentBuilderFactory factory = null; 198 | DocumentBuilder builder = null; 199 | Document document = null; 200 | InputStream inputStream = null; 201 | factory = DocumentBuilderFactory.newInstance(); 202 | try { 203 | // 惯例 取得document文件实例的过程 204 | builder = factory.newDocumentBuilder(); 205 | inputStream = new ByteArrayInputStream(text.getBytes("UTF-8")); 206 | ;// 以工程文件下assets文件夹为根目录 207 | document = builder.parse(inputStream); 208 | 209 | // 取得根Element 以此列出所有节点NodeList 210 | Element root = document.getDocumentElement(); 211 | if (root instanceof Node) { 212 | System.out.println("========"); 213 | setNodesId(null,root); //初始化 214 | // printfNodes(root); 215 | String layout_root = "layout_root"; 216 | String className = root.getNodeName(); 217 | buf_code = new StringBuffer(); 218 | String swiftClassName = getLayoutName(className); 219 | if(className.equals("LinearLayout")){ 220 | String orientation = root.getAttribute("android:orientation"); 221 | if(orientation.equals("vertical")){ 222 | orientation = ".vert"; 223 | } 224 | else if(orientation.equals("horizontal")){ 225 | orientation = ".horz"; 226 | } 227 | else{ 228 | orientation = ".horz"; 229 | } 230 | 231 | buf_code.append(" var "+layout_root+":"+swiftClassName + " = "+swiftClassName+"("+orientation+")\n"); 232 | } 233 | else 234 | buf_code.append(" var "+layout_root+":"+swiftClassName + " = "+swiftClassName+"("+")\n"); 235 | 236 | String layout_width = root.getAttribute("android:layout_width"); 237 | String layout_height = root.getAttribute("android:layout_height"); 238 | 239 | 240 | if (layout_width.equals("match_parent") || layout_width.equals("fill_parent")) { 241 | layout_width = ".fill"; 242 | } 243 | else if (layout_width.equals("wrap_content")) { 244 | layout_width = ".wrap"; 245 | } 246 | else { 247 | layout_width = XmlUtil.getSize(layout_width); 248 | } 249 | 250 | if (layout_height.equals("match_parent") || layout_height.equals("fill_parent")) { 251 | layout_height = ".fill"; 252 | } 253 | else if (layout_height.equals("wrap_content")) { 254 | layout_height = ".wrap"; 255 | } 256 | else { 257 | layout_height = XmlUtil.getSize(layout_height); 258 | } 259 | buf_code.append(" "+layout_root+".tg_width ~= "+layout_width+"\n"); 260 | buf_code.append(" "+layout_root+".tg_height ~= "+layout_height+"\n"); 261 | 262 | printiOSCode(className, layout_root, root); 263 | return; 264 | } 265 | 266 | 267 | } catch (Exception e) { 268 | // TODO Auto-generated catch block 269 | e.printStackTrace(); 270 | } 271 | } 272 | 273 | 274 | int count = 0; 275 | 276 | 277 | 278 | // TODO: 遍历node设置id 279 | void setNodesId(Element eleParent, Node node) { 280 | NodeList nodelist = node.getChildNodes(); 281 | // System.out.println("node name=" + node.getNodeName() + " value=" + 282 | // node.getNodeValue() + " type=" + node.getNodeType()); 283 | if (node.getNodeType() == 1) { 284 | printfAttributes(node); 285 | NamedNodeMap map = node.getAttributes(); 286 | 287 | // node.setNamedItem(nodetag); 288 | if (node instanceof Element) { 289 | System.out.println("is Element"); 290 | Element element = (Element) node; 291 | String id = node.getNodeName(); 292 | if(id.indexOf('.')>0){ 293 | id = id.substring(Str.strrchr(id, '.')); 294 | } 295 | String view_name = id.toLowerCase() + "_" + count; 296 | String key_id = element.getAttribute("android:id"); 297 | if(key_id.length()!=0){ 298 | if(key_id.startsWith("@id/") || key_id.startsWith("@+id/")){ 299 | view_name = key_id.substring(key_id.indexOf('/')+1); 300 | } 301 | } 302 | ((Element) node).setAttribute("id", id.toLowerCase() + "_" + count); 303 | element.setAttribute("name", view_name); 304 | ((Element) node).setAttribute("layoutparams", "layoutParams_" + count); 305 | count++; 306 | 307 | String layout_weight = element.getAttribute("android:layout_weight"); 308 | if(layout_weight.length()!=0){ 309 | 310 | String orientation = eleParent.getAttribute("android:orientation"); 311 | Float X = 100f; 312 | //获取父控件之下的所有子控件 313 | NodeList views = eleParent.getChildNodes(); 314 | ArrayList list_weight = new ArrayList(); 315 | ArrayList list_width = new ArrayList(); 316 | ArrayList list_height = new ArrayList(); 317 | int type = 0; //根据控件的 layout_width 和 layout_height 判断权重的计算方式 318 | if(orientation.length()!=0){ 319 | if(orientation.equals("horizontal")){ 320 | System.out.println("horizontal"); 321 | // element.setAttribute("width_percentage", ); 322 | int fill_index = -1; //第一次出现fill的控件index 323 | int ele_index = 0; 324 | System.out.println("======== "+views.getLength()); 325 | for(int ii=0;ii list_child = XmlUtil.getChildElement(eleParent); 418 | for(int ii=0;ii list_child = XmlUtil.getChildElement(eleParent); 433 | for(int ii=0;ii 0 && zero_num > 0){ 442 | type = 2; 443 | // 第一个控件的宽度为 X+(1/(1+2+2))0=X 444 | // 第二个控件的宽度为 0+(2/(1+2+2))0=0 445 | // 第三个控件的宽度为 0+(2/(1+2+2))*0=0 446 | System.out.println(">>>>>>>>>>> 算法3 "+fill_index); 447 | if(fill_index>=0){ 448 | ArrayList list_child = XmlUtil.getChildElement(eleParent); 449 | for(int ii=0;ii>>>>>>>>> fill_index<0"); 463 | } 464 | } 465 | else{ 466 | 467 | } 468 | 469 | 470 | } 471 | else if(orientation.equals("vertical")){ 472 | // element.setAttribute("height_percentage", value); 473 | int fill_index = -1; //第一次出现fill的控件index 474 | ArrayList list_views = XmlUtil.getChildElement(eleParent); 475 | for(int ii=0;ii 0 && zero_num > 0){ 531 | type = 2; 532 | // 第一个控件的宽度为 X+(1/(1+2+2))0=X 533 | // 第二个控件的宽度为 0+(2/(1+2+2))0=0 534 | // 第三个控件的宽度为 0+(2/(1+2+2))*0=0 535 | if(fill_index>=0){ 536 | for(int ii=0;ii0){ 628 | return nodeName.substring(Str.strrchr(nodeName, '.')+1); 629 | } 630 | return nodeName; 631 | } 632 | } 633 | 634 | // 输出 ios swift 代码 635 | void printiOSCode(String className, String name, Node node) { 636 | NodeList nodelist = node.getChildNodes(); 637 | // System.out.println("node name=" + node.getNodeName() + " value=" + 638 | // node.getNodeValue() + " type=" + node.getNodeType()); 639 | if (node.getNodeType() == 1) { 640 | // 输出代码 641 | Element element = (Element) node; 642 | 643 | String layout_name = element.getAttribute("name"); 644 | String nodeName = element.getNodeName(); 645 | // if(nodeName.equals("LinearLayout")){ 646 | // nodeName = "TGLinearLayout"; 647 | // } 648 | // if(nodeName.equals("Button")){ 649 | // nodeName = "UIButton"; 650 | // } 651 | // if(nodeName.equals("EditText")){ 652 | // nodeName = "UITextField"; 653 | // } 654 | // if(nodeName.equals("ImageView")){ 655 | // nodeName = "UIImageView"; 656 | // } 657 | //优先处理margin top layout_width layout_height layout_weight 658 | String layout_width = element.getAttribute("android:layout_width"); 659 | String layout_height = element.getAttribute("android:layout_height"); 660 | // String layout_weight = element.getAttribute("android:layout_weight"); 661 | String orientation = element.getAttribute("android:orientation"); 662 | if (layout_width.equals("match_parent") || layout_width.equals("fill_parent")) { 663 | layout_width = ".fill"; 664 | } 665 | else if (layout_width.equals("wrap_content")) { 666 | layout_width = ".wrap"; 667 | } 668 | else { 669 | layout_width = XmlUtil.getSize(layout_width); 670 | } 671 | 672 | if (layout_height.equals("match_parent") || layout_height.equals("fill_parent")) { 673 | layout_height = ".fill"; 674 | } 675 | else if (layout_height.equals("wrap_content")) { 676 | layout_height = ".wrap"; 677 | } 678 | else { 679 | layout_height = XmlUtil.getSize(layout_height); 680 | } 681 | if(orientation.equals("vertical")){ 682 | orientation = ".vert"; 683 | } 684 | else if(orientation.equals("horizontal")){ 685 | orientation = ".horz"; 686 | } 687 | 688 | 689 | 690 | 691 | //创建layout 692 | if(nodeName.equals("LinearLayout")){ 693 | buf_code.append(" var " + layout_name + ":" + "TGLinearLayout" + " = " 694 | + "TGLinearLayout" + "(" + orientation+")\n"); 695 | } 696 | else if(nodeName.equals("FrameLayout")){ 697 | buf_code.append(" var " + layout_name + ":" + "TGFrameLayout" + " = " 698 | + "TGFrameLayout" + "(" + orientation+")\n"); 699 | } 700 | else if(nodeName.equals("ImageView")){ 701 | buf_code.append(" var " + layout_name + ":" + "UIImageView" + " = " 702 | + "UIImageView" + "(" +")\n"); 703 | } 704 | else { 705 | buf_code.append(" var " + layout_name + ":" + getLayoutName(nodeName) + " = " 706 | + getLayoutName(nodeName) + "(" +")\n"); 707 | } 708 | if(layout_width.indexOf("%")>0){ 709 | buf_code.append(" " + layout_name + ".tg_width ~= "+layout_width+"\n"); 710 | } 711 | else{ 712 | buf_code.append(" " + layout_name + ".tg_width.equal("+layout_width+")\n"); 713 | } 714 | if(layout_height.indexOf("%")>0){ 715 | buf_code.append(" " + layout_name + ".tg_height ~= "+layout_height+"\n"); 716 | } 717 | else{ 718 | buf_code.append(" " + layout_name + ".tg_height.equal("+layout_height+")\n"); 719 | } 720 | 721 | // buf_code.append(" " + layout_name + ".weight = "+layout_weight+"\n"); 722 | 723 | 724 | String margin = element.getAttribute("android:layout_margin"); 725 | String margin_top = element.getAttribute("android:layout_marginTop"); 726 | String margin_bottom = element.getAttribute("android:layout_marginBottom"); 727 | String margin_left = element.getAttribute("android:layout_marginLeft"); 728 | String margin_right = element.getAttribute("android:layout_marginRight"); 729 | if(margin.length()!=0){ 730 | margin_top = margin; 731 | margin_left = margin; 732 | margin_right = margin; 733 | margin_bottom = margin; 734 | } 735 | if(margin_left.length()!=0 || margin_top.length()!=0 || margin_right.length()!=0 || margin_bottom.length()!=0){ 736 | margin_left = XmlUtil.getSize(margin_left); 737 | margin_top = XmlUtil.getSize(margin_top); 738 | margin_right = XmlUtil.getSize(margin_right); 739 | margin_bottom = XmlUtil.getSize(margin_bottom); 740 | buf_code.append(" "+layout_name+".tg_top.equal( "+margin_top+")\n"); 741 | buf_code.append(" "+layout_name+".tg_bottom.equal( "+margin_bottom+")\n"); 742 | buf_code.append(" "+layout_name+".tg_left.equal( "+margin_left+")\n"); 743 | buf_code.append(" "+layout_name+".tg_right.equal( "+margin_right+")\n"); 744 | } 745 | 746 | String padding = element.getAttribute("android:padding"); 747 | String padding_left = element.getAttribute("android:paddingLeft"); 748 | String padding_right = element.getAttribute("android:paddingRight"); 749 | String padding_top = element.getAttribute("android:paddingTop"); 750 | String padding_bottom = element.getAttribute("android:paddingBottom"); 751 | if(padding.length()!=0){ 752 | padding_left = padding; 753 | padding_top = padding; 754 | padding_right = padding; 755 | padding_bottom = padding; 756 | } 757 | if(padding_left.length()!=0 || padding_top.length()!=0 || padding_bottom.length()!=0 || padding_right.length()!=0){ 758 | padding_left = XmlUtil.getSize(padding_left); 759 | padding_top = XmlUtil.getSize(padding_top); 760 | padding_right = XmlUtil.getSize(padding_right); 761 | padding_bottom = XmlUtil.getSize(padding_bottom); 762 | buf_code.append(" "+layout_name+".tg_padding = UIEdgeInsets(top:CGFloat("+padding_top+"), left:CGFloat("+padding_left+"), bottom:CGFloat("+padding_bottom+"), right:CGFloat("+padding_right+"))\n"); 763 | 764 | } 765 | 766 | NamedNodeMap map = node.getAttributes(); 767 | for (int ii = 0; ii < map.getLength(); ii++) { 768 | String key = map.item(ii).getNodeName(); 769 | String value = element.getAttribute(key); 770 | 771 | if (key.equals("android:layout_width")) { 772 | } 773 | else if(key.indexOf("xmlns:")>=0){ 774 | 775 | } 776 | else if(key.equals("android:layout_margin")){ 777 | } 778 | else if(key.equals("android:layout_marginTop")){ 779 | 780 | } 781 | else if(key.equals("android:layout_marginBottom")){ 782 | 783 | } 784 | else if(key.equals("android:layout_marginLeft")){ 785 | 786 | } 787 | else if(key.equals("android:layout_marginRight")){ 788 | 789 | } 790 | else if (key.equals("android:layout_height")) { 791 | 792 | } else if (key.equals("android:theme")) { 793 | 794 | } else if (key.equals("android:background")) { 795 | if(value.startsWith("#")){ 796 | value = XmlUtil.getColorHex(value); 797 | buf_code.append(" "+layout_name+".layer.backgroundColor = ColorUtil.getCGColor("+value+")\n"); 798 | } 799 | else if(value.startsWith("@color/")){ 800 | buf_code.append(" "+layout_name+".layer.backgroundColor = ColorUtil.getCGColor(\""+value.substring(7)+"\")\n"); 801 | } 802 | else if(value.startsWith("@drawable/")){ 803 | buf_code.append(" "+layout_name+".layer.contents = UIImage(named: \""+value.substring(10)+"\")!.cgImage\n"); 804 | } 805 | else if(value.startsWith("@mipmap/")){ 806 | buf_code.append(" "+layout_name+".layer.contents = UIImage(named: \""+value.substring(8)+"\")!.cgImage\n"); 807 | } 808 | } else if (key.equals("android:orientation")) { 809 | 810 | } else if (key.equals("android:src")) { 811 | if(value.startsWith("@drawable/")){ 812 | value = ""+value.substring(10); 813 | } 814 | else if(value.startsWith("@mipmap/")){ 815 | value = ""+value.substring(8); 816 | } 817 | buf_code.append(" "+layout_name+".image = UIImage(named:\""+value+"\")\n"); 818 | 819 | } else if (key.equals("android:text")) { 820 | if(value.startsWith("@string/")){ 821 | value = XmlUtil.getiOSString(value); 822 | if(nodeName.equals("Button")) { 823 | buf_code.append(" "+layout_name+".setTitle(\"" + value + "\", for: UIControl.State.normal)"+"\n"); 824 | } 825 | else 826 | buf_code.append(" "+layout_name+".text = "+value.substring(8)+"\n"); 827 | } 828 | else{ 829 | if(nodeName.equals("Button")) { 830 | buf_code.append(" "+layout_name+".setTitle(\"" + value + "\", for: UIControl.State.normal)"+"\n"); 831 | } 832 | else 833 | buf_code.append(" "+layout_name+".text = \""+value+"\"\n"); 834 | } 835 | } else if (key.equals("android:textColor")) { 836 | if (value.startsWith("#")) { 837 | buf_code.append(" " + layout_name + ".textColor = ColorUtil.hexToUIColor(" + value + ")\n"); 838 | } 839 | else if(value.startsWith("@color/")){ 840 | buf_code.append(" " + layout_name + ".textColor = ColorUtil.getColor(\"" + value.substring(7) + "\")\n"); 841 | } 842 | else 843 | buf_code.append(" " + layout_name + ".textColor = " + value + "\n"); 844 | } 845 | else if(key.equals("android:textSize")){ 846 | if(value.indexOf("sp")>0){ 847 | buf_code.append(" "+layout_name+".font = UIFont.systemFont(ofSize: "+Str.atoi(value)+")\n"); 848 | } 849 | else{ 850 | buf_code.append(" "+layout_name+".font = UIFont.systemFont(ofSize: CGFloat("+ XmlUtil.getiOSFontSize(value) +"))\n"); 851 | } 852 | 853 | } 854 | else if(key.equals("android:ellipsize")){ 855 | String elipsize = value; 856 | if(elipsize.equals("end")){ 857 | elipsize = "NSLineBreakMode.byTruncatingHead"; 858 | } 859 | else if(elipsize.equals("start")){ 860 | elipsize = "NSLineBreakMode.byTruncatingHead"; 861 | } 862 | else if(elipsize.equals("moddle")){ 863 | elipsize = "NSLineBreakMode.byTruncatingMiddle"; 864 | } 865 | else if(elipsize.equals("marquee")){ 866 | elipsize = "MSLineBreakMode.byWordWrapping"; 867 | } 868 | buf_code.append(" "+layout_name+".lineBreakMode = "+elipsize+"\n"); 869 | } 870 | else if (key.equals("android:layout_weight")) { 871 | System.out.println("layout_weight = "+value); 872 | Element eleParent = (Element) element.getParentNode(); 873 | String orien = eleParent.getAttribute("android:orientation"); 874 | if(orien.equals("horizontal")){ 875 | System.out.println("orien is horizontal"); 876 | String percentage = element.getAttribute("width_percentage"); 877 | if(percentage.length()!=0){ 878 | buf_code.append(" "+layout_name + ".tg_width ~= "+percentage+"%\n"); 879 | } 880 | else{ 881 | System.out.println("width_percentage is null"); 882 | } 883 | } 884 | else if(orien.equals("vertical")){ 885 | System.out.println("orien is vertical"); 886 | String percentage = element.getAttribute("height_percentage"); 887 | if(percentage.length()!=0){ 888 | buf_code.append(" "+layout_name + "tg_height ~= "+percentage+"%\n"); 889 | } 890 | } 891 | else{ 892 | System.out.print("orientation 未找到"+value); 893 | } 894 | } 895 | //date|textUri|textShortMessage|textAutoCorrect|none|numberSigned|textVisiblePassword|textWebEditText|textMultiLine|textNoSuggestions|textCapSentences| 896 | //textAutoComplete|textImeMultiLine|numberDecimal 897 | else if (key.equals("android:inputType")) { 898 | // String items[] = value.split("\\|"); 899 | // ArrayList list_items = new ArrayList(); 900 | // for (String item : items) 901 | // list_items.add(item); 902 | // // System.out.println("........................."+items+ " 903 | // // "+list_items); 904 | // value = ""; 905 | // if(list_items.contains("date")){ 906 | // value += "|UIKeybordType.asciiCapable"; 907 | // } 908 | // if(list_items.contains("textUri")){ 909 | // value += "|UIKeybordType.URL"; 910 | // } 911 | // if(list_items.contains("textShortMessage")){ 912 | // value += "|default"; 913 | // } 914 | // if(list_items.contains("textAutoCorrect")){ 915 | // value += "|default"; 916 | // } 917 | // if(list_items.contains("none")){ 918 | // value += "|default"; 919 | // } 920 | // if(list_items.contains("numberSigned")){ 921 | // value += "|numberPad"; 922 | // } 923 | // if(list_items.contains("textVisiblePassword")){ 924 | // value += "|"; 925 | // } 926 | // if(list_items.contains("textWebEditText")){ 927 | // value += "|"; 928 | // } 929 | // if(list_items.contains("textNoSuggestions")){ 930 | // value += "|"; 931 | // } 932 | // if(list_items.contains("textCapSentences")){ 933 | // value += "|"; 934 | // } 935 | // if(list_items.contains("textImeMultiLine")){ 936 | // value += "|"; 937 | // } 938 | // 939 | // if(list_items.contains("numberDecimal")){ 940 | // value += "|"; 941 | // } 942 | // if (list_items.contains("text")) { 943 | // value += "|default"; 944 | // } 945 | // if (list_items.contains("number")) { 946 | // value += "|numberPad"; 947 | // } 948 | // if (list_items.contains("textCapCharacters")) { 949 | // value += "|"; 950 | // } 951 | // if (list_items.contains("textMultiLine")) { 952 | // value += "|"; 953 | // } 954 | // if(list_items.contains("textPassword")){ 955 | // value += "|"; 956 | // } 957 | // if(value.length()>0){ 958 | // value = value.substring(1); 959 | // } 960 | // System.out.println(" " + layout_name + ".setInputType(" + value + ");"); 961 | } else if (key.equals("android:typeface")) { 962 | // if (value.equals("monospace")) { 963 | // value = "Typeface.MONOSPACE"; 964 | // } else if (value.equals("serif")) { 965 | // value = "Typeface.SERIF"; 966 | // } else if (value.equals("bold")) { 967 | // value = "Typeface.DEFAULT_BOLD"; 968 | // } 969 | // else if(value.equals("sans")) 970 | // { 971 | // value = "Typeface.SANS_SERIF"; 972 | // } 973 | // 974 | // System.out.println(" " + layout_name + ".setTypeface(" + value + ");"); 975 | } 976 | else if(key.equals("android:gravity")){ 977 | String gravity_hor = ""; 978 | String gravity_ver = ""; 979 | if(nodeName.equals("TextView") || nodeName.equals("EditText")){ 980 | if(value.indexOf("left")>=0){ 981 | buf_code.append(" "+layout_name+".textAlignment = "+".left\n"); 982 | } 983 | if(value.indexOf("right")>=0){ 984 | buf_code.append(" "+layout_name+".textAlignment = "+".right\n"); 985 | } 986 | 987 | if(value.indexOf("top")>=0){ 988 | buf_code.append(" "+layout_name+".textAlignment = "+".top\n"); 989 | } 990 | if(value.indexOf("bottom")>=0){ 991 | buf_code.append(" "+layout_name+".textAlignment = "+".bottom\n"); 992 | } 993 | 994 | if(value.indexOf("center")>=0){ 995 | buf_code.append(" "+layout_name+".textAlignment=.center\n"); 996 | } 997 | } 998 | else{ 999 | if(value.indexOf("center")>=0){ 1000 | buf_code.append(" "+layout_name+".tg_gravity = TGGravity.center\n"); 1001 | } 1002 | else{ 1003 | if(value.indexOf("top")>=0) { 1004 | buf_code.append(""+layout_name+".tg_gravity = "+"TGGravity.top"+"\n"); 1005 | } 1006 | if(value.indexOf("bottom")>=0) { 1007 | buf_code.append(""+layout_name+".tg_gravity = "+"TGGravity.bottom"+"\n"); 1008 | } 1009 | if(value.indexOf("left")>=0) { 1010 | buf_code.append(""+layout_name+".tg_gravity = "+"TGGravity.left"+"\n"); 1011 | } 1012 | if(value.indexOf("right")>=0) { 1013 | buf_code.append(""+layout_name+".tg_gravity = "+"TGGravity.right"+"\n"); 1014 | } 1015 | // else { 1016 | // buf_code.append(""+layout_name+".tg_gravity = "+"TGGravity."+value); 1017 | // } 1018 | 1019 | } 1020 | } 1021 | 1022 | 1023 | 1024 | 1025 | 1026 | 1027 | 1028 | } 1029 | else if(key.equals("android:textAlignment")) { 1030 | if(value.indexOf("left")>=0){ 1031 | buf_code.append(" "+layout_name+".textAlignment = "+".left\n"); 1032 | } 1033 | if(value.indexOf("right")>=0){ 1034 | buf_code.append(" "+layout_name+".textAlignment = "+".right\n"); 1035 | } 1036 | 1037 | if(value.indexOf("top")>=0){ 1038 | buf_code.append(" "+layout_name+".textAlignment = "+".top\n"); 1039 | } 1040 | if(value.indexOf("bottom")>=0){ 1041 | buf_code.append(" "+layout_name+".textAlignment = "+".bottom\n"); 1042 | } 1043 | 1044 | if(value.indexOf("center")>=0){ 1045 | buf_code.append(" "+layout_name+".textAlignment = .center\n"); 1046 | } 1047 | } 1048 | else if(key.equals("android:layout_gravity")){ 1049 | String gravity_hor = ""; 1050 | String gravity_ver = ""; 1051 | boolean isLeft = false; 1052 | boolean isTop = false; 1053 | boolean isRight = false; 1054 | boolean isBottom = false; 1055 | if(value.indexOf("left")>=0){ 1056 | isLeft = true; 1057 | buf_code.append(" "+layout_name+".tg_left ~= 0\n"); 1058 | } 1059 | 1060 | if(value.indexOf("right")>=0){ 1061 | isRight = true; 1062 | buf_code.append(" "+layout_name+".tg_right ~= 0\n"); 1063 | } 1064 | 1065 | if(value.indexOf("top")>=0){ 1066 | isTop = true; 1067 | buf_code.append(" "+layout_name+".tg_top ~= 0\n"); 1068 | } 1069 | if(value.indexOf("bottom")>=0){ 1070 | isBottom = true; 1071 | buf_code.append(" "+layout_name+".tg_bottom ~= 0\n"); 1072 | } 1073 | 1074 | if(value.indexOf("center")>=0){ 1075 | if(isLeft || isRight){ 1076 | buf_code.append(" "+layout_name+".tg_centerY ~= 0\n"); 1077 | } 1078 | else if(isBottom || isTop){ 1079 | buf_code.append(" "+layout_name+".tg_centerX ~= 0\n"); 1080 | } 1081 | else { 1082 | buf_code.append(" "+layout_name+".tg_centerX ~= 0\n"); 1083 | buf_code.append(" "+layout_name+".tg_centerY ~= 0\n"); 1084 | } 1085 | 1086 | } 1087 | 1088 | 1089 | 1090 | 1091 | 1092 | 1093 | 1094 | } 1095 | else if(key.equals("android:scrollbars")){ 1096 | // if(value.equals("vertical")) 1097 | // { 1098 | // System.out.println(" " + layout_name+".setVerticalScrollBarEnabled(true);"); 1099 | // System.out.println(" "+layout_name + ".setScrollBarStyle(View.SCROLLBARS_OUTSIDE_INSET);"); 1100 | // } 1101 | // else if(value.equals("none")) 1102 | // { 1103 | // System.out.println(" "+ layout_name + ".setVerticalScrollBarEnabled(false);"); 1104 | // } 1105 | // else 1106 | // { 1107 | // System.out.println(" " + layout_name + ".setScrollBarStyle(view.SCROLLBARS_INSIDE_OVERLAY);"); 1108 | // } 1109 | } 1110 | else if(key.equals("android:minHeight")){ 1111 | value = XmlUtil.getSize(value); 1112 | buf_code.append(" "+layout_name+".tg_height.min.equal("+value+");\n"); 1113 | } 1114 | else if(key.equals("android:maxHeight")){ 1115 | value = XmlUtil.getSize(value); 1116 | buf_code.append(" "+layout_name+".tg_height.max.equal("+value+");\n"); 1117 | } 1118 | else if(key.equals("android:minWidth")){ 1119 | value = XmlUtil.getSize(value); 1120 | buf_code.append(" "+layout_name+".tg_width.min.equal("+value+");\n"); 1121 | } 1122 | else if(key.equals("android:maxWidth")){ 1123 | value = XmlUtil.getSize(value); 1124 | buf_code.append(" "+layout_name+".tg_width.max.equal("+value+");\n"); 1125 | } 1126 | else if(key.equals("android:elevation")){ 1127 | // value = XmlUtil.getSize(value); 1128 | // System.out.println(" "+layout_name+".setElevation("+value+");"); 1129 | } 1130 | else if(key.equals("android:shadowColor")){ 1131 | String shadowDx = element.getAttribute("android:shadowDx"); 1132 | String shadowDy = element.getAttribute("android:shadowDy"); 1133 | String shadowColor = value; 1134 | if(shadowColor.startsWith("#")){ 1135 | shadowColor = XmlUtil.getColorHex(shadowColor); 1136 | } 1137 | else if(shadowColor.startsWith("@color/")){ 1138 | shadowColor = "R.color."+shadowColor.substring(7); 1139 | } 1140 | buf_code.append(" "+layout_name+".layer.shadowColor = "+shadowColor+"\n"); 1141 | buf_code.append(" "+layout_name+".layer.shadowOffset = "+"CGSize(width:"+shadowDx+", height:"+shadowDy+")\n"); 1142 | } 1143 | else if(key.endsWith("android:scaleType")){ 1144 | if(value.equals("matrix")) 1145 | { 1146 | buf_code.append(" "+layout_name+".contentMode = UIView.ContentMode.center\n"); 1147 | } 1148 | else if(value.equals("fitXY")) 1149 | { 1150 | buf_code.append(" "+layout_name+".contentMode = UIView.ContentMode.scaleAspectFill\n"); 1151 | } 1152 | else if(value.equals("fitStart")) 1153 | { 1154 | buf_code.append(" "+layout_name+".contentMode = UIView.ContentMode.scaleAspectFill\n"); 1155 | 1156 | } 1157 | else if(value.equals("fitCenter")) 1158 | { 1159 | buf_code.append(" "+layout_name+".contentMode = UIView.ContentMode.center\n"); 1160 | 1161 | } 1162 | else if(value.equals("fitEnd")) 1163 | { 1164 | buf_code.append(" "+layout_name+".contentMode = UIView.ContentMode.center\n"); 1165 | 1166 | }else if(value.equals("center")) 1167 | { 1168 | buf_code.append(" "+layout_name+".contentMode = UIView.ContentMode.center\n"); 1169 | 1170 | } 1171 | else if(value.equals("centerCrop")) 1172 | { 1173 | buf_code.append(" "+layout_name+".contentMode = UIView.ContentMode.center\n"); 1174 | 1175 | } 1176 | else if(value.equals("centerInside")) 1177 | { 1178 | buf_code.append(" "+layout_name+".contentMode = UIView.ContentMode.center\n"); 1179 | 1180 | } 1181 | } 1182 | else if(key.equals("android:tint")){ 1183 | String color = value; 1184 | if(color.startsWith("#")){ 1185 | color = XmlUtil.getColorHex(color); 1186 | } 1187 | else if(color.startsWith("@color/")){ 1188 | color = "R.color."+color.substring(7); 1189 | } 1190 | buf_code.append(" "+layout_name+".tintColor = "+color+"\n"); 1191 | } 1192 | else if(key.equals("android:paddingTop")){ 1193 | 1194 | } 1195 | else if(key.equals("android:paddingBottom")){ 1196 | 1197 | } 1198 | else if(key.equals("android:paddingLeft")){ 1199 | 1200 | 1201 | } 1202 | else if(key.equals("android:paddingRight")){ 1203 | 1204 | 1205 | } 1206 | else if(key.equals("android:padding")){ 1207 | 1208 | } 1209 | else if(key.equals("android:alpha")){ 1210 | buf_code.append(" "+layout_name+".layer.alpha = "+value+"f\n"); 1211 | } 1212 | else if(key.equals("android:textStyle")){ 1213 | // if(value.equals("bold")) 1214 | // { 1215 | // System.out.println(" "+layout_name+".setTypeface("+layout_name+".getTypeface(), Typeface.BOLD);" ); 1216 | // } 1217 | // if(value.equals("italic")) 1218 | // { 1219 | // System.out.println(" "+layout_name+".setTypeface("+layout_name+".getTypeface(), Typeface.ITALIC);" ); 1220 | // } 1221 | // if(value.equals("bold_italic")){ 1222 | // System.out.println(" "+layout_name+".setTypeface("+layout_name+".getTypeface(), Typeface.BOLD_ITALIC);" ); 1223 | // } 1224 | // 1225 | } 1226 | else if(key.equals("android:selectable")) { 1227 | if(value.equals("true")) { 1228 | buf_code.append(" "+layout_name+".isSelectable = true\n"); 1229 | } 1230 | } 1231 | else if(key.equals("android:visibility")){ 1232 | 1233 | if(value.equals("visible")){ 1234 | buf_code.append(" "+layout_name+".hidden = false\n"); 1235 | } 1236 | 1237 | else if(value.equals("invisible")){ 1238 | buf_code.append(" "+layout_name+".hidden = true\n"); 1239 | } 1240 | 1241 | else if(value.equals("gone")){ 1242 | buf_code.append(" "+layout_name+".hidden = true\n"); 1243 | } 1244 | // 1245 | 1246 | } 1247 | else if(key.equals("android:singleLine")){ 1248 | if(value == "true"){ 1249 | buf_code.append(" "+layout_name+".numberOfLines = 1\n"); 1250 | } 1251 | else{ 1252 | buf_code.append(" "+layout_name+".lineBreakMode = NSLineBreakMode.ByWordWrapping\n"); 1253 | buf_code.append(" "+layout_name+".numberOfLines = 0\n"); 1254 | } 1255 | // System.out.println(" "+layout_name+".setSingleLine("+value+");"); 1256 | } 1257 | else if(key.equals("android:ems")){ 1258 | // System.out.println(" "+layout_name+".setEms("+XmlUtil.getSize(value)+");"); 1259 | } 1260 | else if(key.equals("android:lines")) 1261 | { 1262 | buf_code.append(" "+layout_name+".lineBreakMode = NSLineBreakMode.ByWordWrapping\n"); 1263 | buf_code.append(" "+layout_name+".numberOfLines = "+value+"\n"); 1264 | } 1265 | else if(key.equals("android:minLines")){ 1266 | // System.out.println(" "+layout_name+".setMinLines("+value+");"); 1267 | } 1268 | else if(key.equals("android:maxLines")){ 1269 | // System.out.println(" "+layout_name+".setMaxLines("+value+");"); 1270 | } 1271 | else if(key.equals("android:hint")) 1272 | { 1273 | if(value.startsWith("@string/")){ 1274 | value = XmlUtil.getString(value); 1275 | buf_code.append(" "+layout_name+".placeholder = "+value+"\n"); 1276 | } 1277 | else 1278 | buf_code.append(" "+layout_name+".placeholder = \""+value+"\"\n"); 1279 | } 1280 | else if(key.equals("android:drawable")){ 1281 | if(value.startsWith("@drawable/")){ 1282 | value = "R.drawable."+value.substring(10); 1283 | } 1284 | else if(value.startsWith("@mipmap/")){ 1285 | value = "R.mipmap."+value.substring(8); 1286 | } 1287 | buf_code.append(" "+layout_name+".setImage(UIImage(named:"+value+"),for: UIControl.State.normal)\n"); 1288 | } 1289 | else if(key.equals("name")){ 1290 | 1291 | } 1292 | else if (key.equals("id")) { 1293 | 1294 | } else if (key.equals("layoutparams")) { 1295 | 1296 | } else if (key.equals("xmlns:app")) { 1297 | 1298 | } else if (key.equals("xmlns:android")) { 1299 | 1300 | } else if (key.equals("xmlns:tools")) { 1301 | 1302 | } else if (key.equals("android:id")) { 1303 | // if (value.startsWith("@+id/") || value.startsWith("@id/")) { 1304 | // System.out.println( 1305 | // " " + layout_name + ".setId(R.id." + value.substring(value.indexOf("/") + 1) + ");"); 1306 | // } else 1307 | // System.out.println(" " + layout_name + ".setId(" + value + ");"); 1308 | } else if (key.startsWith("android:")) { 1309 | buf_code.append(""+layout_name+"."+key.substring(8)+" = "+value+"\n"); 1310 | } 1311 | else if(key.startsWith("app:")){ 1312 | buf_code.append(""+layout_name+"."+key.substring(4)+" = "+value+"\n"); 1313 | } 1314 | else 1315 | buf_code.append(" " + layout_name + "." + map.item(ii).getNodeName() + " = " 1316 | + map.item(ii).getNodeValue() + ";\n"); 1317 | } 1318 | 1319 | for (int n = 0; n < nodelist.getLength(); n++) { 1320 | Node nodeitem = nodelist.item(n); 1321 | 1322 | // System.out.println("node item = " + nodeitem.getNodeName() + 1323 | // " value=" + nodeitem.getNodeValue() + " type=" 1324 | // + nodeitem.getNodeType() + " "); 1325 | printiOSCode(element.getNodeName(), layout_name, nodeitem); 1326 | } 1327 | } 1328 | 1329 | if (node.getNodeType() == 1) { 1330 | Element element1 = (Element) node; 1331 | if(className.equals("ScrollView") || className.equals("HorizontalScrollView")) { 1332 | buf_code.append(" " + name + ".addView("+element1.getAttribute("name") + ")\n"); 1333 | } 1334 | else 1335 | buf_code.append(" " + name + ".addSubview(" + element1.getAttribute("name") + ")\n"); 1336 | } 1337 | } 1338 | 1339 | // 输出Swift代码 1340 | void printSwiftCode(Node node) { 1341 | 1342 | } 1343 | 1344 | // 输出kotlin代码 1345 | void printKotlinCode(Node node) { 1346 | 1347 | } 1348 | 1349 | public String toString(){ 1350 | return buf_code.toString(); 1351 | } 1352 | 1353 | } 1354 | -------------------------------------------------------------------------------- /src/main/java/com/xl/util/UIUtil.java: -------------------------------------------------------------------------------- 1 | package com.xl.util; 2 | 3 | import javax.swing.UIManager; 4 | import javax.swing.UnsupportedLookAndFeelException; 5 | 6 | public class UIUtil { 7 | 8 | //设置windows风格UI 9 | public static void setWindowsStyle(){ 10 | String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; 11 | try { 12 | UIManager.setLookAndFeel(lookAndFeel); 13 | } catch (ClassNotFoundException e) { 14 | // TODO Auto-generated catch block 15 | e.printStackTrace(); 16 | } catch (InstantiationException e) { 17 | // TODO Auto-generated catch block 18 | e.printStackTrace(); 19 | } catch (IllegalAccessException e) { 20 | // TODO Auto-generated catch block 21 | e.printStackTrace(); 22 | } catch (UnsupportedLookAndFeelException e) { 23 | // TODO Auto-generated catch block 24 | e.printStackTrace(); 25 | } 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/xl/util/XmlUtil.java: -------------------------------------------------------------------------------- 1 | package com.xl.util; 2 | import java.util.ArrayList; 3 | 4 | import org.w3c.dom.Element; 5 | import org.w3c.dom.Node; 6 | import org.w3c.dom.NodeList; 7 | 8 | import com.xl.game.math.Str; 9 | 10 | public class XmlUtil { 11 | 12 | //读取颜色值 13 | public static int getColor(String text) 14 | { 15 | int color=0; 16 | int argb[]=new int[4]; 17 | int start=0; 18 | int i=0; 19 | int hex=0; //颜色位数 有3 4 6 8 20 | for(i=0;i='A'&&c<='F') 36 | { 37 | argb[i+1]=(c-'A'+10)*16 + (c-'A'+10); 38 | } 39 | else if(c>='a'&&c<='f') 40 | { 41 | argb[i+1]=(c-'a'+10)*16 + (c-'a'+10); 42 | } 43 | else if(c>='0'&&c<='9') 44 | { 45 | argb[i+1]=(c-'0')*16 + (c-'0'); 46 | } 47 | } 48 | } 49 | else if(hex==6) 50 | { 51 | argb[0]=0xff; 52 | for(i=0;i<3;i++) 53 | { 54 | char c=text.charAt(start+i*2); 55 | char c2=text.charAt(start+i*2+1); 56 | 57 | if(c>='A'&&c<='F') 58 | { 59 | argb[i+1]=(c-'A'+10)<<4; 60 | } 61 | else if(c>='a'&&c<='f') 62 | { 63 | argb[i+1]=(c-'a'+10)<<4; 64 | } 65 | else if(c>='0'&&c<='9') 66 | { 67 | argb[i+1]=(c-'0')<<4; 68 | } 69 | if(c2>='A'&&c2<='F') 70 | { 71 | argb[i+1]|=(c2-'A'+10); 72 | } 73 | else if(c2>='a'&&c2<='f') 74 | { 75 | argb[i+1]|=(c2-'a'+10); 76 | } 77 | else if(c2>='0'&&c2<='9') 78 | { 79 | argb[i+1]|=(c2-'0'); 80 | } 81 | } 82 | } 83 | else if(hex==4) 84 | { 85 | for(i=0;i<4;i++) 86 | { 87 | char c=text.charAt(start+i); 88 | if(c>='A'&&c<='Z') 89 | { 90 | argb[i]=((c-'A')+10)*16 + ((c-'A')+10); 91 | } 92 | else if(c>='a'&&c<='z') 93 | { 94 | argb[i]=(c-'a'+10)*16 + (c-'a'+10); 95 | } 96 | else if(c>='0'&&c<='9') 97 | { 98 | argb[i]=(c-'0')*16 + (c-'0'); 99 | } 100 | } 101 | } 102 | else if(hex==8) 103 | { 104 | for(i=0;i<4;i++) 105 | { 106 | char c=text.charAt(start+i*2); 107 | char c2=text.charAt(start+i*2+1); 108 | if(c>='A'&&c<='F') 109 | { 110 | argb[i]=(c-'A'+10)<<4; 111 | } 112 | else if(c>='a'&&c<='f') 113 | { 114 | argb[i]=(c-'a'+10)<<4; 115 | } 116 | else if(c>='0'&&c<='9') 117 | { 118 | argb[i]=(c-'0')<<4; 119 | } 120 | if(c2>='A'&&c2<='F') 121 | { 122 | argb[i]|=(c2-'A'+10); 123 | } 124 | else if(c2>='a'&&c2<='f') 125 | { 126 | argb[i]|=(c2-'a'+10); 127 | } 128 | else if(c2>='0'&&c2<='9') 129 | { 130 | argb[i]|=(c2-'0'); 131 | } 132 | } 133 | } 134 | color=(argb[0]<<24)|(argb[1]<<16)|(argb[2]<<8)|argb[3]; 135 | 136 | return color; 137 | } 138 | 139 | 140 | public static String getColorHex(String text){ 141 | return String.format("0x%08x", getColor(text)); 142 | } 143 | 144 | //读取长度信息 145 | public static String getSize(String text) 146 | { 147 | if(text.startsWith("@dimen/")){ 148 | return "getResources().getDimensionPixelSize(R.dimen."+text.substring(Str.strrchr(text, '/')+1)+")"; 149 | } 150 | else if(text.endsWith("dp")||text.endsWith("dip")) 151 | { 152 | return "DisplayUtil.dip2px(context, Str.atoi(\""+text+"\"))"; 153 | } 154 | else if(text.endsWith("sp")) 155 | { 156 | return "DisplayUtil.sp2px(context, Str.atoi(\""+text+"\"))"; 157 | } 158 | else if(text.endsWith("px")) 159 | { 160 | return ""+Str.atoi(text); 161 | } 162 | return ""+Str.atoi(text); 163 | } 164 | 165 | //读取字号 166 | public static String getFontSize(String text) 167 | { 168 | if(text.startsWith("@dimen/")){ 169 | return "DisplayUtil.px2sp(context,getResources().getDimension(R.dimen."+text.substring(Str.strrchr(text, '/')+1)+"))"; 170 | } 171 | else if(text.endsWith("dp")||text.endsWith("dip")) 172 | { 173 | return "DisplayUtil.dip2sp(context, Str.atoi(\""+text+"\"))"; 174 | } 175 | else if(text.endsWith("sp")) 176 | { 177 | return "Str.atoi(\""+text+"\")"; 178 | } 179 | else if(text.endsWith("px")) 180 | { 181 | return "DisplayUtil.px2sp(context, Str.atoi(\""+text+"\"))"; 182 | } 183 | return "Str.atoi(\""+text+"\")"; 184 | } 185 | 186 | //读取字号 187 | public static String getiOSFontSize(String text) 188 | { 189 | if (text.startsWith("@dimen/")) { 190 | return "DisplayUtil.px2sp(context,DimenUtil.getDimen(\"" 191 | + text.substring(Str.strrchr(text, '/') + 1) + "\"))"; 192 | } else if (text.endsWith("dp") || text.endsWith("dip")) { 193 | return "DisplayUtil.dip2sp(context, Str.atoi(\"" + text + "\"))"; 194 | } else if (text.endsWith("sp")) { 195 | return "Str.atoi(\"" + text + "\")"; 196 | } else if (text.endsWith("px")) { 197 | return "DisplayUtil.px2sp(context, Str.atoi(\"" + text + "\"))"; 198 | } 199 | return "Str.atoi(\"" + text + "\")"; 200 | } 201 | 202 | //读取string 203 | public static String getString(String text){ 204 | if(text.startsWith("@string/")){ 205 | return "R.string."+text.substring(Str.strrchr(text, '/')+1); 206 | } 207 | return text; 208 | } 209 | 210 | public static String getiOSString(String text) { 211 | if(text.startsWith("@string/")){ 212 | return "StringUtil.getString(context,\""+text.substring(Str.strrchr(text, '/')+1)+"\")"; 213 | } 214 | return text; 215 | } 216 | 217 | // 218 | public static String getDrawable(String value){ 219 | if(value.startsWith("@drawable/")){ 220 | value = "R.drawable."+value.substring(10); 221 | } 222 | else if(value.startsWith("@mipmap/")){ 223 | value = "R.mipmap."+value.substring(8); 224 | } 225 | else if(value.startsWith("@android:drawable/")){ 226 | value = "android.R.drawable."+value.substring(18); 227 | } 228 | else{ 229 | return value; 230 | } 231 | return value; 232 | } 233 | 234 | //读取真或假 235 | public static boolean getBoolean(String text) 236 | { 237 | if(text.equals("true")) 238 | { 239 | return true; 240 | } 241 | return false; 242 | } 243 | 244 | //读取浮点数 245 | public static float getFloat(String text) 246 | { 247 | int i=0; 248 | char c=0; 249 | float f=0; 250 | int type=0; 251 | for(i=0;i='0'&&c<='9') 255 | { 256 | if(type==0) 257 | f=f*10+(c-'0'); 258 | else if(type==1) 259 | f=(f*10+(c-'0'))/10; 260 | } 261 | else if(c=='.') 262 | { 263 | type=1; 264 | } 265 | } 266 | return f; 267 | } 268 | 269 | //获取element下面的子element 270 | public static ArrayList getChildElement(Element eleParent){ 271 | ArrayList list_child = new ArrayList<>(); 272 | NodeList views = eleParent.getChildNodes(); 273 | for(int n=0;n0 || value.indexOf("dp")>0){ 68 | layout_width = ""+Str.atoi(value); 69 | } 70 | else if(value.indexOf("px")>0){ 71 | layout_width = ""+Str.atoi(value)*1.0/3; 72 | } 73 | else if (value.startsWith("@dimen/")) { 74 | layout_width = "DimenUtil.getDimen(\""+value.substring(7)+"\")"; 75 | } 76 | } 77 | else if(name.equals("android:layout_height")){ 78 | if(value.equals("match_parent") || value.equals("fill_parent")){ 79 | layout_height = "MATCH_PARENT"; 80 | } 81 | else if(value.equals("wrap_content")){ 82 | layout_height = "WRAP_CONTENT"; 83 | } 84 | else if(value.indexOf("dip")>0 || value.indexOf("dp")>0){ 85 | layout_height = ""+Str.atoi(value); 86 | } 87 | else if(value.indexOf("px")>0){ 88 | layout_height = ""+Str.atoi(value)*1.0/3; 89 | } 90 | else if (value.startsWith("@dimen/")) { 91 | layout_height = "DimenUtil.getDimen(\""+value.substring(7)+"\")"; 92 | } 93 | } 94 | else if(name.equals("android:layout_weight")){ 95 | 96 | } 97 | else if(name.equals("android:paddingTop")){ 98 | paddingTop = getValue(value); 99 | } 100 | else if(name.equals("android:paddingBottom")){ 101 | paddingBottom = getValue(value); 102 | } 103 | else if(name.equals("android:paddingLeft")){ 104 | paddingLeft = getValue(value); 105 | } 106 | else if(name.equals("android:paddingRight")){ 107 | paddingRight = getValue(value); 108 | } 109 | else if(name.equals("android:padding")){ 110 | paddingLeft = getValue(value); 111 | paddingRight = getValue(value); 112 | paddingTop = getValue(value); 113 | paddingBottom = getValue(value); 114 | } 115 | else if(name.equals("android:marginTop")){ 116 | marginTop = getValue(value); 117 | } 118 | else if(name.equals("android:marginBottom")){ 119 | marginBottom = getValue(value); 120 | } 121 | else if(name.equals("android:marginLeft")){ 122 | marginLeft = getValue(value); 123 | } 124 | else if(name.equals("android:marginRight")){ 125 | marginRight = getValue(value); 126 | } 127 | else if(name.equals("android:margin")){ 128 | marginTop = getValue(value); 129 | marginBottom = getValue(value); 130 | marginLeft = getValue(value); 131 | marginRight = getValue(value); 132 | } 133 | else if(name.equals("android:text")){ 134 | 135 | map_params.put("text", getString(value)); 136 | } 137 | else if(name.equals("android:gravity")){ 138 | String gravity = ""; 139 | if(value.indexOf("center")>=0){ 140 | gravity += "|Gravity.CENTER"; 141 | } 142 | if(value.indexOf("top")>=0){ 143 | gravity += "|Gravity.TOP"; 144 | } 145 | if(value.indexOf("right")>=0){ 146 | gravity += "|Gravity.RIGHT"; 147 | } 148 | if(value.indexOf("bottom")>=0){ 149 | gravity += "|Gravity.BOTTOM"; 150 | } 151 | if(gravity.length()>0) 152 | map_params.put("gravity", gravity.substring(1)); 153 | } 154 | else if(name.equals("android:layout_gravity")){ 155 | String gravity = ""; 156 | if(value.indexOf("center")>=0){ 157 | gravity += "|Gravity.CENTER"; 158 | } 159 | if(value.indexOf("top")>=0){ 160 | gravity += "|Gravity.TOP"; 161 | } 162 | if(value.indexOf("right")>=0){ 163 | gravity += "|Gravity.RIGHT"; 164 | } 165 | if(value.indexOf("bottom")>=0){ 166 | gravity += "|Gravity.BOTTOM"; 167 | } 168 | if(gravity.length()>0) 169 | map_params.put("layout_gravity", gravity.substring(1)); 170 | } 171 | else if(name.equals("android:hint")){ 172 | map_params.put("hint", getString(value)); 173 | } 174 | else if(name.equals("android:src")){ 175 | map_params.put("src",getDrawable(value)); 176 | } 177 | else if(name.equals("android:background")){ 178 | if(value.startsWith("#") || value.startsWith("@color/")){ 179 | map_params.put("backgroundColor", getColor(value)); 180 | } 181 | else{ 182 | map_params.put("background", getDrawable(value)); 183 | } 184 | } 185 | else if(name.equals("android:backgroundColor")){ 186 | map_params.put("backgroundColor", getColor(value)); 187 | } 188 | else if(name.equals("android:tint")){ 189 | map_params.put("tint", getColor(value)); 190 | 191 | } 192 | else if(name.equals("android:ellipsize")){ 193 | String elipsize = value; 194 | if(elipsize.equals("end")){ 195 | elipsize = "TextUtils.TruncateAt.END"; 196 | } 197 | else if(elipsize.equals("start")){ 198 | elipsize = "TextUtils.TruncateAt.START"; 199 | } 200 | else if(elipsize.equals("moddle")){ 201 | elipsize = "TextUtils.TruncateAt.MIDDLE"; 202 | } 203 | else if(elipsize.equals("marquee")){ 204 | elipsize = "TextUtils.TruncateAt.MARQUEE"; 205 | } 206 | map_params.put("ellipsize", elipsize); 207 | } 208 | else if(name.equals("android:inputType")){ 209 | 210 | } 211 | else if(name.equals("android:textAlignment")){ 212 | String textAlignment = ""; 213 | if(value.indexOf("center")>=0){ 214 | textAlignment += "|TextAlignment.center"; 215 | } 216 | if(value.indexOf("left")>=0){ 217 | textAlignment += "|TextAlignment.left"; 218 | } 219 | if(value.indexOf("top")>=0){ 220 | textAlignment += "|TextAlignment.top"; 221 | } 222 | if(value.indexOf("right")>=0){ 223 | textAlignment += "|TextAlignment.right"; 224 | } 225 | if (value.indexOf("bottom")>=0) { 226 | textAlignment += "|TextAlignment.bottom"; 227 | } 228 | if(textAlignment.length()>0){ 229 | map_params.put("textAlignment", textAlignment); 230 | } 231 | } 232 | else if(name.equals("android:minHeight")){ 233 | map_params.put("minHeight", getValue(value)); 234 | } 235 | else if(name.equals("android:maxHeight")){ 236 | map_params.put("maxHeight", getValue(value)); 237 | } 238 | else if(name.equals("android:minWidget")){ 239 | map_params.put("minWidget", getValue(value)); 240 | } 241 | else if(name.equals("android:maxWidget")){ 242 | map_params.put("maxWidget", getValue(value)); 243 | } 244 | else if(name.equals("android:shadowColor")){ 245 | map_params.put("shadowColor", getColor(value)); 246 | } 247 | else if(name.equals("android:scaleType")){ 248 | 249 | } 250 | else if(name.equals("android:alpha")){ 251 | map_params.put("alpha", (value)); 252 | } 253 | else if(name.equals("android:selectable")){ 254 | map_params.put("selectable", value); 255 | } 256 | else if(name.equals("android:singleLine")){ 257 | map_params.put("singleLine", value); 258 | } 259 | else if(name.equals("android:ems")){ 260 | map_params.put("ems",value); 261 | } 262 | else if(name.equals("android:lines")){ 263 | map_params.put("lines", value); 264 | } 265 | else if(name.equals("android:minLines")){ 266 | map_params.put("minLines", value); 267 | } 268 | else if(name.equals("android:maxLines")){ 269 | map_params.put("maxLines", value); 270 | } 271 | else if(name.equals("android:drawable")){ 272 | map_params.put("drawable", getDrawable(value)); 273 | } 274 | 275 | } 276 | 277 | //获取一个数值 278 | String getValue(String value) { 279 | String layout_height = null; 280 | if (value.equals("match_parent") || value.equals("fill_parent")) { 281 | layout_height = "double.infinity"; 282 | } else if (value.indexOf("dip") > 0 || value.indexOf("dp") > 0) { 283 | layout_height = "" + Str.atoi(value); 284 | } else if (value.indexOf("px") > 0) { 285 | layout_height = "" + Str.atoi(value) * 1.0 / 3; 286 | } else if (value.startsWith("@dimen/")) { 287 | layout_height = "DimenUtil.getDimen(\"" + value.substring(7) + "\")"; 288 | } 289 | return layout_height; 290 | } 291 | 292 | String getString(String value){ 293 | if(value.startsWith("@string/")){ 294 | return "StringUtil.getString(context, \"" + value.substring(8)+"\")"; 295 | } 296 | return "\""+value+"\""; 297 | } 298 | 299 | String getColor(String value){ 300 | if(value.startsWith("@color/")){ 301 | return "ColorUtil.getString(context, \"" + value.substring(7)+"\")"; 302 | } 303 | else if(value.startsWith("#")){ 304 | value = XmlUtil.getColorHex(value); 305 | return "Color(value)"; 306 | } 307 | return value; 308 | } 309 | 310 | String getDrawable(String value){ 311 | if(value.startsWith("@drawable/")){ 312 | return "DrawableUtil.getDrawable(context, \""+value.substring(10)+"\")"; 313 | } 314 | if(value.startsWith("@mipmap/")){ 315 | return "DrawableUtil.getDrawable(context, \""+value.substring(8)+"\")"; 316 | } 317 | return value; 318 | } 319 | 320 | @Override 321 | public List getChildList() { 322 | 323 | return children; 324 | } 325 | 326 | public void addChild(FlutterWidget widget){ 327 | children.add(widget); 328 | } 329 | 330 | private void getSpace(StringBuffer buffer){ 331 | for(int i=0;i list_widget = getChildList(); 405 | if (list_widget.size() != 0) { 406 | if (isLayout){ 407 | getSpace(buffer); 408 | buffer.append(" children:[\n"); 409 | } 410 | 411 | else { 412 | getSpace(buffer); 413 | buffer.append(" child:"); 414 | } 415 | for (int i = 0; i < list_widget.size(); i++) { 416 | buffer.append(list_widget.get(i).toString()); 417 | } 418 | if (isLayout){ 419 | getSpace(buffer); 420 | buffer.append(" ],\n"); 421 | } 422 | 423 | } 424 | getSpace(buffer); 425 | buffer.append("),\n"); 426 | return buffer.toString(); 427 | } 428 | 429 | } 430 | -------------------------------------------------------------------------------- /src/main/java/com/xl/util/flutter/FrameLayout.java: -------------------------------------------------------------------------------- 1 | package com.xl.util.flutter; 2 | 3 | public class FrameLayout { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/xl/util/flutter/LinearLayout.java: -------------------------------------------------------------------------------- 1 | package com.xl.util.flutter; 2 | 3 | import java.util.List; 4 | 5 | import com.xl.game.math.Str; 6 | 7 | public class LinearLayout implements Widget{ 8 | 9 | String layout_width; 10 | String layout_height; 11 | String padding; 12 | String paddingLeft; 13 | String paddingRight; 14 | String paddingTop; 15 | String paddingBottom; 16 | String margin; 17 | String marginLeft; 18 | String marginRight; 19 | String marginTop; 20 | String marginBottom; 21 | String widgetName; 22 | String orientation; 23 | List children; 24 | 25 | public void addParamItem(String name,String value){ 26 | if(name.equals("android:orientation")){ 27 | if(orientation.equals("vertical")){ 28 | orientation = ".vert"; 29 | } 30 | else if(orientation.equals("horizontal")){ 31 | orientation = ".horz"; 32 | } 33 | else{ 34 | orientation = ".horz"; 35 | } 36 | } 37 | if(name.equals("android:layout_width")){ 38 | if(value.equals("match_parent") || value.equals("fill_parent")){ 39 | layout_width = "MATCH_PARENT"; 40 | } 41 | else if(value.equals("wrap_content")){ 42 | layout_width = "WRAP_CONTENT"; 43 | } 44 | else if(value.indexOf("dip")>0 || value.indexOf("dp")>0){ 45 | layout_width = ""+Str.atoi(value); 46 | } 47 | else if(value.indexOf("px")>0){ 48 | layout_width = ""+Str.atoi(value)*1.0/3; 49 | } 50 | else if (value.startsWith("@dimen/")) { 51 | layout_width = "DimenUtil.getDimen(\""+value.substring(7)+"\")"; 52 | } 53 | } 54 | else if(name.equals("android:layout_height")){ 55 | if(value.equals("match_parent") || value.equals("fill_parent")){ 56 | layout_height = "MATCH_PARENT"; 57 | } 58 | else if(value.equals("wrap_content")){ 59 | layout_height = "WRAP_CONTENT"; 60 | } 61 | else if(value.indexOf("dip")>0 || value.indexOf("dp")>0){ 62 | layout_height = ""+Str.atoi(value); 63 | } 64 | else if(value.indexOf("px")>0){ 65 | layout_height = ""+Str.atoi(value)*1.0/3; 66 | } 67 | else if (value.startsWith("@dimen/")) { 68 | layout_height = "DimenUtil.getDimen(\""+value.substring(7)+"\")"; 69 | } 70 | } 71 | else if(name.equals("android:layout_weight")){ 72 | 73 | } 74 | else if(name.equals("android:paddingTop")){ 75 | paddingTop = getValue(value); 76 | } 77 | else if(name.equals("android:paddingBottom")){ 78 | paddingBottom = getValue(value); 79 | } 80 | else if(name.equals("android:paddingLeft")){ 81 | paddingLeft = getValue(value); 82 | } 83 | else if(name.equals("android:paddingRight")){ 84 | paddingRight = getValue(value); 85 | } 86 | else if(name.equals("android:padding")){ 87 | padding = getValue(value); 88 | } 89 | else if(name.equals("android:marginTop")){ 90 | marginTop = getValue(value); 91 | } 92 | else if(name.equals("android:marginBottom")){ 93 | marginBottom = getValue(value); 94 | } 95 | else if(name.equals("android:marginLeft")){ 96 | marginLeft = getValue(value); 97 | } 98 | else if(name.equals("android:marginRight")){ 99 | marginRight = getValue(value); 100 | } 101 | else if(name.equals("android:margin")){ 102 | margin = getValue(value); 103 | } 104 | } 105 | 106 | //获取一个数值 107 | String getValue(String value){ 108 | String layout_height = null; 109 | if(value.equals("match_parent") || value.equals("fill_parent")){ 110 | layout_height = "double.infinity"; 111 | } 112 | else if(value.indexOf("dip")>0 || value.indexOf("dp")>0){ 113 | layout_height = ""+Str.atoi(value); 114 | } 115 | else if(value.indexOf("px")>0){ 116 | layout_height = ""+Str.atoi(value)*1.0/3; 117 | } 118 | else if (value.startsWith("@dimen/")) { 119 | layout_height = "DimenUtil.getDimen(\""+value.substring(7)+"\")"; 120 | } 121 | return layout_height; 122 | } 123 | 124 | @Override 125 | public String toString() { 126 | StringBuffer buffer = new StringBuffer(); 127 | buffer.append(widgetName); 128 | buffer.append("(\n"); 129 | List list_widget = getChildList(); 130 | buffer.append("children:[\n"); 131 | for(int i=0;i getChildList() { 141 | // TODO Auto-generated method stub 142 | return children; 143 | } 144 | 145 | } 146 | -------------------------------------------------------------------------------- /src/main/java/com/xl/util/flutter/RelativeLayout.java: -------------------------------------------------------------------------------- 1 | package com.xl.util.flutter; 2 | 3 | public class RelativeLayout { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/xl/util/flutter/TextView.java: -------------------------------------------------------------------------------- 1 | package com.xl.util.flutter; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.xl.game.math.Str; 7 | 8 | public class TextView implements Widget{ 9 | String widgetName = "TextView"; 10 | String layout_width; 11 | String layout_height; 12 | String textSize; 13 | String textColor; 14 | String backgroundColor; 15 | String background; 16 | 17 | String paddingLeft; 18 | String paddingTop; 19 | String paddingRight; 20 | String paddingBottom; 21 | 22 | String marginTop; 23 | String marginBottom; 24 | String marginLeft; 25 | String marginRight; 26 | 27 | String layout_gravity; 28 | String gravity; 29 | 30 | public void addParamItem(String name,String value){ 31 | if(name.equals("android:layout_width")){ 32 | if(value.equals("match_parent") || value.equals("fill_parent")){ 33 | layout_width = "double.infinity"; 34 | } 35 | else if(value.indexOf("dip")>0 || value.indexOf("dp")>0){ 36 | layout_width = ""+Str.atoi(value); 37 | } 38 | else if(value.indexOf("px")>0){ 39 | layout_width = ""+Str.atoi(value)*1.0/3; 40 | } 41 | else if (value.startsWith("@dimen/")) { 42 | layout_width = "DimenUtil.getDimen(\""+value.substring(7)+"\")"; 43 | } 44 | } 45 | else if(name.equals("android:layout_height")){ 46 | if(value.equals("match_parent") || value.equals("fill_parent")){ 47 | layout_height = "double.infinity"; 48 | } 49 | else if(value.indexOf("dip")>0 || value.indexOf("dp")>0){ 50 | layout_height = ""+Str.atoi(value); 51 | } 52 | else if(value.indexOf("px")>0){ 53 | layout_height = ""+Str.atoi(value)*1.0/3; 54 | } 55 | else if (value.startsWith("@dimen/")) { 56 | layout_height = "DimenUtil.getDimen(\""+value.substring(7)+"\")"; 57 | } 58 | } 59 | else if(name.equals("android:layout_weight")){ 60 | 61 | } 62 | else if(name.equals("android:paddingTop")){ 63 | paddingTop = getValue(value); 64 | } 65 | else if(name.equals("android:paddingBottom")){ 66 | paddingBottom = getValue(value); 67 | } 68 | else if(name.equals("android:paddingLeft")){ 69 | paddingLeft = getValue(value); 70 | } 71 | else if(name.equals("android:paddingRight")){ 72 | paddingRight = getValue(value); 73 | } 74 | else if(name.equals("android:padding")){ 75 | String padding = getValue(value); 76 | paddingLeft = padding; 77 | paddingTop = padding; 78 | paddingRight = padding; 79 | paddingBottom = padding; 80 | } 81 | else if(name.equals("android:marginTop")){ 82 | marginTop = getValue(value); 83 | } 84 | else if(name.equals("android:marginBottom")){ 85 | marginBottom = getValue(value); 86 | } 87 | else if(name.equals("android:marginLeft")){ 88 | marginLeft = getValue(value); 89 | } 90 | else if(name.equals("android:marginRight")){ 91 | marginRight = getValue(value); 92 | } 93 | else if(name.equals("android:margin")){ 94 | String margin = getValue(value); 95 | marginLeft = margin; 96 | marginTop = margin; 97 | marginRight = margin; 98 | marginBottom = margin; 99 | } 100 | else if(name.equals("android:gravity")){ 101 | StringBuffer buf = new StringBuffer(); 102 | if(value.indexOf("left")>=0){ 103 | buf.append("|Gravity.left"); 104 | } 105 | if(value.indexOf("top")>=0){ 106 | buf.append("|Gravity.top"); 107 | } 108 | if(value.indexOf("right")>=0){ 109 | buf.append("|Gravity.right"); 110 | } 111 | if(value.indexOf("bottom")>=0){ 112 | buf.append("|Gravity.bottom"); 113 | } 114 | if(value.indexOf("center")>=0){ 115 | buf.append("|Gravity.center"); 116 | } 117 | String buf_text = buf.toString(); 118 | if(buf_text.length()>0){ 119 | gravity = buf_text.substring(1); 120 | } 121 | } 122 | } 123 | 124 | //获取一个数值 125 | String getValue(String value){ 126 | String layout_height = null; 127 | if(value.equals("match_parent") || value.equals("fill_parent")){ 128 | layout_height = "double.infinity"; 129 | } 130 | else if(value.indexOf("dip")>0 || value.indexOf("dp")>0){ 131 | layout_height = ""+Str.atoi(value); 132 | } 133 | else if(value.indexOf("px")>0){ 134 | layout_height = ""+Str.atoi(value)*1.0/3; 135 | } 136 | else if (value.startsWith("@dimen/")) { 137 | layout_height = "DimenUtil.getDimen(\""+value.substring(7)+"\")"; 138 | } 139 | return layout_height; 140 | } 141 | 142 | 143 | @Override 144 | public List getChildList() { 145 | return new ArrayList(); 146 | 147 | } 148 | 149 | @Override 150 | public String toString() { 151 | StringBuffer buffer = new StringBuffer(); 152 | 153 | buffer.append(widgetName); 154 | buffer.append("(\n"); 155 | if(layout_width!=null){ 156 | buffer.append("width:"+layout_width+",\n"); 157 | } 158 | if(layout_height!=null){ 159 | buffer.append("height:"+layout_height+",\n"); 160 | } 161 | if(layout_gravity!=null){ 162 | 163 | } 164 | if(gravity!=null){ 165 | buffer.append("gravity:"+gravity+"\n"); 166 | } 167 | 168 | buffer.append(")\n"); 169 | return buffer.toString(); 170 | } 171 | 172 | } 173 | -------------------------------------------------------------------------------- /src/main/java/com/xl/util/flutter/Toolbar.java: -------------------------------------------------------------------------------- 1 | package com.xl.util.flutter; 2 | 3 | public class Toolbar { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/xl/util/flutter/Widget.java: -------------------------------------------------------------------------------- 1 | package com.xl.util.flutter; 2 | 3 | import java.util.List; 4 | 5 | public interface Widget { 6 | void addParamItem(String name,String value); 7 | public List getChildList(); 8 | public String toString(); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/xl/window/TextWindow.java: -------------------------------------------------------------------------------- 1 | package com.xl.window; 2 | 3 | import java.awt.Dimension; 4 | 5 | import javax.swing.JFrame; 6 | import javax.swing.JLabel; 7 | import javax.swing.JPanel; 8 | import javax.swing.JScrollPane; 9 | import javax.swing.JTextArea; 10 | 11 | public class TextWindow extends JFrame{ 12 | 13 | JTextArea textArea; 14 | public TextWindow(){ 15 | setTitle("返回信息"); 16 | setSize(640, 480); 17 | JPanel layout_mainJPanel= new JPanel(); 18 | setContentPane(layout_mainJPanel); 19 | this.textArea= new JTextArea(); 20 | textArea.setLineWrap(true); //激活自动换行功能 21 | textArea.setWrapStyleWord(true); // 激活断行不断字功能 22 | //JTextArea.setSize(600,0); 23 | //label.setMaximumSize(new Dimension(600, 0)); 24 | JScrollPane scrollPane= new JScrollPane(textArea); 25 | setContentPane(scrollPane); 26 | } 27 | 28 | public void setText(String text) { 29 | this.textArea.setText(text); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/xl/window/Toast.java: -------------------------------------------------------------------------------- 1 | package com.xl.window; 2 | 3 | import java.awt.Color; 4 | import java.awt.Dimension; 5 | import java.awt.GridBagLayout; 6 | import java.awt.Point; 7 | import java.awt.event.ActionEvent; 8 | import java.awt.event.ActionListener; 9 | import java.awt.event.ComponentAdapter; 10 | import java.awt.event.ComponentEvent; 11 | import java.awt.geom.RoundRectangle2D; 12 | 13 | import javax.swing.JButton; 14 | import javax.swing.JDialog; 15 | import javax.swing.JFrame; 16 | import javax.swing.JLabel; 17 | import javax.swing.Timer; 18 | 19 | 20 | 21 | 22 | public class Toast extends JDialog{ 23 | private static final long serialVersionUID = -1602907470843951525L; 24 | 25 | public enum Style { NORMAL, SUCCESS, ERROR }; 26 | 27 | public static final int LENGTH_SHORT = 3000; 28 | public static final int LENGTH_LONG = 6000; 29 | public static final Color ERROR_RED = new Color(121, 0, 0); 30 | public static final Color SUCCESS_GREEN = new Color(22, 127, 57); 31 | public static final Color NORMAL_BLACK = new Color(0, 0, 0); 32 | 33 | private final float MAX_OPACITY = 0.8f; 34 | private final float OPACITY_INCREMENT = 0.05f; 35 | private final int FADE_REFRESH_RATE = 20; 36 | private final int WINDOW_RADIUS = 15; 37 | private final int CHARACTER_LENGTH_MULTIPLIER = 16; 38 | private final int DISTANCE_FROM_PARENT_TOP = 100; 39 | 40 | private JFrame mOwner; 41 | private String mText; 42 | private int mDuration; 43 | private Color mBackgroundColor = Color.BLACK; 44 | private Color mForegroundColor = Color.WHITE; 45 | 46 | public Toast(JFrame owner){ 47 | super(owner); 48 | mOwner = owner; 49 | } 50 | 51 | private void createGUI(){ 52 | setLayout(new GridBagLayout()); 53 | addComponentListener(new ComponentAdapter() { 54 | @Override 55 | public void componentResized(ComponentEvent e) { 56 | setShape(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), WINDOW_RADIUS, WINDOW_RADIUS)); 57 | } 58 | }); 59 | 60 | setAlwaysOnTop(true); 61 | setUndecorated(true); 62 | setFocusableWindowState(false); 63 | setModalityType(ModalityType.MODELESS); 64 | getContentPane().setBackground(mBackgroundColor); 65 | setSize(mText.length() * CHARACTER_LENGTH_MULTIPLIER, 25); 66 | 67 | JLabel label = new JLabel(mText); 68 | label.setForeground(mForegroundColor); 69 | add(label); 70 | int width= mText.length() * CHARACTER_LENGTH_MULTIPLIER; 71 | int height = 25; 72 | setBounds((mOwner.getWidth()-width)/2, mOwner.getHeight()/2, width, height); 73 | } 74 | 75 | public void fadeIn() { 76 | final Timer timer = new Timer(FADE_REFRESH_RATE, null); 77 | timer.setRepeats(true); 78 | timer.addActionListener(new ActionListener() { 79 | private float opacity = 0; 80 | @Override public void actionPerformed(ActionEvent e) { 81 | opacity += OPACITY_INCREMENT; 82 | setOpacity(Math.min(opacity, MAX_OPACITY)); 83 | if (opacity >= MAX_OPACITY){ 84 | timer.stop(); 85 | } 86 | } 87 | }); 88 | 89 | setOpacity(0); 90 | timer.start(); 91 | 92 | setLocation(getToastLocation()); 93 | setVisible(true); 94 | } 95 | 96 | public void fadeOut() { 97 | final Timer timer = new Timer(FADE_REFRESH_RATE, null); 98 | timer.setRepeats(true); 99 | timer.addActionListener(new ActionListener() { 100 | private float opacity = MAX_OPACITY; 101 | @Override public void actionPerformed(ActionEvent e) { 102 | opacity -= OPACITY_INCREMENT; 103 | setOpacity(Math.max(opacity, 0)); 104 | if (opacity <= 0) { 105 | timer.stop(); 106 | setVisible(false); 107 | dispose(); 108 | } 109 | } 110 | }); 111 | 112 | setOpacity(MAX_OPACITY); 113 | timer.start(); 114 | } 115 | 116 | private Point getToastLocation(){ 117 | Point ownerLoc = mOwner.getLocation(); 118 | int x = (int) (ownerLoc.getX() + ((mOwner.getWidth() - this.getWidth()) / 2)); 119 | int y = (int) (ownerLoc.getY() + DISTANCE_FROM_PARENT_TOP); 120 | return new Point(x, y); 121 | } 122 | 123 | public void setText(String text){ 124 | mText = text; 125 | } 126 | 127 | public void setDuration(int duration){ 128 | mDuration = duration; 129 | } 130 | 131 | @Override 132 | public void setBackground(Color backgroundColor){ 133 | mBackgroundColor = backgroundColor; 134 | } 135 | 136 | @Override 137 | public void setForeground(Color foregroundColor){ 138 | mForegroundColor = foregroundColor; 139 | } 140 | 141 | public static Toast makeText(JFrame owner, String text){ 142 | return makeText(owner, text, LENGTH_SHORT); 143 | } 144 | 145 | public static Toast makeText(JFrame owner, String text, Style style){ 146 | return makeText(owner, text, LENGTH_SHORT, style); 147 | } 148 | 149 | public static Toast makeText(JFrame owner, String text, int duration){ 150 | return makeText(owner, text, duration, Style.NORMAL); 151 | } 152 | 153 | public static Toast makeText(JFrame owner, String text, int duration, Style style){ 154 | Toast toast = new Toast(owner); 155 | toast.mText = text; 156 | toast.mDuration = duration; 157 | 158 | if (style == Style.SUCCESS) 159 | toast.mBackgroundColor = SUCCESS_GREEN; 160 | if (style == Style.ERROR) 161 | toast.mBackgroundColor = ERROR_RED; 162 | if (style == Style.NORMAL) 163 | toast.mBackgroundColor = NORMAL_BLACK; 164 | 165 | return toast; 166 | } 167 | 168 | public void display(){ 169 | new Thread(new Runnable() { 170 | @Override 171 | public void run() { 172 | try{ 173 | createGUI(); 174 | fadeIn(); 175 | Thread.sleep(mDuration); 176 | fadeOut(); 177 | } 178 | catch(Exception ex){ 179 | ex.printStackTrace(); 180 | } 181 | } 182 | }).start(); 183 | } 184 | 185 | // public static void main(String... args){ 186 | // final JFrame frame = new JFrame(); 187 | // frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 188 | // frame.setSize(new Dimension(500, 300)); 189 | // JButton b = new JButton("Toast!"); 190 | // 191 | // b.addActionListener(new ActionListener() { 192 | // @Override 193 | // public void actionPerformed(ActionEvent e) { 194 | // Toast.makeText(frame, "Annotations were successfully saved.", Style.SUCCESS).display(); 195 | // } 196 | // }); 197 | // 198 | // frame.add(b); 199 | // frame.setVisible(true); 200 | // } 201 | } 202 | 203 | -------------------------------------------------------------------------------- /src/main/java/com/xl/window/XmlToCodeWindow.java: -------------------------------------------------------------------------------- 1 | package com.xl.window; 2 | 3 | import java.awt.Color; 4 | import java.awt.Component; 5 | import java.awt.Desktop; 6 | import java.awt.Dimension; 7 | import java.awt.TextArea; 8 | import java.awt.Toolkit; 9 | import java.awt.datatransfer.DataFlavor; 10 | import java.awt.dnd.DnDConstants; 11 | import java.awt.dnd.DropTarget; 12 | import java.awt.dnd.DropTargetAdapter; 13 | import java.awt.dnd.DropTargetDropEvent; 14 | import java.awt.event.ActionEvent; 15 | import java.awt.event.ActionListener; 16 | import java.awt.event.MouseEvent; 17 | import java.awt.event.MouseListener; 18 | import java.io.File; 19 | import java.io.IOException; 20 | import java.net.URI; 21 | import java.util.List; 22 | 23 | import javax.swing.Box; 24 | import javax.swing.BoxLayout; 25 | import javax.swing.JButton; 26 | import javax.swing.JFrame; 27 | import javax.swing.JLabel; 28 | import javax.swing.JPanel; 29 | import javax.swing.JScrollPane; 30 | import javax.swing.JTextArea; 31 | import javax.swing.JTextField; 32 | 33 | import com.xl.util.ClipBoard; 34 | import com.xl.util.DomParser; 35 | import com.xl.util.FileUtils; 36 | import com.xl.util.FindViewByIdParser; 37 | import com.xl.util.FlutterDomParser; 38 | import com.xl.util.SwiftDomParser; 39 | 40 | public class XmlToCodeWindow extends JFrame { 41 | 42 | JTextArea editArea; 43 | JLabel label_info; 44 | JButton button_findViewById; 45 | JButton button_tojava; 46 | JButton button_tokotlin; 47 | JButton button_toswift; 48 | JButton button_toflutter; 49 | JScrollPane scrollPane; 50 | TextWindow textWindow; 51 | 52 | public XmlToCodeWindow() { 53 | int screen_w, screen_h; 54 | textWindow = new TextWindow(); 55 | int array[] = new int[] { 1, 21 }; 56 | System.out.println("数组" + array.toString()); 57 | 58 | Toolkit toolkit = Toolkit.getDefaultToolkit(); 59 | screen_w = (int) toolkit.getScreenSize().getWidth(); 60 | screen_h = (int) toolkit.getScreenSize().getHeight(); 61 | JPanel mainJPanel = new JPanel(); 62 | setContentPane(mainJPanel); 63 | setLayout(new BoxLayout(mainJPanel, BoxLayout.Y_AXIS)); 64 | Box box_v = Box.createVerticalBox(); 65 | // box_v.setAlignmentX(0.5f); 66 | getContentPane().add(box_v); 67 | mainJPanel.setSize(640, 480); 68 | // 69 | 70 | editArea = new JTextArea(); 71 | editArea.setColumns(20); 72 | editArea.setRows(10); 73 | 74 | label_info = new JLabel("https://github.com/fengdeyingzi/XmlToCode"); 75 | scrollPane = new JScrollPane(editArea); 76 | // scrollPane.add(editArea); 77 | button_findViewById = new JButton("findViewById"); 78 | button_findViewById.setAlignmentX(0.5f); 79 | button_tojava = new JButton("xml转java"); 80 | // 设置对齐方式 不然会出问题 81 | button_tojava.setAlignmentX((float) 0.5); 82 | 83 | button_tokotlin = new JButton("xml转kt"); 84 | button_tokotlin.setAlignmentX(0.5f); 85 | 86 | button_toflutter = new JButton("xml转flutter"); 87 | button_toflutter.setAlignmentX(0.5f); 88 | 89 | button_toswift = new JButton("xml转swift"); 90 | button_toswift.setAlignmentX(0.5f); 91 | 92 | label_info.setPreferredSize(new Dimension(640, 24)); 93 | label_info.setMaximumSize(new Dimension(screen_w, 24)); 94 | 95 | label_info.setForeground(new Color(70, 120, 240)); 96 | box_v.add(label_info); 97 | label_info.setAlignmentX(0.5f); 98 | box_v.add(scrollPane); 99 | Box box_h = Box.createHorizontalBox(); 100 | box_h.add(button_findViewById); 101 | box_h.add(Box.createRigidArea(new Dimension(10, 20))); 102 | box_h.add(button_tojava); 103 | box_h.add(Box.createRigidArea(new Dimension(10, 20))); 104 | box_h.add(button_tokotlin); 105 | box_h.add(Box.createRigidArea(new Dimension(10, 20))); 106 | box_h.add(button_toswift); 107 | box_h.add(Box.createRigidArea(new Dimension(10, 20))); 108 | box_h.add(button_toflutter); 109 | box_v.add(box_h); 110 | box_h.setPreferredSize(new Dimension(640, 30)); 111 | mainJPanel.add(box_v); 112 | // 设置最大宽高 用于适应布局 113 | // button.setPreferredSize(new Dimension(400, 60)); 114 | button_findViewById.setMaximumSize(new Dimension(screen_w, 60)); 115 | button_tojava.setMaximumSize(new Dimension(screen_w, 60)); 116 | button_tokotlin.setMaximumSize(new Dimension(screen_w, 60)); 117 | button_toswift.setMaximumSize(new Dimension(screen_w, 60)); 118 | button_toflutter.setMaximumSize(new Dimension(screen_w, 60)); 119 | button_findViewById.addActionListener(new ActionListener() { 120 | 121 | @Override 122 | public void actionPerformed(ActionEvent e) { 123 | FindViewByIdParser parser = new FindViewByIdParser(); 124 | parser.setXmlText(editArea.getText()); 125 | parser.parse(); 126 | TextWindow window = new TextWindow(); 127 | window.setLocation(getLocation().x+getWidth(), getLocation().y); 128 | window.setText(parser.toString()); 129 | window.show(); 130 | 131 | } 132 | }); 133 | button_tojava.addActionListener(new ActionListener() { 134 | @Override 135 | public void actionPerformed(ActionEvent e) { 136 | DomParser parser = new DomParser(); 137 | parser.setXmlText(editArea.getText()); 138 | parser.parseJava(); 139 | TextWindow window = new TextWindow(); 140 | window.setLocation(getLocation().x+getWidth(), getLocation().y); 141 | window.setText(parser.toString()); 142 | window.show(); 143 | } 144 | }); 145 | 146 | button_tokotlin.addActionListener(new ActionListener() { 147 | 148 | @Override 149 | public void actionPerformed(ActionEvent e) { 150 | Toast.makeText(XmlToCodeWindow.this, "暂未实现").display(); 151 | ; 152 | System.out.println("暂未实现"); 153 | 154 | } 155 | }); 156 | 157 | button_toswift.addActionListener(new ActionListener() { 158 | 159 | @Override 160 | public void actionPerformed(ActionEvent arg0) { 161 | SwiftDomParser parser = new SwiftDomParser(); 162 | parser.setXmlText(editArea.getText()); 163 | parser.parseSwift(); 164 | TextWindow window = new TextWindow(); 165 | 166 | window.setLocation(getLocation().x+getWidth(), getLocation().y); 167 | window.setText(parser.toString()); 168 | window.show(); 169 | } 170 | }); 171 | 172 | button_toflutter.addActionListener(new ActionListener() { 173 | 174 | @Override 175 | public void actionPerformed(ActionEvent e) { 176 | FlutterDomParser parser = new FlutterDomParser(); 177 | parser.setXmlText(editArea.getText()); 178 | parser.parseFlutter(); 179 | TextWindow window = new TextWindow(); 180 | window.setLocation(getLocation().x+getWidth(), getLocation().y); 181 | window.setText(parser.toString()); 182 | window.show(); 183 | } 184 | }); 185 | label_info.addMouseListener(new MouseListener() { 186 | @Override 187 | public void mouseClicked(MouseEvent mouseevent) { 188 | try { 189 | browseTo(label_info.getText()); 190 | } catch (Exception e) { 191 | Toast.makeText(XmlToCodeWindow.this, "跳转失败").display(); 192 | ; 193 | e.printStackTrace(); 194 | } 195 | 196 | } 197 | 198 | @Override 199 | public void mousePressed(MouseEvent mouseevent) { 200 | 201 | } 202 | 203 | @Override 204 | public void mouseReleased(MouseEvent mouseevent) { 205 | 206 | } 207 | 208 | @Override 209 | public void mouseEntered(MouseEvent mouseevent) { 210 | 211 | } 212 | 213 | @Override 214 | public void mouseExited(MouseEvent mouseevent) { 215 | 216 | } 217 | }); 218 | 219 | setSize(new Dimension(640, 480)); 220 | setLocation((screen_w - 640) / 2, (screen_h - 480) / 2); 221 | textWindow.setLocation((screen_w - 640) / 2, (screen_h - 480) / 2); 222 | setTitle("xml布局转代码v1.1 - 风的影子"); 223 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 224 | dragFile(editArea, new OnDragFile() { 225 | 226 | @Override 227 | public void onDragFile(List list_file) { 228 | File file = list_file.get(0); 229 | try { 230 | String text = FileUtils.read(file, "UTF-8"); 231 | editArea.setText(text); 232 | } catch (IOException e) { 233 | 234 | e.printStackTrace(); 235 | } 236 | 237 | } 238 | }); 239 | editArea.setText("\n" 240 | + "\n" + "\n" 243 | + " \n" 248 | + " \n" + "\n" 251 | + ""); 252 | // editArea.setText("\n" 253 | // +"\n" 258 | // +" \n" 263 | // +" \n" 268 | // +" \n" 273 | // +""); 274 | // setVisible(true); 275 | } 276 | 277 | private void browseTo(String url) throws Exception { 278 | Desktop desktop = Desktop.getDesktop(); 279 | 280 | if ((Desktop.isDesktopSupported()) && (desktop.isSupported(Desktop.Action.BROWSE))) { 281 | URI uri = new URI(url); 282 | System.out.println("跳转:" + url); 283 | desktop.browse(uri); 284 | } else { 285 | System.err.println("跳转失败:" + Desktop.isDesktopSupported() + desktop.isSupported(Desktop.Action.BROWSE)); 286 | } 287 | } 288 | 289 | public void dragFile(Component c, OnDragFile onDragFile) { 290 | new DropTarget(c, DnDConstants.ACTION_COPY_OR_MOVE, new DropTargetAdapter() { 291 | @Override 292 | public void drop(DropTargetDropEvent dtde) { 293 | try { 294 | 295 | if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { 296 | dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); 297 | List list = (List) (dtde.getTransferable() 298 | .getTransferData(DataFlavor.javaFileListFlavor)); 299 | if (onDragFile != null) { 300 | onDragFile.onDragFile(list); 301 | } 302 | // String temp=""; 303 | // for(File file:list) 304 | // { 305 | // temp+=file.getAbsolutePath()+";\n"; 306 | // JOptionPane.showMessageDialog(null, temp); 307 | // dtde.dropComplete(true); 308 | // 309 | // } 310 | 311 | } 312 | 313 | else { 314 | 315 | dtde.rejectDrop(); 316 | } 317 | 318 | } catch (Exception e) { 319 | e.printStackTrace(); 320 | } 321 | 322 | } 323 | 324 | }); 325 | 326 | } 327 | 328 | public interface OnDragFile { 329 | public void onDragFile(List list_file); 330 | } 331 | } 332 | --------------------------------------------------------------------------------