"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])=="0>"){
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);
}else if(dx.orientation=="horizontal"){
线布局.setOrientation(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