Should we replace Action Bar by ToolBar?

后端 未结 10 1467
日久生厌
日久生厌 2020-12-12 19:11

I have been using ToolBar since it was added into Support v7 library. And I think I used it well. But there is a point I can\'t understand. Why

相关标签:
10条回答
  • 2020-12-12 19:33

    A Toolbar is a generalization of action bars for use within application layouts. While an action bar is traditionally part of an Activity's opaque window decor controlled by the framework, a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy. An application may choose to designate a Toolbar as the action bar for an Activity using the setActionBar() method. You can find more info here. We replaced our actionbar, as it was easier to customize toolbar for material design. Color palettes and disappearing animation behavior for example. Personally, I don't understand why android throws away old controls and create new one. Another example would be RecyclerView. Don't understand why they just didn't improve old API.

    0 讨论(0)
  • 2020-12-12 19:33

    Note: Both will support general app navigation, icons and have backward support.

    The answer depends on what user interaction(animating toolbar) your designs calls for. That being said you should implement animations on the toolbar to make it Material.


    ActionBar:

    If you simply want a static bar at the top that can host icons, back button and you can theme.

    Toolbar:

    If you want to do anything beyond a static bar such as animations.

    A common implementation and Google design recommendation is to hide the toolbar when scrolling. Material Design checklist: hiding app bar on scroll?

    0 讨论(0)
  • 2020-12-12 19:34

    Yes, you should replace ActionBar with new toolbar.

    Reasons

    1. It looks modern and it follows new material design.

    2. Unlike Action bar, toolbar is not part of window decor. You define it and place it just like any other widget... therefore, you have freedom to place it anywhere in the parent layout.

    3. You have freedom to put any widget inside toolbar.

    4. You can define multiple toolbars.

    EDIT

    What i meant is you can place other widgets (views) inside toolbar.

    Create a separate layout file for the toolbar (good for reusability). In my case file name is main_toolbar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:App="http://schemas.android.com/apk/res-auto"
        xmlns:segmentedgroup="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        App:theme="@style/ToolbarColoredBackArrow"
        android:layout_height="56dp"
        android:background="@color/primary_color" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/drawer_fntsize"
            android:text="Title"
            android:id="@+id/lbl_title"
            android:textColor="@color/title_text_color"
            android:layout_gravity="center" />
    
     </android.support.v7.widget.Toolbar>
    

    Then include this toolbar in your main layout like this

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <include
            android:id="@+id/toolbar"
            layout="@layout/main_toolbar" />
    
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar" />
    
    </RelativeLayout>
    

    As you can see in this example i placed TextView inside the toolbar

    0 讨论(0)
  • 2020-12-12 19:35

    Here is the documentation link -- http://developer.android.com/training/appbar/setting-up.html

    Note that it doesn't include the code where you include it in your other layout files:

    <include
        android:id="@+id/toolbar"
        layout="@layout/tool_bar"/>
    

    Why I changed to Toolbar -- One of the reasons I changed a recent app to Toolbar was that when we wanted to customise the action bar, it wouldn't work on different Android versions or when we extended certain classes (making it transparent or showing an icon instead of the app name). Toolbars have allowed us the customisation options we want.

    0 讨论(0)
  • 2020-12-12 19:36

    why Android would create such a widget?

    Imagine, if you will, an Android tablet.

    This tablet is running an app. That app has, in the lower-right corner of the screen, a rich text editor, where one can enter in some comments and format them with bold, italic, etc.

    In a desktop or Web app, a typical approach for those formatting options, besides keyboard shortcuts, would be a toolbar, like the one you see above the answer text area here on Stack Overflow.

    Prior to Toolbar, Android developers had to either roll their own toolbar, or put the formatting actions in the action bar. While the latter approach is easy, it puts a strain on the user of the aforementioned fictional app, as the user has to keep switching her visual focus from the editor (bottom of screen) to the action bar (top of screen).

    Why have we to use ToolBar?

    You do not have to use Toolbar. I have ~300 sample apps as part of my book, and at the moment, precisely zero of them use Toolbar. I'll have to correct that at some point, as I have not yet written a chapter on Toolbar.

    Is it necessary to replace ActionBar by ToolBar?

    No. There is a way to do this, but it is not necessary.

    0 讨论(0)
  • 2020-12-12 19:36
    1. You can easily customize toolbar.
    2. You can add many widgets inside toolbar.
    3. You can add many toolbar in your view.
    4. You have freedom to place it anywhere in the parent layout.
    5. They have own handling/managing of it child view.
    0 讨论(0)
提交回复
热议问题