> For AI agents: the complete documentation index is available at /zh/llms.txt, the full documentation bundle is available at /zh/llms-full.txt, and this page is available as Markdown at /zh/guide/ui/layout/linear-layout.md.

# 线性布局

如果你需要一种简单的布局方式，仅需按顺序排列子元件，并不想考虑[弹性布局](/zh/guide/ui/layout/flexible-box-layout.md)和[网格布局](/zh/guide/ui/layout/grid-layout.md)的复杂特性（例如缩小和放置问题），那么 Lynx 的**线性布局**是个不错的选择。该布局受到 Android [linear layout](https://developer.android.com/develop/ui/views/layout/linear) 启发。

此外，它还是 Lynx 的**默认布局**。在该布局下，你也可以使用 Web 中的对齐属性：[`align-items`](/zh/api/css/properties/align-items.md)、[`align-self`](/zh/api/css/properties/align-self.md) 和 [`justify-content`](/zh/api/css/properties/justify-content.md)。有关支持的属性，请参见[参考](#参考)部分。

## 如何构建一个线性布局？

### 第一步：应用 `display: linear`

要应用线性布局，只需在元件上设置 `display: linear`。

```css
display: linear;
```

### 第二步：设置布局方向

与[弹性布局（flexible box layout）](/zh/guide/ui/layout/flexible-box-layout.md)类似，线性布局同样利用**主轴**（main axis）和**交叉轴**（cross axis）来布局。**主轴**是沿着线性布局子元件排列的方向延伸的轴，而**交叉轴**与主轴垂直。

通过在父容器中的调整 [`linear-direction`](/zh/api/css/properties/linear-direction.md) 属性可以定义主轴方向（默认是竖直方向）。这一属性与弹性布局（flexible box layout）中的 [`flex-direction`](/zh/api/css/properties/flex-direction.md) 相似。

```css
linear-direction: column;
```

![](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/lynx-website/assets/doc/linear_main_axis.png)
### 第三步：沿主轴对齐子元件

使用 [`justify-content`](/zh/api/css/properties/justify-content.md) 属性，可以来控制子元件在主轴方向上的相对位置。以下例子中的主轴方向为竖轴。

**下面是一个示例: layout**

**Entry:** `src/linear_justify_content`
**Bundle:** `dist/linear_justify_content.lynx.bundle` | Web: `dist/linear_justify_content.web.bundle`

```tsx {9,16,23}
import { root } from "@lynx-js/react";

import "./index.scss";

const LinearJustifyContentExample = () => {
  return (
    <scroll-view>
      <text className="title_style">justify-content: start</text>
      <view className="container" style={{ justifyContent: "start" }}>
        <view className="item1"></view>
        <view className="item2"></view>
        <view className="item3"></view>
      </view>

      <text className="title_style">justify-content: end</text>
      <view className="container" style={{ justifyContent: "end" }}>
        <view className="item1"></view>
        <view className="item2"></view>
        <view className="item3"></view>
      </view>

      <text className="title_style">justify-content: center</text>
      <view className="container" style={{ justifyContent: "center" }}>
        <view className="item1"></view>
        <view className="item2"></view>
        <view className="item3"></view>
      </view>

      <text className="title_style">justify-content: space-between</text>
      <view className="container" style={{ justifyContent: "space-between" }}>
        <view className="item1"></view>
        <view className="item2"></view>
        <view className="item3"></view>
      </view>
    </scroll-view>
  );
};

root.render(<LinearJustifyContentExample />);

if (import.meta.webpackHot) {
  import.meta.webpackHot.accept();
}

```



### 第四步：沿交叉轴对齐子元件

可以通过容器上使用 [`align-items`](/zh/api/css/properties/align-items.md) 或在子元件上使用 [`align-self`](/zh/api/css/properties/align-self.md) 实现元件在交叉轴上的对齐。

在下面的例子中，交叉轴是竖直方向，容器中应用了 `align-items: center`，因此子元件在交叉轴方向上居中对齐。

**下面是一个示例: layout**

**Entry:** `src/linear_align_items`
**Bundle:** `dist/linear_align_items.lynx.bundle` | Web: `dist/linear_align_items.web.bundle`

```tsx {27}
import { root } from "@lynx-js/react";

const LinearAlignItemsExample = () => {
  return (
    <scroll-view>
      <text
        style={{
          fontSize: "45rpx",
          fontWeight: "bold",
          marginLeft: "auto",
          marginRight: "auto",
          textAlign: "center",
          color: "linear-gradient(to right, rgb(255,53,26), rgb(0,235,235))",
        }}
      >
        align-items: center
      </text>
      <view
        className="container"
        style={{
          display: "linear",
          linearOrientation: "horizontal",
          height: "300px",
          width: "90%",
          padding: "5px",
          margin: "10px",
          justifyContent: "center",
          marginLeft: "auto",
          marginRight: "auto",
          alignItems: "center",
          border: "1px solid #000",
          borderRadius: "6px",
        }}
      >
        <view
          style={{
            margin: "2px",
            height: "100px",
            width: "30%",
            backgroundColor: "rgb(255,53,26)",
            borderRadius: "6px",
          }}
        >
        </view>
        <view
          style={{
            margin: "2px",
            height: "100px",
            width: "30%",
            backgroundColor: "rgb(0,235,235)",
            borderRadius: "6px",
          }}
        >
        </view>
        <view
          style={{
            margin: "2px",
            height: "100px",
            width: "30%",
            backgroundColor: "rgb(255,53,26)",
            borderRadius: "6px",
          }}
        >
        </view>
      </view>
    </scroll-view>
  );
};

root.render(<LinearAlignItemsExample />);

if (import.meta.webpackHot) {
  import.meta.webpackHot.accept();
}

```



通过将第一个子元件的 `align-self` 设置为 `end`，你可以观察到之前的 `align-items` 的行为被覆盖了，第一个元件现在位于容器底部。

**下面是一个示例: layout**

**Entry:** `src/linear_align_self`
**Bundle:** `dist/linear_align_self.lynx.bundle` | Web: `dist/linear_align_self.web.bundle`

```tsx {17,27}
import { root } from "@lynx-js/react";

const LinearAlignSelfExample = () => {
  return (
    <scroll-view>
      <view
        className="container"
        style={{
          display: "linear",
          linearOrientation: "horizontal",
          height: "300px",
          width: "90%",
          padding: "5px",
          margin: "10px",
          justifyContent: "center",
          marginLeft: "auto",
          marginRight: "auto",
          alignItems: "center",
          border: "1px solid #000",
          borderRadius: "6px",
        }}
      >
        <view
          style={{
            margin: "2px",
            height: "100px",
            width: "30%",
            alignSelf: "end",
            backgroundColor: "rgb(255,53,26)",
            borderRadius: "6px",
          }}
        >
        </view>
        <view
          style={{
            margin: "2px",
            height: "100px",
            width: "30%",
            backgroundColor: "rgb(0,235,235)",
            borderRadius: "6px",
          }}
        >
        </view>
        <view
          style={{
            margin: "2px",
            height: "100px",
            width: "30%",
            backgroundColor: "rgb(255,53,26)",
            borderRadius: "6px",
          }}
        >
        </view>
      </view>
    </scroll-view>
  );
};

root.render(<LinearAlignSelfExample />);

if (import.meta.webpackHot) {
  import.meta.webpackHot.accept();
}

```



::: info
请注意，当父元件的交叉轴尺寸（例如在 `linear-direction: column` 时的 `width`）被指定，且子元件在该方向的尺寸未设置（或为 `auto`）时，子元件在交叉轴上的尺寸将增长以填充父元件。
:::

### 第五步：规定主轴上动态尺寸（拓展）

通过为子元件设置 [`linear-weight`](/zh/api/css/properties/linear-weight.md) 属性，可以定义权重值。容器会根据所有子元件的权重值比例及剩余空间自动分配主轴方向上子元件的尺寸，达到动态尺寸调整的效果。

**下面是一个示例: layout**

**Entry:** `src/linear_weight`
**Bundle:** `dist/linear_weight.lynx.bundle` | Web: `dist/linear_weight.web.bundle`

```tsx {33,42,51}
import { root } from "@lynx-js/react";

const LinearAlginItemsExample = () => {
  return (
    <scroll-view>
      <text
        style={{
          fontSize: "45rpx",
          fontWeight: "bold",
          marginLeft: "auto",
          marginRight: "auto",
          textAlign: "center",
          color: "linear-gradient(to right, rgb(255,53,26), rgb(0,235,235))",
        }}
      >
        linear-weight: 0.5 : 2 : 0.5
      </text>
      <view
        style={{
          display: "linear",
          linearOrientation: "vertical",
          height: "300px",
          width: "90%",
          padding: "5px",
          margin: "10px",
          marginLeft: "auto",
          marginRight: "auto",
          border: "1px solid #000",
          borderRadius: "6px",
        }}
      >
        <view
          style={{
            margin: "5px",
            linearWeight: 0.5,
            backgroundColor: "rgb(255,53,26)",
            borderRadius: "6px",
          }}
        >
        </view>
        <view
          style={{
            margin: "5px",
            linearWeight: 2,
            backgroundColor: "rgb(0,235,235)",
            borderRadius: "6px",
          }}
        >
        </view>
        <view
          style={{
            margin: "5px",
            linearWeight: 0.5,
            backgroundColor: "rgb(255,53,26)",
            borderRadius: "6px",
          }}
        >
        </view>
      </view>
    </scroll-view>
  );
};

root.render(<LinearAlginItemsExample />);

if (import.meta.webpackHot) {
  import.meta.webpackHot.accept();
}

```



在上面的例子中，`linear-weight` 是一个无单位的比例值，表示每个子元件在主轴上的空间份额。三个子元件按 `0.5 : 2 : 0.5` 的比例分配主轴上的空间。

## 参考

目前，线性布局支持以下布局属性：

- **特有的 CSS 属性**
  - [`linear-direction`](/zh/api/css/properties/linear-direction.md)
  - [`linear-weight`](/zh/api/css/properties/linear-weight.md)

- **对齐属性**
  - [`justify-content`](/zh/api/css/properties/justify-content.md)
  - [`align-items`](/zh/api/css/properties/align-items.md)
  - [`align-self`](/zh/api/css/properties/align-self.md)

- **其它属性**

  其它属性如 [`order`](/zh/api/css/properties/order.md)、[`aspect-ratio`](/zh/api/css/properties/aspect-ratio.md) 等，在这里就不一一列举，具体属性支持可以参考 [API 参考](/zh/api/css/properties.md)。
