第一章:环境信息 · 平台:RLGG 2.1.0(基于 GameGuardian 的 Lua 脚本引擎) · 底层:Android 15,SDK 35 · 架构:arm64-v8a · Lua 版本:Lua 5.1 · 悬浮窗实现:Android WebView + 原生 View · 通信机制:rlgg 对象(JS 注入) 第二章:所有必需的 import 语句 使用 luajava.loadlayout 之前,必须在脚本顶部 import 所有用到的 Android 类。这是一条硬性规则,遗漏任何一个都会报错 cannot find Xxx (field 'loadlayout')。 ```lua -- 基础布局容器 import "android.widget.LinearLayout" import "android.widget.FrameLayout" import "android.widget.RelativeLayout" import "android.widget.ScrollView" import "android.widget.HorizontalScrollView" import "android.widget.GridLayout" -- 基础控件 import "android.widget.TextView" import "android.widget.Button" import "android.widget.ImageView" import "android.widget.ImageButton" import "android.widget.EditText" import "android.widget.CheckBox" import "android.widget.RadioButton" import "android.widget.Switch" import "android.widget.SeekBar" import "android.widget.ProgressBar" import "android.widget.RatingBar" import "android.widget.Spinner" -- 布局参数 import "android.view.Gravity" import "android.view.ViewGroup" import "android.view.View" -- 颜色和图形 import "android.graphics.Color" import "android.graphics.drawable.GradientDrawable" import "android.graphics.drawable.ColorDrawable" import "android.graphics.drawable.StateListDrawable" import "android.graphics.PixelFormat" -- 系统 import "android.os.Build" import "android.os.Handler" import "android.os.Looper" -- 窗口管理 import "android.view.WindowManager" import "android.view.WindowManager$LayoutParams" -- 内容 import "android.content.Context" -- 位图 import "android.graphics.Bitmap" import "android.graphics.BitmapFactory" ``` 第三章:luajava.loadlayout 完整语法 luajava.loadlayout 用 Lua 表构建 Android 原生布局。表结构规则: · 第一个元素:类名(已 import) · 后续:属性键值对 · 嵌套子视图:用表包裹,第一个元素是子类名 ```lua local view = luajava.loadlayout({ LinearLayout, -- 根容器类名 layout_width = 'match_parent', -- 或 'wrap_content' 或 '40dp' 或数字 layout_height = 'match_parent', orientation = 'vertical', -- horizontal / vertical gravity = 'center', padding = '10dp', backgroundColor = 0xff333333, -- ARGB 格式 { TextView, -- 子视图 id = 'myText', -- 给子视图取ID(用于 findViewByName) layout_width = 'wrap_content', layout_height = 'wrap_content', text = 'Hello World', textColor = 0xffffffff, textSize = '14sp', gravity = 'center', layout_margin = '8dp', padding = '4dp', }, { Button, layout_width = 'match_parent', layout_height = '48dp', text = 'Click Me', onClick = function(view) gg.toast('Button clicked') end, }, }) -- layout_width / layout_height 支持三种写法 -- 'match_parent' 或 -1 -- 'wrap_content' 或 -2 -- '40dp' 字符串 -- 40 纯数字(px) ``` 第四章:luajava.newInstance 完整语法 创建 Java 对象时,参数必须是完整的包名字符串,用单引号或双引号括起来。 ```lua -- 正确写法 local drawable = luajava.newInstance('android.graphics.drawable.GradientDrawable') -- 错误写法(会报错) local drawable = luajava.newInstance(GradientDrawable) -- 创建后设置属性 drawable:setShape(0) -- 0=矩形, 1=椭圆, 2=线条, 3=环形 drawable:setCornerRadius(200) -- 圆角半径(px) drawable:setCornerRadii({ -- 四角不同圆角 50, 50, -- 左上 50, 50, -- 右上 0, 0, -- 右下 0, 0 -- 左下 }) drawable:setColor(0xcc6750a4) -- 纯色填充 (ARGB) drawable:setStroke(2, 0xffff0000) -- 描边(宽度px, ARGB颜色) drawable:setGradientCenter(0.5, 0.5) -- 渐变中心 drawable:setGradientType(0) -- 0=线性, 1=径向, 2=扫描 drawable:setColors({0xffff0000, 0xff0000ff}) -- 渐变色数组 drawable:setOrientation( -- 渐变方向 luajava.bindClass('android.graphics.drawable.GradientDrawable$Orientation').TL_BR ) -- 应用到视图 view:setBackgroundDrawable(drawable) -- ColorDrawable(纯色背景) local cd = luajava.newInstance('android.graphics.drawable.ColorDrawable', 0xff333333) view:setBackgroundDrawable(cd) -- StateListDrawable(状态选择器) local sld = luajava.newInstance('android.graphics.drawable.StateListDrawable') local normal = luajava.newInstance('android.graphics.drawable.ColorDrawable', 0xff444444) local pressed = luajava.newInstance('android.graphics.drawable.ColorDrawable', 0xff666666) sld:addState({-android.R.attr.state_pressed}, normal) sld:addState({android.R.attr.state_pressed}, pressed) view:setBackgroundDrawable(sld) ``` 第五章:windowManager 模块全部函数详解 ```lua local wm = require('windowManager') -- 5.1 bindView:绑定视图,返回代理对象 -- 代理对象可以调用原 view 的方法,也可以调用 wm 的方法 -- 调用 wm 方法时,绑定的 view 作为第一个参数自动传入 local mgr = wm:bindView(view) -- 5.2 addView:添加视图到悬浮窗 -- 参数可以是 table 或 WindowManager.LayoutParams 对象 mgr:addView({ width = -1, -- -1=MATCH_PARENT, -2=WRAP_CONTENT, 数值=px height = -1, gravity = 17, -- 定位方式 flags = 8, -- 标志位 x = 0, -- X坐标偏移 y = 0, -- Y坐标偏移 type = 2038, -- 窗口类型 format = 1, -- 像素格式 }) -- 5.3 removeView:从窗口移除视图 wm:removeView(view) -- 5.4 updateViewLayout:更新视图的 LayoutParams -- params 是 getLayoutParams 返回的 Java 对象,可直接修改其字段 wm:updateViewLayout(view, params) -- 5.5 updateView:用缓存的 LayoutParams 更新 wm:updateView(view) -- 5.6 getLayoutParams:获取当前 LayoutParams Java 对象 -- 返回的对象可直接读写字段: local p = wm:getLayoutParams(view) p.x = newX p.y = newY p.flags = newFlags -- 5.7 setVisibility:设置可见性 -- 0 = VISIBLE(可见) -- 4 = INVISIBLE(不可见但占空间) -- 8 = GONE(不可见且不占空间) wm:setVisibility(view, 0) -- 5.8 hide / show:隐藏/恢复 -- hide 内部保存恢复方法,show 用于恢复 wm:hide(view) wm:show(view) wm:hideAll() wm:showAll() -- 5.9 destroy:销毁视图(移除 + 清除tag) wm:destroy(view) wm:destroyAll() -- 5.10 触摸控制 -- 开启/关闭内置拖动 wm:setMoveable(view, true) -- 自定义触摸监听 wm:addOnTouchListener(view, function(v, event) -- 返回值 true=消费事件, false=不消费 end) -- 移除触摸监听 wm:removeOnTouchListener(view, func) wm:removeAllOnTouchListener(view) wm:cancelOnTouchListener(view) -- 创建移动回调 local moveCallback = wm:newMoveCallback(view, params) -- 5.11 创建新的 LayoutParams local lp = wm:newWindowManagerLayoutParams({flags = 8}) -- 5.12 线程控制 wm:wait() -- 阻塞当前线程,直到 wm:exit() 被调用 wm:exit() -- 销毁所有窗口并唤醒 wait() wm:park() -- 手动阻塞 wm:unpark() -- 手动唤醒 -- 5.13 在主线程执行 wm:post(function() -- 这里的代码在 UI 线程执行 end) -- 5.14 获取信息 wm:containsView(view) -- 检查视图是否已添加 wm:getInstance() -- 获取系统 WindowManager wm:getContext() -- 获取 Context wm:getTag(view, key) -- 获取 tag wm:setTag(view, key, value) -- 设置 tag ``` 第六章:LayoutParams 参数完全说明 6.1 width 和 height ``` -1 (MATCH_PARENT):占满父容器 -2 (WRAP_CONTENT):根据内容自适应 正数:具体像素值 ``` 6.2 gravity 定位 ``` 17 = CENTER 居中 51 = TOP | LEFT 左上角 53 = TOP | RIGHT 右上角 83 = BOTTOM | LEFT 左下角 85 = BOTTOM | RIGHT 右下角 48 = TOP | CENTER_HORIZONTAL 顶部居中 80 = BOTTOM | CENTER_HORIZONTAL 底部居中 ``` 6.3 flags 标志位 ``` 0x00000008 FLAG_NOT_FOCUSABLE 不获取焦点(悬浮窗必须) 0x00000010 FLAG_NOT_TOUCHABLE 不响应触摸 0x00000020 FLAG_NOT_TOUCH_MODAL 触摸事件穿透 0x00000100 FLAG_LAYOUT_IN_SCREEN 允许延伸到状态栏 0x00000200 FLAG_LAYOUT_NO_LIMITS 允许超出屏幕边界 0x00000400 FLAG_DIM_BEHIND 背景变暗 0x00001000 FLAG_WATCH_OUTSIDE_TOUCH 监听外部触摸 0x00020000 FLAG_HARDWARE_ACCELERATED 硬件加速 0x02000000 FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS 绘制系统栏背景 常用组合: -- 普通悬浮窗 flags = 0x00000008 -- 仅 FLAG_NOT_FOCUSABLE -- 全屏悬浮窗(可延伸到状态栏) flags = 0x00000400 | 0x00000200 | 0x00000008 -- 可穿透触摸 flags = 0x00000008 | 0x00000020 ``` 6.4 type 窗口类型 ``` 1 TYPE_SYSTEM_ALERT(旧版,API < 26) 2002 TYPE_PHONE(旧版) 2003 TYPE_SYSTEM_OVERLAY 2007 TYPE_TOAST 2038 TYPE_APPLICATION_OVERLAY(API >= 26,推荐) ``` 6.5 format 像素格式 ``` 1 PixelFormat.RGBA_8888(支持透明) 2 PixelFormat.RGBX_8888 3 PixelFormat.RGB_888 4 PixelFormat.RGB_565 5 PixelFormat.RGBA_4444(旧版透明) -1 PixelFormat.OPAQUE(不透明) -2 PixelFormat.TRANSLUCENT(半透明) -3 PixelFormat.TRANSPARENT(透明) ``` 第七章:rlgg 对象(JS-Lua 通信完整说明) 7.1 rlgg 方法 ```javascript // rlgg.call(函数名) —— 调用 Lua 全局函数,无参数 rlgg.call('closePanel') // rlgg.calls(函数名, [参数数组]) —— 调用 Lua 函数,传参 rlgg.calls('tryLogin', [username, password]) // rlgg.load(lua代码字符串) —— 直接执行 Lua 代码 rlgg.load('gg.alert("hello")') // rlgg.log(内容) —— 输出日志到 RLGG 控制台 rlgg.log('Button clicked') ``` 7.2 Lua 端函数定义 ```lua -- 方式1:直接定义全局函数(推荐) function closePanel() wm:setVisibility(ball, 0) wm:setVisibility(panel, 8) end function tryLogin(user, pass) -- 处理登录 end -- 方式2:显式写入 _ENV _ENV["closePanel"] = function() wm:setVisibility(ball, 0) wm:setVisibility(panel, 8) end ``` 7.3 JS 中调用 Lua 的完整示例 ```html ``` 7.4 rlgg.load 注意事项 ```lua -- rlgg.load 在子线程执行,可直接用 gg 函数 -- 但如果内部抛异常,会中断后续所有 Lua 代码 -- 错误示例: function badFunc() rlgg.load('doSomething()') -- 如果 doSomething 报错 gg.toast('done') -- 这行不会执行 end -- 正确示例: function goodFunc() pcall(function() rlgg.load('doSomething()') end) gg.toast('done') -- 这行会执行 end ``` 第八章:luajava 完整 API 8.1 创建 WebView ```lua local webView = luajava.webView(function(wv) -- wv 是 android.webkit.WebView 实例 wv:loadData(htmlString, 'text/html', 'UTF-8') wv:setBackgroundColor(0x00000000) -- 透明背景 wv:clearCache(true) -- 清除缓存 wv:setVerticalScrollBarEnabled(false) -- 隐藏滚动条 wv:setHorizontalScrollBarEnabled(false) end) ``` 8.2 线程操作 ```lua -- 启动子线程 luajava.startThread(function() -- 耗时操作 gg.sleep(1000) -- 回到主线程更新 UI luajava.post(function() wm:setVisibility(panel, 0) end) end) -- 主线程执行 luajava.post(function() gg.alert('在主线程执行') end) -- 获取当前毫秒时间戳 local now = luajava.currentTimeMillis() ``` 8.3 隐藏 GG 原生悬浮窗 ```lua luajava.setFloatingWindowHide(false) ``` 8.4 下载文件 ```lua local ok, err = luajava.download(url, savePath, '下载提示文字') if ok then gg.alert('下载成功') else gg.alert('下载失败: ' .. tostring(err)) end ``` 第九章:gg 库完整函数列表 ```lua -- 9.1 弹窗 gg.alert('单按钮提示') gg.alert('提示', '按钮1', '按钮2', '按钮3') -- 返回 1, 2, 3 表示用户点击了哪个按钮 -- 9.2 输入弹窗 local result = gg.prompt( {'输入框1标题', '输入框2标题'}, {'默认值1', '默认值2'}, {'text', 'number'} -- 输入类型:text, number, path ) -- result[1], result[2] 为用户输入的值 -- 用户点取消返回 nil -- 9.3 Toast 提示 gg.toast('短暂提示') gg.alert2('弹窗标题', '弹窗内容') -- 9.4 悬浮窗控制 gg.setVisible(false) -- 隐藏 GG 悬浮窗 gg.setVisible(true) -- 显示 GG 悬浮窗 -- 9.5 线程休眠 gg.sleep(1000) -- 休眠 1000 毫秒 -- 9.6 路径 gg.EXT_STORAGE -- 外部存储路径字符串,如 '/storage/emulated/0' gg.getFile() -- 文件选择器,返回路径字符串或 nil -- 9.7 搜索相关 gg.clearResults() gg.searchNumber(100, gg.TYPE_DWORD) gg.getResults(count) gg.getResultsCount() gg.editAll(200, gg.TYPE_DWORD) -- 9.8 进程 gg.getTargetPackage() -- 获取目标包名 gg.processKill() -- 结束目标进程 ``` 第十章:文件与网络操作 10.1 file 库 ```lua file.read(path) -- 读取文件,返回字符串 file.write(path, data) -- 写入文件,覆盖写入 file.append(path, data) -- 追加写入 file.delete(path) -- 删除文件 file.checkFile(path) -- 创建文件或目录 file.isFile(path) -- 判断文件是否存在 file.isDir(path) -- 判断目录是否存在 file.list(path) -- 列出目录内容,返回文件名字符串数组 file.mkdir(path) -- 创建目录 file.rmdir(path) -- 删除目录(空目录) file.copy(src, dst) -- 复制文件 file.move(src, dst) -- 移动文件 file.size(path) -- 获取文件大小(字节) ``` 10.2 io 库(标准 Lua) ```lua local f = io.open(path, 'r') -- 'r'读, 'w'写, 'a'追加, 'rb'二进制读, 'wb'二进制写 if f then local content = f:read('*a') -- 读取全部 -- f:read('*l') 读取一行 -- f:read('*n') 读取数字 -- f:read(10) 读取10个字符 f:write('内容') f:close() end ``` 10.3 http 库 ```lua -- POST 请求 local response = http.post(url, headers, body) -- response 是 table -- response.content 响应体字符串 -- response.code 状态码 -- response.message 状态消息 -- response.headers 响应头表 -- response.url 最终URL -- GET 请求(用 gg.makeRequest 更简单) local result = gg.makeRequest(url) -- result.content 是响应体字符串 -- 下载文件 local ok, err = http.download(url, headers, path, function(current, total) -- current: 已下载字节 -- total: 总字节(可能为 nil) local percent = total and math.floor(current / total * 100) or '未知' end) ``` 10.4 JSON 处理 ```lua local json = require('ljson') -- 或 local json = require('json') local table = json.decode(jsonString) -- JSON -> Lua table local str = json.encode(luaTable) -- Lua table -> JSON string ``` 第十一章:WebView 输入框聚焦处理 ```lua -- 输入框聚焦时去掉 FLAG_NOT_FOCUSABLE function focusIn() params.flags = params.flags & ~0x00000008 wm:updateViewLayout(webView, params) end -- 输入框失焦时恢复 FLAG_NOT_FOCUSABLE function focusOut() params.flags = params.flags | 0x00000008 wm:updateViewLayout(webView, params) end ``` ```html ``` 第十二章:常用设计模式 12.1 悬浮球 + 全屏面板(最常用) ```lua -- 变量 local ball, panel local ballMgr, panelMgr local ballParams, panelParams -- 创建球(原生视图,小而轻) -- 创建面板(WebView,全屏) -- 打开面板 function openPanel() wm:setVisibility(panel, 0) wm:setVisibility(ball, 8) end -- 关闭面板 function closePanel() wm:setVisibility(ball, 0) wm:setVisibility(panel, 8) end -- 全局退出 function closeAll() if ballMgr then ballMgr:exit() end if panelMgr then panelMgr:exit() end end ``` 12.2 多界面切换 ```lua -- 创建多个视图(可以是多个 WebView) local view1, view2, view3 -- 同时添加到屏幕 mgr1:addView({...}) mgr2:addView({...}) mgr3:addView({...}) -- 初始全部隐藏,只显示第一个 wm:setVisibility(view2, 8) wm:setVisibility(view3, 8) -- 切换时先显示新界面,再隐藏旧界面(避免闪烁) function switchTo(v) wm:setVisibility(v, 0) wm:setVisibility(currentView, 8) currentView = v end ``` 12.3 HTML 中点击空白关闭面板 ```javascript document.body.addEventListener('click', function(e) { // 列出所有可交互的控件选择器 var ctrl = e.target.closest('button, .nav-item, .switch, input, select, .chip, .list-item, .color-dot, .check, .radio, .tab-item, label'); if (!ctrl) { rlgg.call('closePanel'); } }); ``` 12.4 自定义拖动 + 点击 ```lua local startX, startY, moved wm:addOnTouchListener(view, function(v, e) local action = e:getAction() if action == e.ACTION_DOWN then startX = e:getRawX() startY = e:getRawY() moved = false return true elseif action == e.ACTION_MOVE then local dx = e:getRawX() - startX local dy = e:getRawY() - startY -- 移动超过 5px 才算拖动 if math.abs(dx) > 5 or math.abs(dy) > 5 then moved = true params.x = params.x + dx params.y = params.y + dy wm:updateViewLayout(view, params) startX = e:getRawX() startY = e:getRawY() end return true elseif action == e.ACTION_UP then if not moved then -- 点击事件 end return true end return false end) ``` 第十三章:控件 CSS 完整样式表 13.1 开关 (Switch/Toggle) ```css /* MD3 风格开关 */ .switch { position:relative; width:52px; height:32px; cursor:pointer; display:inline-block; flex-shrink:0; } .switch input { opacity:0; width:0; height:0; position:absolute; } .switch .track { position:absolute; inset:0; background:#79747e; border-radius:16px; transition:all 0.2s; } .switch input:checked + .track { background:#6750a4; box-shadow:0 0 8px rgba(103,80,164,0.4); } .switch .thumb { position:absolute; top:4px; left:4px; width:24px; height:24px; background:#fff; border-radius:50%; transition:all 0.2s; box-shadow:0 1px 3px rgba(0,0,0,0.3); } .switch input:checked + .track + .thumb { transform:translateX(20px); } ``` ```html ``` 13.2 iOS 风格开关 ```css .ios-switch { position:relative; width:51px; height:31px; cursor:pointer; display:inline-block; flex-shrink:0; } .ios-switch input { opacity:0; width:0; height:0; position:absolute; } .ios-switch .track { position:absolute; inset:0; background:rgba(120,120,128,0.16); border-radius:16px; transition:all 0.3s; } .ios-switch input:checked + .track { background:#34C759; } .ios-switch .thumb { position:absolute; top:2px; left:2px; width:27px; height:27px; background:#fff; border-radius:50%; transition:all 0.3s cubic-bezier(0.4,0,0.2,1); box-shadow:0 3px 8px rgba(0,0,0,0.15),0 1px 3px rgba(0,0,0,0.1); } .ios-switch input:checked + .track + .thumb { transform:translateX(20px); } ``` 13.3 滑块 (Slider/Range) ```css input[type="range"] { width:100%; height:4px; -webkit-appearance:none; appearance:none; background:#e0e0e0; border-radius:2px; outline:none; cursor:pointer; } input[type="range"]::-webkit-slider-thumb { -webkit-appearance:none; appearance:none; width:20px; height:20px; border-radius:50%; background:#6750a4; cursor:pointer; box-shadow:0 2px 6px rgba(0,0,0,0.2); border:2px solid #fff; } ``` ```html ``` 13.4 按钮 (Button) ```css /* 填充按钮 */ .btn-filled { width:100%; padding:14px 24px; border:none; border-radius:20px; background:#6750a4; color:#fff; font-family:inherit; font-size:14px; font-weight:500; cursor:pointer; letter-spacing:0.5px; transition:all 0.15s; -webkit-tap-highlight-color:transparent; outline:none; user-select:none; } .btn-filled:active { background:#5e489d; transform:scale(0.97); } /* 轮廓按钮 */ .btn-outlined { width:100%; padding:14px 24px; border:1px solid #79747e; border-radius:20px; background:transparent; color:#6750a4; font-family:inherit; font-size:14px; font-weight:500; cursor:pointer; letter-spacing:0.5px; transition:all 0.15s; -webkit-tap-highlight-color:transparent; outline:none; user-select:none; } .btn-outlined:active { background:rgba(103,80,164,0.08); } /* 文字按钮 */ .btn-text { padding:8px 16px; border:none; background:transparent; color:#6750a4; font-family:inherit; font-size:13px; cursor:pointer; -webkit-tap-highlight-color:transparent; } ``` 13.5 复选框 (Checkbox) ```css .check { width:22px; height:22px; border-radius:6px; border:2px solid #79747e; cursor:pointer; display:flex; align-items:center; justify-content:center; flex-shrink:0; transition:all 0.15s; } .check.on { background:#6750a4; border-color:#6750a4; } .check.on::after { content:'✓'; color:#fff; font-size:12px; font-weight:bold; } ``` ```html
``` 13.6 单选框 (Radio) ```css .radio { width:22px; height:22px; border-radius:50%; border:2px solid #79747e; cursor:pointer; display:flex; align-items:center; justify-content:center; flex-shrink:0; transition:all 0.15s; } .radio.on { border-color:#6750a4; } .radio.on::after { content:''; width:12px; height:12px; border-radius:50%; background:#6750a4; } ``` ```html
``` 13.7 下拉选择 (Select) ```css select { width:100%; padding:12px 14px; border:1px solid rgba(255,255,255,0.12); border-radius:12px; background:rgba(0,0,0,0.2); color:#c0c0d0; font-family:inherit; font-size:13px; outline:none; -webkit-appearance:none; } select:focus { border-color:#6a9aff; } ``` ```html ``` 13.8 进度条 (Progress Bar) ```css .progress-wrap { height:8px; background:rgba(255,255,255,0.08); border-radius:4px; overflow:hidden; } .progress-fill { height:100%; background:#6750a4; border-radius:4px; transition:width 0.3s ease; box-shadow:0 0 10px rgba(103,80,164,0.4); } ``` ```html
``` 13.9 标签/芯片 (Chip) ```css .chip { display:inline-block; padding:8px 16px; border-radius:20px; border:1px solid rgba(255,255,255,0.1); background:rgba(255,255,255,0.04); color:#aaa; font-size:12px; cursor:pointer; transition:all 0.15s; } .chip.active { background:rgba(103,80,164,0.2); border-color:#6750a4; color:#fff; } ``` ```html
标签1
``` 13.10 颜色选择器 (Color Picker) ```css .color-dot { width:28px; height:28px; border-radius:50%; cursor:pointer; border:2px solid transparent; transition:all 0.15s; flex-shrink:0; } .color-dot.active { border-color:#fff; box-shadow:0 0 12px rgba(180,210,240,0.5); } ``` ```html
``` 13.11 列表项 (List Item) ```css .list-item { display:flex; align-items:center; justify-content:space-between; padding:14px 16px; border-bottom:1px solid rgba(255,255,255,0.04); cursor:pointer; transition:background 0.15s; } .list-item:active { background:rgba(255,255,255,0.04); } .list-item:last-child { border-bottom:none; } .list-item .leading { margin-right:12px; font-size:20px; flex-shrink:0; } .list-item .text { flex:1; font-size:14px; } .list-item .trailing { color:#888; font-size:12px; margin-left:8px; } .list-item .arrow { color:#666; font-size:16px; } ``` 13.12 输入框 (Text Field) ```css .text-field { width:100%; padding:14px 16px; border:1px solid rgba(255,255,255,0.1); border-radius:12px; background:rgba(0,0,0,0.2); color:#c0c0d0; font-family:inherit; font-size:14px; outline:none; transition:border 0.2s; margin-bottom:10px; } .text-field:focus { border-color:#6a9aff; border-width:2px; padding:13px 15px; } .text-field::placeholder { color:#555; } ``` 13.13 选项卡 (Tabs) ```css .tabs { display:flex; gap:0; background:rgba(255,255,255,0.04); border-radius:16px; padding:4px; } .tab { flex:1; padding:10px 0; text-align:center; border-radius:14px; font-size:13px; cursor:pointer; color:#888; transition:all 0.2s; } .tab.active { background:#6750a4; color:#fff; box-shadow:0 2px 8px rgba(103,80,164,0.3); } ``` 13.14 底部导航栏 (Bottom Navigation) ```css .bottom-nav { display:flex; justify-content:space-around; background:rgba(30,30,40,0.95); border-top:1px solid rgba(255,255,255,0.06); padding:8px 0; } .nav-item { display:flex; flex-direction:column; align-items:center; font-size:10px; color:#888; cursor:pointer; padding:4px 12px; transition:color 0.2s; } .nav-item.active { color:#6a9aff; } .nav-item .icon { font-size:22px; margin-bottom:2px; } ``` 第十四章:完整可运行代码模板 ```lua -- ========================================== -- 标准悬浮窗模板(悬浮球 + 全屏面板) -- ========================================== -- 1. 导入所有需要的类 import "android.widget.TextView" import "android.widget.LinearLayout" import "android.view.Gravity" import "android.graphics.drawable.GradientDrawable" -- 2. 初始化 luajava.setFloatingWindowHide(false) local wm = require('windowManager') -- 3. 全局变量 local ball, panel local ballMgr, panelMgr local ballParams, panelParams -- 4. 全局函数 function closeAll() if ballMgr then ballMgr:exit() end if panelMgr then panelMgr:exit() end end function closePanel() wm:setVisibility(ball, 0) wm:setVisibility(panel, 8) end function openPanel() wm:setVisibility(panel, 0) wm:setVisibility(ball, 8) end function focusIn() panelParams.flags = panelParams.flags & ~0x00000008 wm:updateViewLayout(panel, panelParams) end function focusOut() panelParams.flags = panelParams.flags | 0x00000008 wm:updateViewLayout(panel, panelParams) end -- 业务回调 function onToggle(id, val) gg.toast(id .. ': ' .. (val == 'true' and 'ON' or 'OFF')) end function onSlide(id, val) gg.toast(id .. ': ' .. val) end function btnAction(name) luajava.startThread(function() gg.alert('执行: ' .. name) end) end -- 5. 创建悬浮球 local ballDrawable = luajava.newInstance('android.graphics.drawable.GradientDrawable') ballDrawable:setShape(0) ballDrawable:setCornerRadius(200) ballDrawable:setColor(0xdd1a1c24) ballDrawable:setStroke(1, 0x33444455) ball = luajava.loadlayout({ LinearLayout, layout_width = '42dp', layout_height = '42dp', gravity = 'center', { TextView, text = '●', textColor = 0xaa8899cc, textSize = '16sp', gravity = 'center', layout_width = '42dp', layout_height = '42dp', backgroundColor = 0x00000000, } }) ball:setBackgroundDrawable(ballDrawable) -- 6. 创建面板 HTML local html = [[
开关
]] panel = luajava.webView(function(wv) wv:setBackgroundColor(0x00000000) wv:loadData(html, 'text/html', 'UTF-8') end) -- 7. 添加到屏幕 ballMgr = wm:bindView(ball) ballMgr:addView({ width = -2, height = -2, gravity = 51, x = 300, y = 200, flags = 8 }) ballParams = wm:getLayoutParams(ball) panelMgr = wm:bindView(panel) panelMgr:addView({ width = -1, height = -1, flags = 0x400 | 0x200 | 8 }) panelParams = wm:getLayoutParams(panel) wm:setVisibility(panel, 8) -- 8. 自定义拖动 + 点击 local startX, startY, moved wm:addOnTouchListener(ball, function(v, e) if e:getAction() == e.ACTION_DOWN then startX = e:getRawX(); startY = e:getRawY(); moved = false return true elseif e:getAction() == e.ACTION_MOVE then local dx = e:getRawX() - startX; local dy = e:getRawY() - startY if math.abs(dx) > 5 or math.abs(dy) > 5 then moved = true ballParams.x = ballParams.x + dx; ballParams.y = ballParams.y + dy wm:updateViewLayout(ball, ballParams) startX = e:getRawX(); startY = e:getRawY() end return true elseif e:getAction() == e.ACTION_UP then if not moved then openPanel() end return true end return false end) -- 9. 启动 gg.setVisible(false) ballMgr:wait() ``` 第十五章:常见错误与解决方案 序号 错误信息 原因 解决方法 1 cannot find Xxx (field 'loadlayout') 类未 import 脚本顶部加 import "android.widget.Xxx" 2 Expected receiver of type Xxx, but got ... luajava.newInstance 参数不是字符串 改为 luajava.newInstance('完整包名.类名') 3 attempt to index nil value 变量未定义或作用域问题 检查函数是否全局定义,子线程是否能访问 4 loadlayout error: First value Must be a Class import 了类但没正确使用 检查 import 语句是否正确,类名拼写是否正确 5 WebView 输入框无法输入 FLAG_NOT_FOCUSABLE 阻止 用 focusIn/focusOut 动态切换 flags 6 rlgg.load 后 Lua 代码中断 load 内部异常打断执行 用 pcall(function() rlgg.load(...) end) 包裹 7 界面严重卡顿 backdrop-filter: blur() 过多 删掉所有 backdrop-filter,用静态半透明色代替 8 拖动悬浮球时内部控件误触 setMoveable 与控件冲突 不用 setMoveable,全部用自定义 addOnTouchListener 9 切换页面时闪烁 removeView/addView 之间有空隙 用 setVisibility,先显示新界面再隐藏旧界面 10 悬浮球拖动只沿边缘移动 gravity 设置问题 使用 gravity = 51(TOP\|LEFT)配合 x/y 偏移 11 rlgg.calls 参数传递错误 JS 端参数格式不对 确认参数是数组格式 [参数1, 参数2] 12 no coercible public method Lua 调用 Java 方法时参数类型不匹配 检查参数类型,数字传 number,字符串传 string 第十六章:开发流程清单 1. 确定 UI 风格和功能 2. 写所有 import 语句 3. require('windowManager') 4. luajava.setFloatingWindowHide(false) 隐藏 GG 原生图标 5. 创建悬浮球(原生视图): · 用 GradientDrawable 做圆角背景 · luajava.loadlayout 构建球体 6. 创建面板(WebView): · 编写 HTML 字符串(包含 CSS 和 JS) · luajava.webView 加载 HTML 7. 分别 bindView + addView 添加到屏幕 8. 获取 getLayoutParams 保存引用 9. 给球添加 addOnTouchListener 自定义触摸 10. 定义所有 Lua 回调函数 11. HTML 中用 rlgg 调用 Lua 12. gg.setVisible(false) 隐藏 GG 图标 13. ballMgr:wait() 启动主循环 14. 在 closeAll 中清理所有资源 第十七章:AI 生成代码检查清单 在输出代码前,确认以下所有项: · 所有 import 语句写在脚本最顶部 · luajava.newInstance 使用完整的类名字符串 · luajava.loadlayout 中所有类已 import · 所有函数定义在全局作用域 · 使用了 rlgg.call / rlgg.calls 进行通信 · 输入框有 onfocus / onblur 处理 · HTML 中有点击空白关闭面板的逻辑 · 悬浮球有自定义拖动 + 点击逻辑 · 有 closeAll 或 closePanel 函数 · 最后调用 ballMgr:wait() 或 wm:wait() · 调用了 gg.setVisible(false) · 没有使用 backdrop-filter(会导致卡顿) 控件CSS完整库补充 文本域(Textarea) ```css .textarea { width: 100%; padding: 14px 16px; border: 1px solid rgba(255,255,255,0.1); border-radius: 12px; background: rgba(0,0,0,0.2); color: #c0c0d0; font-family: inherit; font-size: 14px; outline: none; resize: vertical; min-height: 80px; line-height: 1.5; transition: border 0.2s; } .textarea:focus { border-color: #6a9aff; border-width: 2px; padding: 13px 15px; } .textarea::placeholder { color: #555; } ``` 数字步进器(Stepper) ```css .stepper { display: flex; align-items: center; gap: 0; border-radius: 12px; overflow: hidden; border: 1px solid rgba(255,255,255,0.1); } .stepper button { width: 40px; height: 40px; border: none; background: rgba(255,255,255,0.04); color: #c0c0d0; font-size: 18px; cursor: pointer; display: flex; align-items: center; justify-content: center; transition: background 0.15s; } .stepper button:active { background: rgba(255,255,255,0.1); } .stepper .value { width: 50px; text-align: center; font-size: 16px; font-weight: 500; color: #fff; background: rgba(0,0,0,0.2); padding: 8px 0; } ``` 评分星星(Rating) ```css .rating { display: flex; gap: 6px; } .rating .star { font-size: 28px; color: #555; cursor: pointer; transition: color 0.15s, transform 0.15s; } .rating .star.on { color: #ffb300; transform: scale(1.1); } .rating .star:hover { color: #ffb300; } ``` 徽章/角标(Badge) ```css .badge { position: relative; } .badge::after { content: attr(data-count); position: absolute; top: -6px; right: -10px; min-width: 18px; height: 18px; line-height: 18px; text-align: center; background: #ff3b30; color: #fff; border-radius: 9px; font-size: 10px; font-weight: 600; padding: 0 5px; } .badge.dot::after { width: 8px; height: 8px; min-width: 8px; content: ''; top: -2px; right: -2px; border-radius: 50%; padding: 0; } ``` 头像(Avatar) ```css .avatar { width: 44px; height: 44px; border-radius: 50%; background: linear-gradient(135deg, #667eea, #764ba2); display: flex; align-items: center; justify-content: center; font-size: 18px; color: #fff; font-weight: 600; flex-shrink: 0; overflow: hidden; } .avatar.sm { width: 32px; height: 32px; font-size: 14px; } .avatar.lg { width: 60px; height: 60px; font-size: 24px; } .avatar.xl { width: 80px; height: 80px; font-size: 30px; } .avatar.square { border-radius: 12px; } .avatar.bordered { border: 2px solid rgba(255,255,255,0.2); } ``` 头像组(Avatar Group) ```css .avatar-group { display: flex; } .avatar-group .avatar { margin-right: -10px; border: 2px solid #1a1c24; } ``` 分隔线(Divider) ```css .divider { width: 100%; height: 1px; background: rgba(255,255,255,0.06); margin: 8px 0; } .divider.vertical { width: 1px; height: 100%; min-height: 20px; margin: 0 8px; } .divider.text { display: flex; align-items: center; gap: 12px; background: transparent; height: auto; } .divider.text::before, .divider.text::after { content: ''; flex: 1; height: 1px; background: rgba(255,255,255,0.06); } .divider.text span { font-size: 11px; color: #666; } ``` 加载动画(Spinner/Loader) ```css .spinner { width: 30px; height: 30px; border: 3px solid rgba(255,255,255,0.1); border-top-color: #6a9aff; border-radius: 50%; animation: spin 0.8s linear infinite; } @keyframes spin { to { transform: rotate(360deg); } } .spinner.sm { width: 20px; height: 20px; border-width: 2px; } .spinner.lg { width: 44px; height: 44px; border-width: 4px; } ``` 骨架屏(Skeleton) ```css .skeleton { background: linear-gradient(90deg, rgba(255,255,255,0.04) 25%, rgba(255,255,255,0.08) 50%, rgba(255,255,255,0.04) 75%); background-size: 200% 100%; animation: skeleton-shimmer 1.5s infinite; border-radius: 4px; } @keyframes skeleton-shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } } .skeleton.text { height: 14px; width: 80%; } .skeleton.title { height: 20px; width: 60%; } .skeleton.avatar { height: 44px; width: 44px; border-radius: 50%; } .skeleton.card { height: 80px; width: 100%; border-radius: 12px; } ``` 提示气泡(Tooltip) ```css .tooltip { position: relative; } .tooltip::after { content: attr(data-tip); position: absolute; bottom: calc(100% + 8px); left: 50%; transform: translateX(-50%); background: rgba(0,0,0,0.85); color: #fff; padding: 6px 12px; border-radius: 6px; font-size: 11px; white-space: nowrap; opacity: 0; pointer-events: none; transition: opacity 0.2s; } .tooltip:hover::after { opacity: 1; } ``` 弹出菜单(Popover Menu) ```css .popover { position: absolute; top: 100%; right: 0; min-width: 150px; background: rgba(30,32,40,0.95); backdrop-filter: blur(20px); border: 1px solid rgba(255,255,255,0.08); border-radius: 12px; padding: 6px 0; box-shadow: 0 8px 32px rgba(0,0,0,0.5); z-index: 100; opacity: 0; transform: translateY(-8px); pointer-events: none; transition: all 0.2s; } .popover.show { opacity: 1; transform: translateY(0); pointer-events: auto; } .popover-item { padding: 10px 16px; font-size: 13px; color: #c0c0d0; cursor: pointer; transition: background 0.15s; display: flex; align-items: center; gap: 8px; } .popover-item:hover { background: rgba(255,255,255,0.06); color: #fff; } .popover-item.danger { color: #ff6666; } ``` 底部弹出面板(Bottom Sheet) ```css .bottom-sheet-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 90; opacity: 0; pointer-events: none; transition: opacity 0.3s; } .bottom-sheet-overlay.show { opacity: 1; pointer-events: auto; } .bottom-sheet { position: fixed; bottom: 0; left: 0; right: 0; background: rgba(24,26,32,0.95); backdrop-filter: blur(20px); border-radius: 20px 20px 0 0; padding: 20px 16px; z-index: 91; transform: translateY(100%); transition: transform 0.3s cubic-bezier(0.4,0,0.2,1); max-height: 70vh; overflow-y: auto; } .bottom-sheet.show { transform: translateY(0); } .bottom-sheet .handle { width: 36px; height: 4px; background: rgba(255,255,255,0.2); border-radius: 2px; margin: 0 auto 16px; } ``` 手风琴折叠面板(Accordion) ```css .accordion-item { border-bottom: 1px solid rgba(255,255,255,0.06); } .accordion-header { display: flex; align-items: center; justify-content: space-between; padding: 14px 0; cursor: pointer; font-size: 14px; color: #c0c0d0; transition: color 0.15s; } .accordion-header:hover { color: #fff; } .accordion-header .arrow { transition: transform 0.3s; font-size: 12px; color: #666; } .accordion-item.open .accordion-header .arrow { transform: rotate(180deg); } .accordion-body { max-height: 0; overflow: hidden; transition: max-height 0.3s ease; } .accordion-item.open .accordion-body { max-height: 300px; } ``` 表格(Table) ```css .table { width: 100%; border-collapse: collapse; font-size: 13px; } .table th { text-align: left; padding: 10px 12px; color: #888; font-weight: 500; font-size: 11px; text-transform: uppercase; letter-spacing: 1px; border-bottom: 1px solid rgba(255,255,255,0.1); } .table td { padding: 10px 12px; color: #c0c0d0; border-bottom: 1px solid rgba(255,255,255,0.04); } .table tr:hover td { background: rgba(255,255,255,0.02); } .table .align-right { text-align: right; } .table .align-center { text-align: center; } ``` 标签页卡片(Card with Tabs) ```css .tab-card { background: rgba(255,255,255,0.02); border: 1px solid rgba(255,255,255,0.06); border-radius: 16px; overflow: hidden; } .tab-card-header { display: flex; border-bottom: 1px solid rgba(255,255,255,0.06); } .tab-card-tab { flex: 1; padding: 12px; text-align: center; font-size: 13px; color: #888; cursor: pointer; transition: all 0.2s; border-bottom: 2px solid transparent; } .tab-card-tab.active { color: #fff; border-bottom-color: #6a9aff; } .tab-card-body { padding: 16px; } .tab-card-panel { display: none; } .tab-card-panel.active { display: block; } ``` 键盘快捷键提示(KBD) ```css kbd { display: inline-block; padding: 2px 6px; background: rgba(255,255,255,0.06); border: 1px solid rgba(255,255,255,0.1); border-radius: 4px; font-size: 11px; font-family: monospace; color: #aaa; box-shadow: 0 1px 0 rgba(0,0,0,0.3); } ``` 滚动条美化 ```css ::-webkit-scrollbar { width: 4px; height: 4px; } ::-webkit-scrollbar-track { background: transparent; } ::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 2px; } ::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.2); } ``` 圆角容器(带状态指示) ```css .status-card { background: rgba(255,255,255,0.03); border: 1px solid rgba(255,255,255,0.06); border-radius: 14px; padding: 14px 16px; position: relative; padding-left: 20px; } .status-card::before { content: ''; position: absolute; left: 8px; top: 50%; transform: translateY(-50%); width: 4px; height: 60%; border-radius: 2px; } .status-card.success::before { background: #34c759; } .status-card.warning::before { background: #ff9f0a; } .status-card.error::before { background: #ff3b30; } .status-card.info::before { background: #6a9aff; } ``` 响应式网格 ```css .grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; } .grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; } .grid-4 { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; gap: 8px; } ``` 媒体查询(横屏适配) ```css @media (orientation: landscape) { .container { max-width: 600px; margin: 0 auto; } .grid-2 { grid-template-columns: 1fr 1fr 1fr 1fr; } } ``` 第十八章:关键陷阱与注意事项(必读) 以下问题在通用开发文档中容易被忽略,但在 RLGG 环境中几乎必然遇到,请严格遵守。 18.1 luajava.newInstance 必须使用完整类名字符串 这是最高频的错误。newInstance 的第一个参数必须是完整的 Java 包名字符串,不能传类引用。 ```lua -- 错误(报错 Expected receiver of type Xxx) local d = luajava.newInstance(GradientDrawable) -- 正确 local d = luajava.newInstance('android.graphics.drawable.GradientDrawable') ``` 此规则同样适用于所有 newInstance 调用,包括 ColorDrawable、StateListDrawable 等。 18.2 禁用 backdrop-filter RLGG 的 WebView 渲染能力有限,任何 backdrop-filter: blur() 都会导致严重卡顿甚至界面冻结。 · 不要 使用 CSS 的 backdrop-filter 或 -webkit-backdrop-filter。 · 毛玻璃效果只能用静态半透明背景色 + 多层渐变高光 + 细边框来模拟。 18.3 悬浮球触摸:不要混用 setMoveable 和自定义监听 · wm:setMoveable(view, true) 和 wm:addOnTouchListener(view, func) 不能同时使用。 · 推荐完全放弃 setMoveable,统一用自定义触摸逻辑。 · 点击判定阈值 math.abs(dx) > 5 中的 5 可调为 3~8,防止手抖误触。 18.4 多界面切换避免闪烁 · 使用 wm:setVisibility(view, 0/8) 而非 removeView + addView。 · 切换顺序:先显示新界面,再隐藏旧界面。 · 多个 WebView 场景:启动时全部加载并显示约 200ms,然后隐藏非当前页面,可避免首次切换黑屏。 18.5 输入框聚焦的 flags 操作 ```lua -- 聚焦:去掉 NOT_FOCUSABLE params.flags = params.flags & ~0x00000008 wm:updateViewLayout(webView, params) -- 失焦:恢复 NOT_FOCUSABLE params.flags = params.flags | 0x00000008 wm:updateViewLayout(webView, params) ``` HTML 中每个 和