导航栏避让常规方法:
let windowClass: window.Window | undefined = undefined;
window.getLastWindow(this.context, (err: BusinessError, data) => {
windowClass = data;
}) windowClass.on('avoidAreaChange', (data: window.AvoidAreaOptions) => {
AppStorage.setOrCreate('avoidArea', data.area);
}); @StorageLink('avoidArea') avoidArea: window.AvoidArea = {
// 具体参数
};
Text('title')
.padding({ bottom: this.avoidArea.bottomRect.height }) 切换全屏前:
"bottomRect":{"left":0,"top":0,"width":0,"height":0} 由日志可知,切换全屏前避让区域的高度为0。
切换全屏后:
"bottomRect":{"left":0,"top":0,"width":0,"height":0} 切换全屏后避让区域的高度依然为0。
import { ConfigurationConstant, UIAbility } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
const DOMAIN = 0x0000;
export default class EntryAbility extends UIAbility {
onCreate(): 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');
}
onDestroy(): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
}
onWindowStageCreate(windowStage: window.WindowStage): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
return;
}
let windowClass: window.Window | undefined = undefined;
window.getLastWindow(this.context, (err: BusinessError, data: window.Window) => {
const errCode: number = err.code;
if (errCode) {
console.error(`Failed to create the system window. Cause code: ${err.code}, message: ${err.message}`);
return;
}
windowClass = data;
windowClass.setWindowLayoutFullScreen(true).then(() => {
console.info('Succeeded in setting the window layout to full-screen mode.');
}).catch((err: BusinessError) => {
console.error(`Failed to set the window layout to full-screen mode. Cause code: ${err.code}, message: ${err.message}`);
});
try {
// 首次启动应用时避让导航栏
let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR;
let avoidArea = windowClass.getWindowAvoidArea(type);
AppStorage.setOrCreate('avoidArea', avoidArea);
// 通过监听系统规避区尺寸动态避让导航栏
windowClass.on('avoidAreaChange', (data: window.AvoidAreaOptions) => {
AppStorage.setOrCreate('avoidArea', data.area);
});
} catch (exception) {
console.error(`Failed to enable the listener for window size changes. Cause code: ${exception.code}, message: ${exception.message}`);
}
});
hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
});
}
onWindowStageDestroy(): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
}
onForeground(): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
}
onBackground(): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
}
} import { window } from '@kit.ArkUI';
@Entry
@Component
struct Index {
// 获取导航栏尺寸
@StorageLink('avoidArea') avoidArea: window.AvoidArea = {
visible: true,
topRect: {
top: 0,
left: 0,
width: 0,
height: 0
},
bottomRect: {
top: 0,
left: 0,
width: 0,
height: 0
},
leftRect: {
top: 0,
left: 0,
width: 0,
height: 0
},
rightRect: {
top: 0,
left: 0,
width: 0,
height: 0
}
};
@Builder
tabBuilder(content: string, img: Resource) {
Column({ space: 5 }) {
Image(img)
.height(20)
Text(content)
.fontSize(15)
}
}
build() {
Tabs({ barPosition: BarPosition.End }) {
TabContent() {
Text('页签1')
.fontSize(50)
}
.tabBar(this.tabBuilder('页签1', $r('app.media.startIcon')))
.backgroundColor(Color.Red)
TabContent() {
Text('页签2')
.fontSize(50)
}
.tabBar(this.tabBuilder('页签2', $r('app.media.startIcon')))
.backgroundColor(Color.Yellow)
TabContent() {
Text('页签3')
.fontSize(50)
}
.tabBar(this.tabBuilder('页签3', $r('app.media.startIcon')))
.backgroundColor(Color.Blue)
TabContent() {
Text('页签4')
.fontSize(50)
}
.tabBar(this.tabBuilder('页签4', $r('app.media.startIcon')))
.backgroundColor(Color.Green)
}
.padding({ bottom: this.avoidArea.bottomRect.height + 'px' }) // 在padding中设置,避让导航栏
}
}