> For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt.

# deprecated linear-layout-gravity

APISummary
:::warning
该 API 已被废弃。
:::

## 介绍

子元件在[线性布局](/next/zh/guide/ui/layout/linear-layout.md)中垂直于布局方向上的位置，作用在子元件上。类似于[弹性布局](/next/zh/guide/ui/layout/flexible-box-layout.md)的 [`align-self`](/next/zh/api/css/properties/align-self.md)。

当在线性布局中同时使用 `linear-layout-gravity`、[`align-self`](/next/zh/api/css/properties/align-self.md)、[`linear-cross-gravity`](/next/zh/api/css/properties/linear-cross-gravity.md) 和 [`align-items`](/next/zh/api/css/properties/align-items.md) 时，优先级从前到后依次降低。优先级高的会覆盖优先级低的样式。

## 使用示例

**下面是一个示例: css-api**

**Entry:** `src/linear-layout-gravity`
**Bundle:** `dist/linear-layout-gravity.lynx.bundle`

```tsx
import { root, useState } from "@lynx-js/react";
import "./index.scss";

const LinearLayoutGravity = () => {
  const [linearOrientation, setLinearOrientation] = useState(false);
  const containerStyle = {
    linearOrientation: linearOrientation ? "horizontal" : "vertical" as "horizontal" | "vertical",
  };
  const buttonStyle = {
    width: "20%",
    height: "50px",
    backgroundColor: "red" as const,
  };
  const text1Style = {
    fontSize: "40rpx" as const,
    marginRight: "12rpx",
    fontWeight: "900" as const,
    color: "linear-gradient(120deg ,#0095ff 30% ,#42d392 100%)" as const,
  };
  const text2Style = {
    fontSize: "50rpx" as const,
    alignSelf: "center" as const,
    fontWeight: "900" as const,
    color: "linear-gradient(120deg,#0095ff 30%,#42d392 100%)" as const,
  };
  const Container1View1Style = {
    width: "20px",
    height: "20px",
    linearLayoutGravity: "none" as const,
    backgroundColor: "red" as const,
  };
  const Container2View1Style = {
    width: "20px",
    height: "20px",
    linearLayoutGravity: "start" as const,
    backgroundColor: "red" as const,
  };
  const Container3View1Style = {
    width: "20px",
    height: "20px",
    backgroundColor: "red" as const,
    linearLayoutGravity: "end" as const,
  };
  const Container4View1Style = {
    width: "20px",
    height: "20px",
    backgroundColor: "red" as const,
    linearLayoutGravity: "center" as const,
  };
  const Container5View1Style = {
    width: "20px",
    height: "20px",
    backgroundColor: "red" as const,
    linearLayoutGravity: "stretch" as const,
  };
  const view2Style = {
    width: "20px",
    height: "20px",
    backgroundColor: "yellow" as const,
  };
  const view3Style = {
    width: "20px",
    height: "20px",
    backgroundColor: "green" as const,
  };

  return (
    <scroll-view className="root" scroll-orientation="vertical">
      <view style={buttonStyle} bindtap={() => setLinearOrientation(!linearOrientation)}></view>
      <text style={text1Style}>Click above red button to switch linear-direction</text>
      <text style={text2Style}>Current: "{linearOrientation ? "row" : "column"}"</text>
      <text className="title">linear-layout-gravity:none;(default)</text>
      <view className="linear_container" style={containerStyle}>
        <view style={Container1View1Style}></view>
        <view style={view2Style}></view>
        <view style={view3Style}></view>
      </view>
      <text className="title">linear-layout-gravity:start;</text>
      <view className="linear_container" style={containerStyle}>
        <view style={Container2View1Style}></view>
        <view style={view2Style}></view>
        <view style={view3Style}></view>
      </view>
      <text className="title">linear-layout-gravity:end;</text>
      <view className="linear_container" style={containerStyle}>
        <view style={Container3View1Style}></view>
        <view style={view2Style}></view>
        <view style={view3Style}></view>
      </view>
      <text className="title">linear-layout-gravity:center;</text>
      <view className="linear_container" style={containerStyle}>
        <view style={Container4View1Style}></view>
        <view style={view2Style}></view>
        <view style={view3Style}></view>
      </view>
      <text className="title">linear-layout-gravity:stretch;</text>
      <view className="linear_container" style={containerStyle}>
        <view style={Container5View1Style}></view>
        <view style={view2Style}></view>
        <view style={view3Style}></view>
      </view>
    </scroll-view>
  );
};

root.render(<LinearLayoutGravity />);

```

