3 Star 44 Fork 10

Ryan Zhu / OhosExtension

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Toolbar.md 2.49 KB
一键复制 编辑 原始数据 按行查看 历史
朱帆 提交于 2021-07-26 15:31 . update readme and utils

Toolbar

具体用法与Android中大致一致。

使用

参考其他Toolbar教程。

新增属性

  • statusBarHeight 默认通过WindowUtil获取状态栏高度,如果要去掉状态栏将此属性设置为0vp
  • statusBarBackground 默认透明
  • hideNavigationIcon 隐藏导航图标,默认为true
<com.ryan.ohos.extension.widget.toolbar.Toolbar
    ...
    ohos:hideNavigationIcon="false"
    ohos:statusBarHeight="30vp"
    ohos:statusBarBackground="$color:primaryColor" />

statusBar相关属性需要配合透明状态栏一起使用:

  • config.json中,为ability增加metaData:
...
"abilities": [
    {
        "metaData": {
            "customizeData": [
                {
                    "name": "hwc-theme",
                    "value": "androidhwext:style/Theme.Emui.NoTitleBar",
                    "extra": ""
                }
            ]
        },
        ...
    },
    ...
]
  • 在ability的onStart中增加如下代码:
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.TRANSPARENT.getValue());
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);

        setUIContent(ResourceTable.Layout_ability_main);
    }

原有属性

  • gravity 设置Toolbar子元素(除开按钮)垂直对齐方式,如center、top
  • buttonGravity 设置Toolbar按钮的垂直对齐方式,如center、top
  • titleMargin
  • titleMarginStart
  • titleMarginEnd
  • titleMarginTop
  • titleMarginBottom
  • navigationIconWidth
  • maxButtonHeight
  • contentInsetStartWithNavigation
  • contentInsetEndWithActions
  • collapseIcon
  • title
  • subtitle
  • navigationIcon
  • logo
  • titleTextColor
  • subtitleTextColor
  • titleTextSize
  • subtitleTextSize

默认值

Toolbar的默认值可以查看src\main\resources\base\element\toolbar.json

标题居中

title

  1. 不设置Toolbar的title
  2. 包裹一个Text,并将layout_gravity属性设置为center
<com.ryan.ohos.extension.widget.toolbar.Toolbar
    ohos:id="$+id:toolbar"
    ohos:height="$float:toolbarHeight"
    ohos:width="match_parent">
    
    <Text
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:text_size="15vp"
        ohos:text_color="#fff"
        ohos:text="title"
        ohos:layout_gravity="center"/>

</com.ryan.ohos.extension.widget.toolbar.Toolbar>

未实现功能

  • Menu相关
Java
1
https://gitee.com/thoseyears/ohos-extension.git
git@gitee.com:thoseyears/ohos-extension.git
thoseyears
ohos-extension
OhosExtension
master

搜索帮助