使用include引用布局信息
<include
ohos:height="0"
ohos:weight="1"
ohos:layout="$layout:ability_navigation_bar"
ohos:width="match_parent"
ohos:layout_alignment="bottom"/>
需要被引用的布局文件ability_navigation_bar
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="60vp"
ohos:width="match_parent"
ohos:orientation="horizontal">
<DirectionalLayout
ohos:id="$+id:xinwen"
ohos:height="match_content"
ohos:width="90vp"
ohos:clickable="true"
ohos:alignment="horizontal_center"
ohos:orientation="vertical">
<Image
ohos:id="$+id:img_xinwen"
ohos:height="45vp"
ohos:scale_mode="zoom_center"
ohos:width="match_content"
ohos:image_src="$media:xinwen"/>
<Text
ohos:height="match_content"
ohos:width="match_content"
ohos:text="新闻"
ohos:text_size="15fp"/>
</DirectionalLayout>
<DirectionalLayout
ohos:id="$+id:liaotainshi"
ohos:height="match_content"
ohos:width="90vp"
ohos:clickable="true"
ohos:alignment="horizontal_center"
ohos:orientation="vertical">
<Image
ohos:id="$+id:img_liaotianshi"
ohos:height="45vp"
ohos:scale_mode="zoom_center"
ohos:width="match_content"
ohos:image_src="$media:xinwen"/>
<Text
ohos:height="match_content"
ohos:width="match_content"
ohos:text="聊天室"
ohos:text_size="15fp"/>
</DirectionalLayout>
<DirectionalLayout
ohos:id="$+id:luntan"
ohos:height="match_content"
ohos:width="90vp"
ohos:clickable="true"
ohos:alignment="horizontal_center"
ohos:orientation="vertical">
<Image
ohos:id="$+id:img_luntan"
ohos:height="45vp"
ohos:scale_mode="zoom_center"
ohos:width="match_content"
ohos:image_src="$media:xinwen"/>
<Text
ohos:height="match_content"
ohos:width="match_content"
ohos:text="论坛"
ohos:text_size="15fp"/>
</DirectionalLayout>
<DirectionalLayout
ohos:id="$+id:gerenzhongxin"
ohos:height="match_content"
ohos:width="90vp"
ohos:clickable="true"
ohos:alignment="horizontal_center"
ohos:orientation="vertical">
<Image
ohos:id="$+id:img_geren"
ohos:height="45vp"
ohos:scale_mode="zoom_center"
ohos:width="match_content"
ohos:image_src="$media:xinwen"/>
<Text
ohos:height="match_content"
ohos:width="match_content"
ohos:text="我"
ohos:text_size="15fp"/>
</DirectionalLayout>
</DirectionalLayout>
引用文件的后台方法
package com.example.xinyun.slice;
import com.example.xinyun.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.window.dialog.ToastDialog;
public class NavigationBarSlice extends AbilitySlice{
boolean state_xinwen;
boolean state_liaoTianShi;
boolean state_lunTan;
boolean state_geRenZhongXin;
Image imgXinWen;
Image imgLiaoTianShi;
Image imgLunTan;
Image imgGeRen;
DirectionalLayout xinWen;
DirectionalLayout liaoTianShi;
DirectionalLayout lunTan;
DirectionalLayout geRenZhongXin;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_navigation_bar);
state_xinwen = true;
state_liaoTianShi=false;
state_lunTan=false;
state_geRenZhongXin=false;
xinWen = (DirectionalLayout)findComponentById(ResourceTable.Id_xinwen);
liaoTianShi = (DirectionalLayout)findComponentById(ResourceTable.Id_liaotainshi);
lunTan = (DirectionalLayout)findComponentById(ResourceTable.Id_luntan);
geRenZhongXin = (DirectionalLayout)findComponentById(ResourceTable.Id_gerenzhongxin);
imgXinWen = (Image) findComponentById(ResourceTable.Id_img_xinwen);
imgLiaoTianShi = (Image) findComponentById(ResourceTable.Id_img_liaotianshi);
imgLunTan = (Image) findComponentById(ResourceTable.Id_img_luntan);
imgGeRen = (Image) findComponentById(ResourceTable.Id_img_geren);
xinWen.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
if(!state_xinwen)
{
//创建一个意图
Intent intent=new Intent();
//将跳转信息进行打包
Operation operation = new Intent.OperationBuilder().
withDeviceId("").//跳转的设备
withBundleName("com.example.xinyun").//跳转的包名
withAbilityName("com.example.xinyun.MainAbility").//跳转的页面
build();
//进行页面跳转
intent.setOperation(operation);
startAbility(intent);
state_xinwen = true;
state_liaoTianShi=false;
state_lunTan=false;
state_geRenZhongXin=false;
new ToastDialog(NavigationBarSlice.this)
.setText("新闻被点击")
.show();
}
}
});
liaoTianShi.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
if(!state_liaoTianShi)
{
//创建一个意图
Intent intent=new Intent();
//将跳转信息进行打包
Operation operation = new Intent.OperationBuilder().
withDeviceId("").//跳转的设备
withBundleName("com.example.xinyun").//跳转的包名
withAbilityName("com.example.xinyun.ChatRoom").//跳转的页面
build();
//进行页面跳转
intent.setOperation(operation);
startAbility(intent);
state_xinwen = false;
state_liaoTianShi=true;
state_lunTan=false;
state_geRenZhongXin=false;
new ToastDialog(NavigationBarSlice.this)
.setText("聊天室被点击")
.show();
}
}
});
lunTan.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
if(!state_lunTan)
{
//创建一个意图
Intent intent=new Intent();
//将跳转信息进行打包
Operation operation = new Intent.OperationBuilder().
withDeviceId("").//跳转的设备
withBundleName("com.example.xinyun").//跳转的包名
withAbilityName("com.example.xinyun.Forum").//跳转的页面
build();
//进行页面跳转
intent.setOperation(operation);
startAbility(intent);
state_xinwen = false;
state_liaoTianShi=false;
state_lunTan=true;
state_geRenZhongXin=false;
new ToastDialog(NavigationBarSlice.this)
.setText("论坛被点击")
.show();
}
}
});
geRenZhongXin.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
if(!state_geRenZhongXin)
{
//创建一个意图
Intent intent=new Intent();
//将跳转信息进行打包
Operation operation = new Intent.OperationBuilder().
withDeviceId("").//跳转的设备
withBundleName("com.example.xinyun").//跳转的包名
withAbilityName("com.example.xinyun.state").//跳转的页面
build();
//进行页面跳转
intent.setOperation(operation);
startAbility(intent);
state_xinwen = false;
state_liaoTianShi=false;
state_lunTan=false;
state_geRenZhongXin=true;
new ToastDialog(NavigationBarSlice.this)
.setText("个人中心被点击")
.show();
}
}
});
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}