鸿蒙Java开发模式7:鸿蒙自定义滑动侧边栏的实现及效果 原创
六合李欣
发布于 2021-2-12 22:11
浏览
2收藏
1.鸿蒙自定义滑动侧边栏的实现及效果通过对android侧边栏的重新实现和引入,实现效果如下:
通过滑动侧边栏图标,实现侧边视图的滑动弹出:
2.模块的引入和依赖,工程导航视图如下:
3.设置加载模块并依赖,通过settings.gradle
和工程目录下的build.gradle,添加工程依赖:
4.布局文件
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:width="match_parent"
ohos:height="match_parent">
<Image
ohos:height="300px"
ohos:width="300px"
ohos:image_src="$media:cb2"
ohos:left_margin="-100px"
ohos:top_margin="-50px"
>
</Image>
</DirectionalLayout>
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:width="match_parent"
ohos:height="match_parent"
ohos:orientation="vertical">
</DirectionalLayout>
5.Java代码
package com.example.javahm4.slice;
import com.example.javahm4.MainAbility;
import com.example.javahm4.ResourceTable;
import com.huawei.menu.SlidingMenu;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.bundle.AbilityInfo;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import java.io.IOException;
public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
// super.setUIContent(ResourceTable.Layout_ability_main);
this.setDisplayOrientation(AbilityInfo.DisplayOrientation.PORTRAIT);
//初始化一个directionalLayout(后面attachToAbility会用到,用来获取页面展示的根节点)
DirectionalLayout directionalLayout = (DirectionalLayout) LayoutScatter.getInstance(this).
parse(ResourceTable.Layout_ability_main,null,false);
//把directionalLayout设置为Ability的主要UI布局
setUIContent(directionalLayout);
SlidingMenu slidingMenu = null;
try {
//初始化SlidingMenu实例
slidingMenu = new SlidingMenu(this);
} catch (IOException e) {
e.printStackTrace();
} catch (NotExistException e) {
e.printStackTrace();
}
//设置菜单放置位置
slidingMenu.setMode(SlidingMenu.LEFT);
//设置滑动的触发范围
slidingMenu.setTouchScale(60);
//设置菜单布局
slidingMenu.setMenu(ResourceTable.Layout_layout_left_menu);
//设置菜单最大宽度
slidingMenu.setMenuWidth(800);
try {
//关联Activity
slidingMenu.attachToAbility(directionalLayout,
getAbility(), SlidingMenu.SLIDING_WINDOW);
} catch (NotExistException e) {
e.printStackTrace();
} catch (WrongTypeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
鸿蒙是多设备协同的操作系统,全场景分布式,现在是长征第一步,要抓住机遇,机会是给有准备的人,前进!哈哈!写于牛年大年初一。
©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
标签
赞
收藏 2
回复
相关推荐
相当不错👍👍👍
大年初一做笔记,一个勤奋的少年
棒棒哒....组件源码在哪儿呢?^_^!