# RandomLayout

> 无自动排布——手动定位（setBounds）或相对定位（setRelativeBounds）。

Source: https://crossfly.dev/zh/docs/ui/layouts/random-layout — CrossFly documentation. Full docs as one file: https://crossfly.dev/llms-full.txt

# RandomLayout

不会自动排布任何东西：每个子组件都需要 bounds。当你调用 `add(...)` 而未设定布局时，它是**默认布局**。尽管名字如此，它却是**浮层和浮动卡片**的基础。

> 🔴 · **包：** `dev.crossfly.ui.layout` · **继承自：** `Layout`

## 何时使用
- 浮层、浮动卡片、自由定位。
- 你自行控制 bounds 的自定义布局。

## 何时不要使用
- 常规屏幕 → 使用会自动排布的布局（Vertical/Horizontal/Grid）。

## `add(...)`
```java
public Layout add(Object... params);   // 只收集组件；不对其定位
```

## 两种定位方式
1. **手动（像素——非响应式）：** `filho.setBounds(x, y, w, h)`。
2. **相对（响应式——推荐）：** `filho.setRelativeBounds(Relative x, Relative y, Relative w, Relative h)`。
   `RandomLayout` 的 `doLayout()` 会为每个具有相对 bounds 的子组件自动调用 `Relative.setBounds(c)`。

## 完整示例（复制、粘贴并运行）
```java
package com.exemplo;

import dev.crossfly.ui.*;
import dev.crossfly.ui.layout.*;
import dev.crossfly.ui.graphics.*;

/** 可执行的主界面。将 launcher 指向此类（classname）并运行：gradlew :lwjgl3:run */
public class MainWindow extends Window
{
   @Override
   public void onShow() throws Exception
   {
      super.onShow();

      // Window 本身默认使用 RandomLayout（无 setLayout）
      Component card = new Component();
      card.setLayout(new VerticalLayout(Position.START, Position.START));
      card.add(LAY_AUTO, new Label("Cartão flutuante"));
      card.setRelativeBounds(
         Relative.CENTER,                            // x：居中
         Relative.CENTER,                            // y：居中
         Relative.ofScreen(0.8f, ScreenSize.MIN),   // 宽度：较短边的 80%
         Relative.ofFont(10f)                       // 高度：10 行字体
      );
      add(card);                                     // RandomLayout 解析相对边界
   }
}
```

> 下面的片段只展示**核心部分**（放进 `onShow()` 里的内容）。要运行它们，请将其放入上面的 `MainWindow` 骨架中。

## 基本用法（响应式居中卡片）
```java
Component overlay = new Component();          // 无 setLayout → RandomLayout
Component card = new Component();
card.setRelativeBounds(
   Relative.CENTER,                            // x：居中
   Relative.CENTER,                            // y：居中
   Relative.ofScreen(0.8f, ScreenSize.MIN),   // 宽度：较短边的 80%
   Relative.ofFont(10f)                       // 高度：10 行字体
);
overlay.add(card);                             // RandomLayout 解析相对边界
```

## 最佳实践
- **优先使用 `setRelativeBounds`** 而非 `setBounds`——保持响应式。
- 如果**必须**使用固定的 `setBounds`，请在 `Window.onReposition()`（resize 时调用）中重新计算。

## 常见错误
- 使用固定的 `setBounds(x,y,w,h)`，导致在其他屏幕/密度下 UI 出问题。
- 忘记若没有 bounds（既无固定也无相对），子组件会停在 `(0,0)` 且尺寸为 0。

## 另请参阅
- [响应式设计](/docs/ui/layouts/responsiveness) · [RelativeLayout](/docs/ui/layouts/relative-layout)
