Beautify your app--start from toolbar

Source: Internet
Author: User

What is toolbar?

Toolbar is Google's alternative to Actionbar's view, launched in Android 5.0. Actionbar must be part of the activity content, and toolbar can be placed at any level. Toolbar supports more features than Actionbar, from start to finish, toolbar contains the following optional elements:
-A navigation button. Can be a forward button, a navigation menu button, and so on.
-A logo image
-Title and subtitle
-One or more custom one view
-a menu

Let's take a picture to introduce the layout of toolbar. As follows:

How to use toolbar for simple use

Now, I'm using Android studio to create a module, which, by default, will generate an activity with toolbar for me with the following interface:

The top of which is toolbar.
For use, there are two points to note:
1. Activity needs to inherit from appcompatactivity,activity use no Actionbar theme

publicclass MainActivity extends AppCompatActivity {    @Override    protectedvoidonCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main2);        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);        setSupportActionBar(toolbar);    }}

Here are the theme theme settings:

android:theme="@style/AppTheme.NoActionBar"
    1. Using the toolbar control in a layout file
 <android.support.v7.widget.Toolbar            android:id="@+id/toolbar"            android:layout_width="match_parent"            android:layout_height="?attr/actionBarSize"            android:background="?attr/colorPrimary"            app:popupTheme="@style/AppTheme.PopupOverlay" />
A little bit more complicated to use

The following uses will display the individual elements of the toolbar.

From left to right, navigation icon, App icon, title and subtitle, and menus are in turn.
Navigation icons, app icons, and captions can be defined in XML as well as in code. To let toolbar support menu, you can rewrite the activity of the Oncreatecontextmenu load menu, and then give toolbar set a Menulistener, as follows:

Ublic class mainactivity extends appcompatactivity  implements  Toolbar. Onmenuitemclicklistener {@Overrideprotected voidOnCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (r.layout.activity_main2);        Toolbar Toolbar = (Toolbar) Findviewbyid (R.id.toolbar);        Setsupportactionbar (toolbar); Getsupportactionbar (). setdisplayhomeasupenabled (true); Toolbar.setonmenuitemclicklistener ( This); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main_menu, menu);return true; } @Override Public BooleanOnmenuitemclick (MenuItem Item) {Switch(Item.getitemid ()) { CaseR.id.action_search:toast.maketext ( This,"Search", Toast.length_short). Show (); Break; CaseR.id.action_add:toast.maketext ( This,"Add", Toast.length_short). Show (); Break; CaseR.id.action_setting:toast.maketext ( This,"Settings", Toast.length_short). Show (); Break; }return false; }}

If you want to give the navigation buttons, add the return function, you need to rewrite the onoptionsitemselected method, as follows:

@Override    publicbooleanonOptionsItemSelected(MenuItem item) {        if (item.getItemId() == android.R.id.home) {            finish();            returntrue;        }        returnsuper.onOptionsItemSelected(item);    }
Toolbar The title to center the display

Many applications, instead of using Actionbar, use a custom Topbar. Using toolbar found that some layouts may not be implemented, or can not be implemented, such as the title of toolbar in the middle. Toolbar provides the Titlemargin property by setting the Marigin to the center of the title text, but the next option is to add a textview in the middle of the toolbar. The effect is as follows:

where implementation is implemented by a toolbarhelper, it is implemented as follows:

public   Class  Toolbarhelper {public  static  Span class= "Hljs-keyword" >void  addmiddletitle  (context context, charsequence        Title, Toolbar Toolbar) {TextView TextView = new  TextView (context);        Textview.settext (title); Toolbar.layoutparams params  = new         Toolbar.layoutparams (Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT);        params . Gravity = Gravity.center_horizontal;    Toolbar.addview (TextView, params ); }}

This allows for an effect such as QQ

In this "dynamic" word we can use the toolbar Title property, and the middle of the friend dynamic can be added TextView way to achieve.
For other functions, you can also add view by toolbar.
SOURCE Please see GitHub

Beautify your app--start from toolbar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.