Skip to content

自动宽度 ​

关键属性 ​

autoWidth: number | IUnitData ​

自动宽度权重,分配剩余宽度给此元素(忽略 width),类似 Flex 的 grow / shrink,默认为 0。

设为大于 0 的值会自动扩充宽度,一般设为 1,已支持设置百分比宽度。

ts
interface IUnitData {
  type: 'percent' | 'px'
  value: number
}

rect.autoWidth = {
  type: 'percent',
  value: 0.5, // 50%
}

归属 ​

UI 元素 ​

示例 ​

自动宽度 ​

ts
// #自动布局 - 自动宽度 [自动宽度(填充剩余宽度)]
import { Leafer, Box } from 'leafer-ui'
import { Flow } from '@leafer-in/flow'  // 导入自动布局插件

const leafer = new Leafer({ view: window })

const flow = new Flow({
    fill: '#676',
    width: 100,
    height: 100,
    children: [
        new Box({ fill: '#FF4B4B', children: [{ tag: 'Text', text: '1', fill: 'white', textAlign: 'center', verticalAlign: 'middle', width: 25, height: 20 }] }),
        new Box({ fill: '#FEB027', children: [{ tag: 'Text', text: '2', fill: 'white', textAlign: 'center', verticalAlign: 'middle', width: 25, height: 40 }] }),
        new Box({
            autoWidth: 1, // 自动宽度(填充剩余宽度)
            fill: '#79CB4D', children: [{ tag: 'Text', text: '3', fill: 'white', textAlign: 'center', verticalAlign: 'middle', width: 25, height: 30 }]
        })
    ],
})

leafer.add(flow)

自动宽度和高度 ​

ts
// #自动布局 - 自动宽度 [自动宽度和高度]
import { Leafer, Box } from 'leafer-ui'
import { Flow } from '@leafer-in/flow'  // 导入自动布局插件

const leafer = new Leafer({ view: window })

const flow = new Flow({
    fill: '#676',
    width: 100,
    height: 100,
    children: [
        new Box({ fill: '#FF4B4B', children: [{ tag: 'Text', text: '1', fill: 'white', textAlign: 'center', verticalAlign: 'middle', width: 25, height: 20 }] }),
        new Box({ fill: '#FEB027', children: [{ tag: 'Text', text: '2', fill: 'white', textAlign: 'center', verticalAlign: 'middle', width: 25, height: 40 }] }),
        new Box({
            autoWidth: 1, // 自动宽度和高度
            autoHeight: 1,
            fill: '#79CB4D', children: [{ tag: 'Text', text: '3', fill: 'white', textAlign: 'center', verticalAlign: 'middle', width: 25, height: 30 }]
        })
    ],
})

leafer.add(flow)

自动宽度为百分比单位 ​

ts
// #自动布局 - 自动宽高 [使用百分比]
import { Leafer, Box } from 'leafer-ui'
import { Flow } from '@leafer-in/flow'  // 导入自动布局插件

const leafer = new Leafer({ view: window })

const flow = new Flow({
    fill: '#676',
    width: 100,
    height: 100,
    children: [
        new Box({
            autoWidth: { type: 'percent', value: 0.8 }, // 80% 父元素宽度
            autoHeight: { type: 'percent', value: 0.8 }, // 80% 父元素高度
            fill: '#79CB4D', children: [{ tag: 'Text', text: '3', fill: 'white', textAlign: 'center', verticalAlign: 'middle', width: 25, height: 30 }]
        })
    ],
})

leafer.add(flow)

Released under the MIT License.