Use of Toobar in Android5.0
In general, the use of Toolbar can be divided into two aspects: one is to use ToolBar as ActionBar, and the other is to use Toolbar as a separate control, however, most of the situations I have seen so far are that Toolbar is used as an ActionBar. The following two usage methods are described respectively.
1. Use Toolbar as ActionBar
To use Toolbar as an ActionBar, you must first hide the default ActionBar of the system. The hiding method is actually very simple. Just set the style as follows:
<style name="AppTheme.NoActionBar"> <item name="android:windowActionBar">false</item> <item name="android:windowNoTitle">true</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item></style>
Before Android phone 3.0, the title bar was called titlebar. After Android phone 3.0, ActionBar was introduced to replace titlebar. Therefore, titlebar should be hidden for compatibility with early mobile phones. After these two things are hidden, you can set a topic for the Activity or App in the list file. Here I will set a topic for the Activity, as shown below:
<activity android:name=".MainActivity" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN"> <category android:name="android.intent.category.LAUNCHER"> </category></action></intent-filter> </activity>
In this way, my MainActivity successfully hides the ActionBar, and the next step is how to add the Toolbar. We know that there are not many actionbars used in enterprise development, one important reason is that this is too rigid and ugly, so Google will inevitably avoid these problems when launching Toolbar. so, besides being nice-looking, Toolbar has another biggest advantage: flexibility, you can use it as an ordinary control. How can you use a common control? Write it in the layout file. At the same time, we use the Toolbar in the v7 package when using the Toolbar to be compatible with mobile phones Before Android. The layout file is as follows:
<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--><linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="org.mobiletrain.toolbar2.MainActivity"> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorPrimary" android:popuptheme="@style/AppTheme.PopupOverlay"> </android.support.v7.widget.toolbar></linearlayout>
OK, isn't it easy? A simple layout file is ready, so there are three points to explain. First, the toolbar is high. You can give a fixed value, you can also refer to the height of the previous ActionBar. The advantage of the ActionBar height is that the system will automatically adjust the height of the Toolbar for you on different devices. The second point is the background attribute. When using toolbar, we can set a background color for the toolbar. The third is the popupTheme attribute, which is the display in the pop-up box. Here we generally set it as follows: