// 文档地址 https://docs.hamibot.com/

const { pay_password } = hamibot.env;

var floatyWin = floaty.window(
<vertical padding="4" h="260dp" layout_weight="1">
<horizontal gravity="center" marginTop="2" >
<text text="计时:" textSize="12" />
<text id="timeDisplay" textSize="12" layout_weight="1" textColor="#FF0000" />
</horizontal>
<horizontal gravity="center_vertical" marginTop="2" visibility="gone">
<text text="密码:" textSize="12" />
<input id="pwdInput" text="" password="true" layout_weight="1" hint="支付密码" width="100dp" textSize="10" />
</horizontal>
<horizontal gravity="center_vertical" marginTop="2">
<text text="数量:" textSize="10" />
<button id="countToggleBtn" text="1" h="40"layout_weight="1" textColor="white" textSize="10" marginLeft="5" />
</horizontal>
<horizontal gravity="center" marginTop="2">
<button id="shopType" text="送到家" h="40"layout_weight="1" style="Widget.AppCompat.Button.Colored" textSize="10" />
</horizontal>
<horizontal gravity="center" marginTop="1">
<button id="startBtn" text="开始" h="40" layout_weight="1" style="Widget.AppCompat.Button.Colored" textSize="10" />
</horizontal>

<horizontal gravity="center"  marginTop="1">
  <button id="closeBtn" text="关闭" h="40" layout_weight="1" style="Widget.AppCompat.Button.Colored" textSize="10" />
</horizontal>

</vertical>
);
// 初始化变量
var shopTypeStr = "送到家";
var selectedCount = 1;
var isRunning = true;
var isExpanded = true;

ui.run(function() {
floatyWin.pwdInput.setText(pay_password);
});

// 更新时间的函数 - 只显示时分秒
function updateTime() {
while (isRunning) {
ui.run(function() {
var now = new Date();
var timeStr = util.format("%s:%s:%s:%s",
now.getHours(), now.getMinutes(), now.getSeconds(), now.getMilliseconds());
floatyWin.timeDisplay.setText(timeStr);
});
sleep(88);
}
}

// 数量切换按钮点击事件
floatyWin.shopType.click(function() {
shopTypeStr = shopTypeStr === "送到家" ? "到店取" : "送到家";
floatyWin.shopType.setText(shopTypeStr.toString());
});

// 数量切换按钮点击事件
floatyWin.countToggleBtn.click(function() {
selectedCount = selectedCount === 1 ? 2 : 1;
floatyWin.countToggleBtn.setText(selectedCount.toString());
});
// 启动时间更新线程
threads.start(updateTime);

// 开始按钮点击事件
floatyWin.startBtn.click(function() {
var password = floatyWin.pwdInput.text();
toastLog("开始执行\n数量: " + selectedCount + "\n密码: " + password);
});

// 关闭按钮点击事件
floatyWin.closeBtn.click(function() {
isRunning = false;
floatyWin.close();
exit();
});

// 保持脚本运行
setInterval(function() {}, 500);

上下列间距变少,讲页面移动到屏幕中间

已存档
y3rtmwo5zq · 1 个月前

回答

Hamibot AI
1
· 1 个月前

要调整悬浮窗的上下列间距并使页面居中,需修改布局参数和添加位置设置。以下是修改后的代码:

// 文档地址 https://docs.hamibot.com/
const { pay_password } = hamibot.env;

var floatyWin = floaty.window(
 <vertical padding="4" h="260dp" layout_weight="1">
    <horizontal gravity="center" marginTop="1"> <!-- 减小间距 -->
      <text text="计时:" textSize="12" />
      <text id="timeDisplay" textSize="12"  layout_weight="1"  textColor="#FF0000" />
    </horizontal>
    <horizontal gravity="center_vertical" marginTop="1" visibility="gone"> <!-- 减小间距 -->
      <text text="密码:" textSize="12" />
      <input id="pwdInput" text="" password="true" layout_weight="1" hint="支付密码" width="100dp" textSize="10" />
    </horizontal>
    <horizontal gravity="center_vertical" marginTop="1"> <!-- 减小间距 -->
      <text text="数量:" textSize="10"  />
      <button id="countToggleBtn" text="1" h="40"layout_weight="1"  textColor="white" textSize="10" marginLeft="5" />
    </horizontal>
  	<horizontal gravity="center" marginTop="1"> <!-- 减小间距 -->
      <button id="shopType" text="送到家" h="40"layout_weight="1"  style="Widget.AppCompat.Button.Colored" textSize="10"  />
    </horizontal>
  	<horizontal gravity="center" marginTop="1"> <!-- 减小间距 -->
      <button id="startBtn" text="开始" h="40" layout_weight="1" style="Widget.AppCompat.Button.Colored" textSize="10" />
    </horizontal>
    
    <horizontal gravity="center"  marginTop="1"> <!-- 减小间距 -->
      <button id="closeBtn" text="关闭" h="40" layout_weight="1" style="Widget.AppCompat.Button.Colored" textSize="10" />
    </horizontal>
  </vertical>
);

// 添加居中显示逻辑
floatyWin.setPosition(device.width / 2, device.height / 2); // 自动计算屏幕中心坐标

// 其余原有逻辑保持不变...

主要修改点:

  1. 将所有marginTop值统一调整为"1"(原值为"2"、"1"等)
  2. 在创建悬浮窗后添加setPosition方法,通过device.width/device.height自动计算屏幕中心坐标
  3. 保持原有布局结构不变,仅调整间距参数

注意:device.widthdevice.height是Hamibot提供的设备信息接口,可获取当前屏幕分辨率,确保悬浮窗居中显示。

ℹ️ 本回答由 AI 生成,内容仅供参考