• Auto.js java布局 转 UI 布局,自己定义uii.layout()


    "ui";
    自定义布局()
    view=uii.layout(
       <linear orientation="vertical">
          <linear orientation="horizontal">
             <button id="按钮1"text="按钮1"/>
             <button id="按钮2"text="按钮2"/>
          </linear>
          <linear orientation="horizontal">
             <button id="按钮3"text="按钮3"/>
             <button id="按钮4"text="按钮4"/>
          </linear>
          <linear orientation="vertical"w="*">
             <button id="按钮5"text="按钮5"w="*"/>
             <button id="按钮6"text="toast"/>
          </linear>
       </linear>
    );
    ui.setContentView(view)
    uii.按钮6.setOnClickListener(new android.view.View.OnClickListener(){
          onClick:function(p1){
             toast("完成")
          }
    });
    function 自定义布局(){
       importClass(android.widget.Button);
       importClass(android.widget.LinearLayout);
       importClass(android.content.DialogInterface);
       importClass(android.graphics.Color);
       uii={
          layout:function (xml){
             var bj=xml.toString().split("\n")
             var 主布局  = new LinearLayout(context);
             fenge(bj,0,主布局,0)
             return 主布局;
          }
       }
    }
    function fenge(bj,j,zbj,c){
       for(var i=j;i<bj.length;i++){
          if(geshi(bj[i])=="<0>"){
             log(c+"进布局"+bj[i])
             var bjc=fenxi(bj[i].replace(/^\s+|\s+$/g,""))//分析控件
             i++;
             i=fenge(bj,i,bjc[0],c+1)
             if(bjc.length==1){
                zbj.addView(bjc[0])
             }else{
                zbj.addView(bjc[0],bjc[1])
             }
          }else if(geshi(bj[i])==""){
             log(c+"退布局"+bj[i])
             return i
          }else if(geshi(bj[i])=="<0/>"){
             var bjc=fenxi(bj[i].replace(/^\s+|\s+$/g,""))//分析控件
             if(bjc.length==1){
                zbj.addView(bjc[0])
             }else{
                zbj.addView(bjc[0],bjc[1])
             }
             log(c+"添加"+bj[i])
          }
       }
    }
    function shuzi(str){
       if(!str){
          return false
       }
       var pp=str.match(/^\d+$/g)
       if(pp){
          return true
       }
       return false;
    }
    function geshi(nr){
       var pp=nr.match(/<\/|<|\/>|>/g)
       if(pp){
          var str=""
          for(var i=0;i<pp.length;i++){
             str=str+pp[i]+"0"
          }
          return str.replace(/0$/,"");
       }
       return false;
    }
    function fenxi(str){
       var tou=str.split(" ")[0].match(/\w+/g)[0]
       var wei=str.match(/\w+="(\w|\d|[\u4e00-\u9fa5]|\*)+"/g)
       var dx='{'
       for(var i=0;i<wei.length;i++){
          var fg=wei[i].split("=")
          dx+='"'+fg[0].replace(/^\s+|\s+$/g,"")+'":'+fg[1].replace(/^\s+|\s+$/g,"")+","
       }
       dx=JSON.parse(dx.replace(/.$/g,'}'))
       return bj_linear(tou,dx)//线布局
    }
    function bj_linear(tou,dx){
       if(tou=="linear"){
          线布局  = new LinearLayout(context);
          if(dx.bg){
             线布局.setBackgroundColor(Color.parseColor(dx.bg));
          }
          if(dx.orientation){
             if(dx.orientation=="vertical"){
                线布局.setOrientation(1);//1.卡片布局
             }else if(dx.orientation=="horizontal"){
                线布局.setOrientation(0);//0.水平布局
             }
          }
          return [线布局,chicun(dx)];
       }else if(tou=="button"){
          var 按钮 = new Button(context);
          if(dx.text){
             按钮.setText(dx.text)
          }
          if(dx.id){
             uii[dx.id]=按钮;
          }
          return [按钮,chicun(dx)];
       }
    }
    function chicun(dx){
       var w=-2 //适应内容宽度
       var h=-2 //适应内容宽度
       if(dx.w){
          if(shuzi(dx.w)){
             w=dx.w-0 //实际宽度
          }else if(dx.w=="*"){
             w=-1 //适应父控件宽度
          }
       }
       if(dx.h){
          if(shuzi(dx.h)){
             h=dx.h-0 //实际宽度
          }else if(dx.h=="*"){
             h=-1 //适应父控件宽度
          }
       }
       log("适应="+w+","+h)
       return new LinearLayout.LayoutParams(w,h);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
  • 相关阅读:
    用Excel制作甘特图跟踪项目进度(附绘制教程)
    若依前后端分离版:增加新的登录接口,用于小程序或者APP获取token,并使用若依的验证方法
    Nature Communications|柔性无感智能隐形眼镜(柔性传感/可穿戴电子/柔性电子)
    【第2章 Node.js基础】2.1 JavaScript基本语法
    Android 使用元数据
    java毕业设计基于web的学校工资管理系统Mybatis+系统+数据库+调试部署
    Java面试题大全(2021版)
    记一次 .NET 某自动化采集软件 崩溃分析
    [计算机网络安全实验] TCP协议漏洞利用
    自定义指令
  • 原文地址:https://blog.csdn.net/qq_25226575/article/details/128160256