1.在activity的layout中添加toolbar,使用一个TextView来显示title,方便控制
1 <?xml version="1.0" encoding="utf-8"?> 2 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context="com.mazingtec.mazing.Hall"> 8 9 <android.support.design.widget.AppBarLayout 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content"> 12 13 <android.support.v7.widget.Toolbar 14 android:id="@+id/toolbar" 15 android:layout_width="match_parent" 16 app:navigationIcon="@mipmap/ic_menu_white_48dp" 17 app:titleTextAppearance="@style/TextAppearance.AppCompat.Large" 18 android:layout_height="?attr/actionBarSize" 19 android:background="@color/primary_dark_material_dark"> 20 21 <TextView 22 android:id="@+id/toolbar_title" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:layout_gravity="center" 26 android:textAppearance="@style/TextAppearance.AppCompat.Large" 27 android:text="@string/toolbar_title1" 28 android:textSize="20sp" /> 29 30 </android.support.v7.widget.Toolbar> 31 32 </android.support.design.widget.AppBarLayout> 33 34 <include layout="@layout/content_main" /> 35 36 </android.support.design.widget.CoordinatorLayout>
2.新建一个layout来放主要内容
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 </LinearLayout>
3.代码中启用toolbar
1 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 2 toolbar.setTitle("Hall"); 3 setSupportActionBar(toolbar);
4.添加其他需要的按钮,需要新建一个menu,最左边的导航按钮不包括在这里面
1 <menu xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:app="http://schemas.android.com/apk/res-auto" 3 xmlns:tools="http://schemas.android.com/tools" 4 tools:context="com.mazingtec.mazing.Hall"> 5 <item 6 android:id="@+id/action_search" 7 android:orderInCategory="80" 8 android:title="Search" 9 android:icon="@mipmap/ic_search_white_48dp" 10 app:showAsAction="ifRoom" /> 11 </menu>
5.代码中很简单了,响应时间,设置属性,最左边的home按钮的id用android.R.id.home
1 package com.mazingtec.mazing; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.support.v7.widget.Toolbar; 6 import android.view.Menu; 7 import android.view.MenuItem; 8 import android.view.View; 9 import android.widget.Toast; 10 11 public class Hall extends AppCompatActivity implements View.OnClickListener{ 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_hall); 17 18 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 19 toolbar.setTitle(""); 20 setSupportActionBar(toolbar); 21 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 22 getSupportActionBar().setHomeButtonEnabled(true); 23 } 24 25 @Override 26 public void onClick(View v) { 27 28 } 29 30 @Override 31 public boolean onCreateOptionsMenu(Menu menu) { 32 // Inflate the menu; this adds items to the action bar if it is present. 33 getMenuInflater().inflate(R.menu.toolbar_menu, menu); 34 return true; 35 } 36 37 @Override 38 public boolean onOptionsItemSelected(MenuItem item) { 39 // Handle action bar item clicks here. The action bar will 40 // automatically handle clicks on the Home/Up button, so long 41 // as you specify a parent activity in AndroidManifest.xml. 42 int id = item.getItemId(); 43 44 //noinspection SimplifiableIfStatement 45 if (id == R.id.action_search) { 46 Toast.makeText(this, "搜索!", Toast.LENGTH_LONG).show(); 47 return true; 48 } 49 50 if (id == android.R.id.home) { 51 Toast.makeText(this, "哈哈!", Toast.LENGTH_LONG).show(); 52 return true; 53 } 54 55 return super.onOptionsItemSelected(item); 56 } 57 }