v0.11.0



## 语法

```css
linear-layout-gravity: none;
linear-layout-gravity: stretch;
linear-layout-gravity: start;
linear-layout-gravity: end;
linear-layout-gravity: center;
```

### 取值

- `none`

  **默认值**。当父容器（linear containers）的交叉轴上的大小（如 [`linear-direction: column`](/next/zh/api/css/properties/linear-direction.md) 时的 [`width`](/next/zh/api/css/properties/width.md)）固定，且该子元件在该方向的宽度为 `auto` 时，`none` 将等同于 `fill-horizontal` 或 `fill-vertical`。在其他情况下则等同与 `left` 或 `top`。

- `stretch`

  填满父容器交叉轴。

- `start`

  对齐父容器的交叉轴开始端。

- `end`

  对齐父容器的交叉轴结束端。

- `center`

  元件在父容器交叉轴居中。

- `top` deprecated

  不推荐使用，使用 `start` 代替。对齐父容器的顶端，横向布局生效。

- `bottom` deprecated

  不推荐使用，使用 `end` 代替。对齐父容器的底端，横向布局生效。

- `left` deprecated

  不推荐使用，使用 `start` 代替。对齐父容器的左侧，纵向布局生效。

- `right` deprecated

  不推荐使用，使用 `end` 代替。对齐父容器的右侧，纵向布局生效。

- `center-vertical` deprecated

  不推荐使用，使用 `center` 代替。垂直方向上居中，父容器横向布局生效。

- `center-horizontal` deprecated

  不推荐使用，使用 `center` 代替。水平方向上居中，父容器纵向布局生效。

- `fill-vertical` deprecated

  不推荐使用，使用 `stretch` 代替。纵向填满整个容器，父容器横向布局生效。

- `fill-horizontal` deprecated

  不推荐使用，使用 `stretch` 代替。横向填满整个容器，父容器纵向布局生效。

## 形式定义

| 初始值 | none |
| 适用元素 | linear items |
| 是否支持继承 | 否 |
| 是否支持动画 |  |


## 形式语法

```
linear-layout-gravity = none | stretch | start | end | center
```

## 与 Web 的区别

- Web 无该样式。

## 兼容性

**Compatibility Table**

**Query:** `css.properties.linear-layout-gravity`

[Source](https://github.com/lynx-family/lynx/blob/develop/tools/css_generator/css_defines/82-linear-layout-gravity.json)

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| linear-layout-gravity | Yes (1.0) | Yes (3.4) | Yes (1.0) | Yes (1.0) | Yes (1.0) | Yes |
| value1 | Yes (1.6) | Yes (3.4) | Yes (1.6) | Yes (1.6) | Yes (1.6) | Yes |

**`linear-layout-gravity`**

The position of the child element in the linear container, perpendicular to the layout direction.

**Specifications:**

**`value1`**

<code>stretch</code>, <code>start</code>, <code>end</code>, <code>center</code>


## 常见问题

- `linear-layout-gravity` 中 `none` 与 `top`/`left` 并不相同，具体区别如下：
  - 当父亲的交叉轴为固定大小时，`none` 与 `fill-vertical`/`fill-horizontal` 表现相同，即撑满父亲非布局方向。
  - 当父亲交叉轴不是固定大小时，`none` 与 `top`/`right` 表现相同。
