All Products
Search
Document Center

Custom title bar(10.1.68)

Last Updated: Feb 19, 2021

The Nebula container allows you to customize a navigation bar. You can define the style of the navigation bar, such as the position of the title and the style of the Back button. This topic describes how to customize a navigation bar in baseline 10.1.68.

Prerequisites

Before you read this guide, take note of the following key points:

  • To develop a navigation bar to be shared by Mini Programs and HTML5 pages, you must take into account the use of navigation bars on HTML5 pages and that in Mini Programs. You can ignore this rule if you want to use the navigation bar only in Mini Programs or only on HTML5 pages.
  • The custom navigation bar must comply with the standard process of calling API operations in the container. Please carefully read this document and develop the custom navigation bar as required.
  • By default, Mini Programs use the built-in navigation bar. To use a custom navigation bar in Mini Programs, see Configure the HTML5 container.
  • The color of the navigation bar can be dynamically set. To achieve the best experience, you must prepare two sets of theme configurations and switch between them based on scenarios.

Procedure

  1. Inherit the abstract class AbsTitleView and implement a custom navigation bar.

  2. Implement H5ViewProvider. In the createTitleView method, create and return an instance of the custom navigation bar.

    1. public class H5ViewProviderImpl implements H5ViewProvider {
    2. @Override
    3. public H5TitleView createTitleView(Context context) {
    4. return new NewH5TitleViewImpl(context);
    5. }
    6. @Override
    7. public H5NavMenuView createNavMenu() {
    8. return null;
    9. }
    10. @Override
    11. public H5PullHeaderView createPullHeaderView(Context context, ViewGroup viewGroup) {
    12. return null;
    13. }
    14. @Override
    15. public H5WebContentView createWebContentView(Context context) {
    16. return null;
    17. }
    18. }
  3. Set H5ViewProvider in the container in specific scenarios, such as application startup.

    1. MPNebula.setCustomViewProvider(new H5ViewProviderImpl());
  4. If your project is based on the Portal and Bundle architecture, additional configurations are required.

    1. H5Utils.setProvider(H5ReplaceResourceProvider.class.getName(), new H5ReplaceResourceProvider() {
    2. @Override
    3. public String getReplaceResourcesBundleName() {
    4. return BuildConfig.BUNDLE_NAME;
    5. }
    6. });

More information

Background color

  1. /**
  2. * Return the value of the background color of the navigation bar.
  3. * @return
  4. */
  5. public abstract int getBackgroundColor();
  6. /**
  7. * Set the transparency of the navigation bar.
  8. * @param alpha
  9. */
  10. public abstract void setBackgroundAlphaValue(int alpha);
  11. /**
  12. * Sets the background color of the navigation bar without using Alpha values.
  13. * @param color
  14. */
  15. public abstract void setBackgroundColor(int color);
  16. /**
  17. * Resets the navigation bar.
  18. */
  19. public abstract void resetTitle();

The resetTitle operation is triggered when the JSAPI named setTitleColor is called on a foreground page. In this case, we recommend that you reset the display elements to default elements. These display elements include the background of the navigation bar and the color values of other components mentioned later in this topic.

Title

Subtitles are supported in HTML5 page-only scenarios. If the application does not need a subtitle, you may ignore implementation of the subtitle.

To enable an HTML5 page to listen to events of tapping the title bar, you must enable the navigation bar to call the invokeTitleClickEvent method in specified scenarios. To enable an HTML5 page to listen to events of tapping the subtitle bar, you must enable the navigation bar to call the invokeSubTitleClickEvent method.

  1. /**
  2. * Return the text of the main title.
  3. * @return
  4. */
  5. public abstract String getTitle();
  6. /**
  7. * Set the text of the main title.
  8. * @param title
  9. */
  10. public abstract void setTitle(String title);
  11. /**
  12. * Set the text of the subtitle.
  13. * @param subTitle
  14. */
  15. public abstract void setSubTitle(String subTitle);
  16. /**
  17. * Return the view of the main title.
  18. * @return
  19. */
  20. public abstract TextView getMainTitleView();
  21. /**
  22. * Return the view of the subtitle.
  23. * @return
  24. */
  25. public abstract TextView getSubTitleView();

