3 Star 44 Fork 10

Ryan Zhu / OhosExtension

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
Toolbar.md 2.49 KB
Copy Edit Raw Blame History
朱帆 authored 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

Search

53164aa7 5694891 3bd8fe86 5694891