onConfigurationUpdate(newConfig: Configuration): void {
console.info(`[ConfigurationChange], config: ${JSON.stringify(newConfig)}`);
AppStorage.setOrCreate('FontSizeScale', newConfig.fontSizeScale); // 字体缩放比例
AppStorage.setOrCreate('ScreenDensity', newConfig.screenDensity); // 屏幕像素密度
// 如果应用采用响应式布局,可以根据字体缩放比例和屏幕像素密度更新断点
// 或者根据fontSizeScale、screenDensity的值直接调整页面布局(限制组件宽高、换行显示、超出省略等方案)
} @Component
export struct MaxScale {
build() {
Column() {
// 只会限制字体大小,实际显示效果还是会受到显示缩放大小的影响
Text('限制字体最大缩放倍数')
.maxFontScale(1.15)
}
}
} {
"configuration": {
"fontSizeScale": "followSystem",
"fontSizeMaxScale": "1.45"
}
} import { display, uiObserver, window } from '@kit.ArkUI';
import { BusinessError } from '@ohos.base';
@Component
export struct DensityUpdateTest {
parentPadding: number = 12; // 左右边距
// 限制组件最大宽度
@State maxWidth: number =
(this.getUIContext().px2vp(display.getDefaultDisplaySync().width) / 2 - this.parentPadding) / 2;
aboutToAppear(): void {
this.getUIContext()
.getUIObserver()
.on('densityUpdate', (info: uiObserver.DensityInfo) => {
// 如果采用了响应式布局,可以直接更新断点,根据断点调整布局
console.info('[densityChange]', `current width breakpoint:`, this.getUIContext().getWindowWidthBreakpoint());
// 也可以根据density数据计算组件大小,调整布局
// vp和pv转换公式:`vp = px / (DPI/160)` 和`info.density`相关
// 建议直接通过UIContext的px2vp或vp2px方法转换
console.info('[densityChange]', `current density: ${info.density}`);
// 此处仅为示例,可以根据实际需要选择合适布局调整方案
// 获取窗口宽度,并调整组件的最大宽度
window.getLastWindow(this.getUIContext().getHostContext(), (err: BusinessError, windowClass: window.Window) => {
let windowWidth = this.getUIContext().px2vp(windowClass.getGlobalRect().width);
// 设置组件最大宽度,为(窗口左半-边距)/2,左侧组件占一半,右侧组件的一半宽度也不能超过该值
this.maxWidth = (windowWidth / 2 - this.parentPadding) / 2;
});
});
}
build() {
Column() {
Column() {
Stack() {
Row() {
Text('布局测试-偏左')
.backgroundColor('#ff92f1b0')
.constraintSize({ maxWidth: this.maxWidth }) // 限制最大宽度
}
.width('100%')
.onSizeChange((oldSize, newSize) => {
// 也可以在onSizeChange事件中调整布局
console.info('[CompSizeChange]', oldSize.width, newSize.width);
})
Row() {
Text('布局测试-居中')
.backgroundColor('#ffffcfcf')
.constraintSize({ maxWidth: this.maxWidth * 2 }) // 限制最大宽度
}
}
.padding({
left: this.parentPadding,
right: this.parentPadding
})
}
.width('100%')
}
.width('100%')
.padding({
top: 50,
bottom: 30
})
}
} 完整示例参考如下:
import { AbilityConstant, Configuration, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
const DOMAIN = 0x0000;
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
try {
this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET);
} catch (err) {
hilog.error(DOMAIN, 'testTag', 'Failed to set colorMode. Cause: %{public}s', JSON.stringify(err));
}
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate');
hilog.info(DOMAIN, 'testTag', '%{public}s', JSON.stringify(launchParam));
}
onDestroy(): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
}
onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/LayoutTest', (err) => {
if (err.code) {
hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
return;
}
hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
});
}
onWindowStageDestroy(): void {
// Main window is destroyed, release UI related resources
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
}
onForeground(): void {
// Ability has brought to foreground
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
}
onBackground(): void {
// Ability has back to background
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
}
onConfigurationUpdate(newConfig: Configuration): void {
console.info(`[ConfigurationChange], config: ${JSON.stringify(newConfig)}`);
AppStorage.setOrCreate('FontSizeScale', newConfig.fontSizeScale); // 字体缩放比例
AppStorage.setOrCreate('ScreenDensity', newConfig.screenDensity); // 屏幕像素密度
// 如果应用采用响应式布局,可以根据字体缩放比例和屏幕像素密度更新断点
// 或者根据fontSizeScale、screenDensity的值直接调整页面布局(限制组件宽高、换行显示、超出省略等方案)
}
} import { DensityUpdateTest } from '../component/DensityUpdateTest';
import { MaxScale } from '../component/MaxScaleTest';
@Entry
@Component
struct LayoutTest {
build() {
Column() {
DensityUpdateTest()
MaxScale()
}
}
} import { display, uiObserver, window } from '@kit.ArkUI';
import { BusinessError } from '@ohos.base';
@Component
export struct DensityUpdateTest {
parentPadding: number = 12; // 左右边距
// 限制组件最大宽度
@State maxWidth: number =
(this.getUIContext().px2vp(display.getDefaultDisplaySync().width) / 2 - this.parentPadding) / 2;
aboutToAppear(): void {
this.getUIContext()
.getUIObserver()
.on('densityUpdate', (info: uiObserver.DensityInfo) => {
// 如果采用了响应式布局,可以直接更新断点,根据断点调整布局
console.info('[densityChange]', `current width breakpoint:`, this.getUIContext().getWindowWidthBreakpoint());
// 也可以根据density数据计算组件大小,调整布局
// vp和pv转换公式:`vp = px / (DPI/160)` 和`info.density`相关
// 建议直接通过UIContext的px2vp或vp2px方法转换
console.info('[densityChange]', `current density: ${info.density}`);
// 此处仅为示例,可以根据实际需要选择合适布局调整方案
// 获取窗口宽度,并调整组件的最大宽度
window.getLastWindow(this.getUIContext().getHostContext(), (err: BusinessError, windowClass: window.Window) => {
let windowWidth = this.getUIContext().px2vp(windowClass.getGlobalRect().width);
// 设置组件最大宽度,为(窗口左半-边距)/2,左侧组件占一半,右侧组件的一半宽度也不能超过该值
this.maxWidth = (windowWidth / 2 - this.parentPadding) / 2;
});
});
}
build() {
Column() {
Column() {
Stack() {
Row() {
Text('布局测试-偏左')
.backgroundColor('#ff92f1b0')
.constraintSize({ maxWidth: this.maxWidth }) // 限制最大宽度
}
.width('100%')
.onSizeChange((oldSize, newSize) => {
// 也可以在onSizeChange事件中调整布局
console.info('[CompSizeChange]', oldSize.width, newSize.width);
})
Row() {
Text('布局测试-居中')
.backgroundColor('#ffffcfcf')
.constraintSize({ maxWidth: this.maxWidth * 2 }) // 限制最大宽度
}
}
.padding({
left: this.parentPadding,
right: this.parentPadding
})
}
.width('100%')
}
.width('100%')
.padding({
top: 50,
bottom: 30
})
}
} @Component
export struct MaxScale {
build() {
Column() {
// 只会限制字体大小,实际显示效果还是会受到显示缩放大小的影响
Text('限制字体最大缩放倍数')
.maxFontScale(1.15)
}
}
}