Left-side control section

  1. /**
  2. * Set whether to show the Close button.
  3. * @param visible
  4. */
  5. public abstract void showCloseButton(boolean visible);
  6. /**
  7. * Set whether to show the Back button.
  8. * @param visible
  9. */
  10. public abstract void showBackButton(boolean visible);
  11. /**
  12. * Set whether to show the Home button.
  13. * @param visible
  14. */
  15. public abstract void showBackHome(boolean visible);
  16. /**
  17. * Set whether to show the loading progress icon in the title bar.
  18. * @param visible
  19. */
  20. public abstract void showTitleLoading(boolean visible);

Close button

As shown in the red box in the preceding figure, the Close button only appears in HTML5 pages. When more than one historical online pages exist in a browser, the container calls the showCloseButton method to control whether to show the Close button. When the Close button is tapped, you must call the invokePageCloseEvent method to comply with the container behavior specification.

Back button

The Back button shown in the red box in the preceding figure is a control that must be implemented in a custom navigation bar. The container calls the showBackButton method to control whether to show the Back button. When the Back button is tapped, you must call theinvokePageBackEvent method to comply with the container behavior specification.

Home button

The Home button is used only in Mini Programs. When you are redirected to a non-homepage of a Mini Program, the container calls the showBackHome method to control whether to show the Home button. When the Home button is tapped, you must call the invokeHomeClickEvent method to comply with the container behavior specification.

Loading progress icon

When an HTML5 page or a Mini Program calls the API for the loading animation in the navigation bar, the container calls the showTitleLoading method to control whether to show the loading progress icon.

Right-side control section

The right-side control section is also referred to as the OptionMenu section. It is mainly used to provide more operations for users.

  • In the HTML5 container:
  • In Mini Programs:

As shown in the preceding figures, you must reserve two view sections for the OptionMenu section. The container manages these two sections based on indexes. The indexes are sorted from right to left and starts with 0.

  1. public abstract void showOptionMenu(boolean visible);
  2. public abstract View getOptionMenuContainer(int index);
  3. public abstract void setOptionMenu(boolean reset, boolean override, boolean isTinyApp, List<MenuData> menus);

The container calls the showOptionMenu method to control whether to show the OptionMenu section. In some cases, the container must obtain the view in this section and perform operations based on the view. You must properly implement the getOptionMenuContainer method.

To implement the setOptionMenu method, see request parameters in Set upper-right buttons. icontype parameter is a built-in button style. It is available only on HTML5 pages. If HTML5 pages are not used in your access scenarios, you can ignore this parameter. Otherwise, you must configure buttons for different styles. Similar to the icontype parameter, the redDot parameter is optional.

To enable an HTML5 page or a Mini Program to listen to the events of tapping upper-right buttons, you must call the invokeOptionClickEvent method.

Take note of the following settings in Mini Programs:

  • To distinguish between HTML5 pages and Mini Programs, you must set the isTinyApp parameter in the setOptionMenu method to true.
  • When you set multiple buttons, you must start from the second button from the right.

Upper-right control section in Mini Programs

In Mini Programs, you must specially implement the right-side section by performing the following steps:

  1. Use the legacy abstract class AbsTinyOptionMenuView to implement a custom control section.
  2. Set TinyOptionMenuViewProvider in the container in specific scenarios, such as application startup.
  1. H5Utils.setProvider(TinyOptionMenuViewProvider.class.getName(), new TinyOptionMenuViewProvider() {
  2. @Override
  3. public AbsTinyOptionMenuView createView(Context context) {
  4. return new TinyOptionMenuView(context);
  5. }
  6. });

You must implement and configure the views of the More and Close buttons based on the container specification.

  1. public abstract void setOptionMenuOnClickListener(View.OnClickListener listener);
  2. public abstract void setCloseButtonOnClickListener(View.OnClickListener listener);
  3. public abstract void setCloseButtonOnLongClickListener(View.OnLongClickListener listener);
  4. public abstract void onStateChanged(TinyAppActionState currentState);
  5. public abstract View getView();

The container calls the first three methods in the preceding code to set a reasonable response callback. You must set the response callback in the specified view.

The onStateChanged method is called in LBS(location-based service) and Bluetooth scenarios. For example, when a Mini Program is using LBS(location-based service), the container calls this method, and you can respond to the callback. The following code shows the style.

The following sample code is for your reference.

  1. public class TinyOptionMenuView extends AbsTinyOptionMenuView {
  2. private View container;
  3. private ImageView ivMore;
  4. private View ivClose;
  5. private Context context;
  6. public TinyOptionMenuView(Context context) {
  7. this.context = context;
  8. ViewGroup parent = null;
  9. if (context instanceof Activity) {
  10. parent = (ViewGroup) ((Activity) context).findViewById(android.R.id.content);
  11. }
  12. container = LayoutInflater.from(context).inflate(R.layout.layout_tiny_right, parent, false);
  13. ivClose = container.findViewById(R.id.close);
  14. ivMore = (ImageView) container.findViewById(R.id.more);
  15. }
  16. @Override
  17. public View getView() {
  18. return container;
  19. }
  20. @Override
  21. public void setOptionMenuOnClickListener(View.OnClickListener onClickListener) {
  22. ivMore.setOnClickListener(onClickListener);
  23. }
  24. @Override
  25. public void setCloseButtonOnClickListener(View.OnClickListener onClickListener) {
  26. ivClose.setOnClickListener(onClickListener);
  27. }
  28. @Override
  29. public void setCloseButtonOnLongClickListener(View.OnLongClickListener onLongClickListener) {
  30. ivClose.setOnLongClickListener(onLongClickListener);
  31. }
  32. @Override
  33. public void onStateChanged(TinyAppActionState state) {
  34. if (state == null) {
  35. ivMore.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_more));
  36. } else if (state.getAction().equals(TinyAppActionState.ACTION_LOCATION)) {
  37. ivMore.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_miniprogram_location));
  38. }
  39. }
  40. }

Theme changes

Different Mini Programs or HTML5 applications may use different background colors in navigation bars. To improve user experience, you also need to adjust other elements in the navigation bar, such as the upper-right control section, in response to changes in the background colors in navigation bars.

In the extension to the navigation bar, the upper-right control section and the navigation bar are different components. Therefore, API operations are provided for you to enable the upper-right control section to respond to changes in the navigation bar.

  • The AbsTinyOptionMenuView class provides the onTitleChanged method to allow you to use override to respond to changes in the navigation bar. When this method is called, the H5TitleView object can be passed in to obtain the information about the navigation bar, such as the background color. In addition, the AbsTinyOptionMenuView class provides the getTitleBar method to allow you to directly obtain objects of the navigation bar. Alternatively, you can convert H5TitleView to a navigation bar object that you can implement, because the AbsTitleView class implements the H5TitleView class. This allows you to obtain more information about the navigation bar.

    1. protected void onTitleChange(H5TitleView title)
  • You must proactively call the notifyTitleBarChanged method provided by the AbsTitleView class, to enable the onTitleChange operation to be called, for example, when the background color of the navigation bar is set. The following sample code is for your reference.

    1. package com.mpaas.demo.nebula;
    2. public class NewH5TitleViewImpl extends AbsTitleView {
    3. @Override
    4. public void setBackgroundAlphaValue(int i) {
    5. content.getContentBgView().setAlpha(i);
    6. notifyTitleBarChanged();
    7. }
    8. @Override
    9. public void setBackgroundColor(int i) {
    10. content.getContentBgView().setColor(i);
    11. notifyTitleBarChanged();
    12. }
    13. @Override
    14. public void resetTitle() {
    15. content.getContentBgView().setColor(context.getResources().getColor(R.color.h5_default_titlebar_color));
    16. notifyTitleBarChanged();
    17. }
    18. }

    In the preceding sample code, when the notifyTitleBarChange method is called, objects of the AbsTinyOptionMenuView subclass may be not 100% initialized. Therefore, we recommend that you override the setH5Page method to obtain the information about the navigation bar and determine the current theme. The following sample code is for your reference.

    1. public class TinyOptionMenuView extends AbsTinyOptionMenuView {
    2. @Override
    3. public void setH5Page(H5Page h5Page) {
    4. super.setH5Page(h5Page);
    5. // title becomes available from here.
    6. if (getTitleBar().getBackgroundColor() == -1) {
    7. bgView.setBackgroundColor(Color.RED);
    8. }
    9. }
    10. }