Manterial design-Toolbar (2), androidmanterial
Translated from: www.android4devs.com/2014/12/how-to-make-meterial-design-app.html has become a professional interpreter on the learning Road...
Put the final
Images must be big enough to look good.
How to make Meterial Design App Bar/ActionBar and style it
In this article and subsequent articles, we will learn material design and Material design bring many new functions to android, in this article, we will learn how to implement the App Bar using a special attribute, Toobar (ActionBar wants to be called App Bars), and add icons to the App bar.
1. Import racks
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.0.0'}
2. Although Actionbar has been replaced by App bar/Toolbar, we can still use ActionBar. In our example, we will create a ToolBar, so we needStyle. xmlTo change ourTheme,"Theme. AppCompat. Light. NoActionBar" (as long as there is a NoActionBar) is to remove the original ActionBar.
3. Now, let's discuss our projecyt color scheme, as you can see.
These attributes allow you to set the basic color scheme of your app ,(How to compile windowBackground and show that there is a problem. The implementation personnel kindly ask for code segments.)
To achieve this, a file calledColor. xml(I didn't do this, so it's not good)
<?xml version="1.0" encoding="utf-8"?><resources> <color name="colorPrimaryDark">#1976D2</color> <color name="colorPrimary">#2196F3</color> <color name="textColorPrimary">#FFFFFF</color> <color name="navigationBarColor">#2196F3</color></resources>
The following is the Style. xml format:
<?xml version="1.0" encoding="utf-8"?><resources> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorPrimary">@color/colorPrimary</item> <item name="android:textColorPrimary">@color/textColorPrimary</item> <item name="android:navigationBarColor">@color/navigationBarColor</item> </style></resources>
Here I add 2 more properties, may need v21-style/xml for details see previous blog, http://www.cnblogs.com/ryan-ys/p/4745010.html
4. Now we have created a Toolbar. The Toolbar is just another layout, but it can be placed anywhere on the UI. Now, in order to make the ToolBar available to all needs, or in most activit, we place the tool_bar.xml in the Activity, but in a separate file, it is called tool_bar.xml, include her in the activity we need.
Go to the res directory, and create a new Layout Resourse File named tool_bar.xml with the root element android. support. v7.widget. toolbar, as shown in
(Source image translation)
5. Add the background color of the toolbar in tool_bar.xml and setThe shadow effect of elevation.
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:elevation="4dp"> </android.support.v7.widget.Toolbar>
6. Add our Toolbar to main_activity.java,
<RelativeLayout 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=".MainActivity"> <include android:id="@+id/toolbar" layout="@layout/tool_bar"></include> <TextView android:layout_below="@+id/toolbar" android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /></RelativeLayout>
7. MainActivity settings
package com.ryan.toolbardemo01;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.Toolbar;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends AppCompatActivity { private Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); }}
8. the rest is to show how to add menu items, such as serch icon and uesr icon. I chose icons from icons4andrioid.com and recommended one of my posts, 5 tools every android developer must know post. After downloading the image (officially recommended size), add it to the drawbles directory,
9. Now you need to add search icons and user icons.Menu_main.xmlMedium
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <item android:id="@+id/action_settings" android:orderInCategory="100" android:title="@string/action_settings" app:showAsAction="never" /> <item android:title="serch" android:id="@+id/search" android:icon="@drawable/search" android:orderInCategory="200" app:showAsAction="ifRoom"></item> <item android:title="user" android:id="@+id/user" android:icon="@drawable/users" android:orderInCategory="300" app:showAsAction="ifRoom"></item></menu>
Now all the steps are completed.
I hope you will like this translation and the original author www.android4devs.com/2014/12/how-to-make-meterial-design-app.html.
I reconstructed my original code and cut it down. The shadow part above can still be seen.