Xml文件——activity
<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"
android:orientation="vertical"
tools:context="alice.bw.com.day03toolabardrawlayout.MainActivity">
<Button
android:id="@+id/ToolBarBaseActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="跳转到ToolBarBase页面"/>
<Button
android:id="@+id/ToolBarActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="跳转到ToolBar页面"/>
<Button
android:id="@+id/DrawLayoutActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="跳转到DrawLayout页面"/>
<Button
android:id="@+id/ToolBarDrawLayoutActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="跳转到ToolBar和DrawLayout页面"/>
</LinearLayout>
Xml文件——activity_tool_bar_base
<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"
android:orientation="vertical"
tools:context="alice.bw.com.day03toolabardrawlayout.ToolBarBaseActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolaBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1509A开发全体成员"/>
</android.support.v7.widget.Toolbar>
</LinearLayout>
MainActivity
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private Intent mIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIntent = new Intent();
}
public void onClick(View view) {
switch (view.getId()){
case R.id.ToolBarBaseActivity://ToolBar的基本使用方法
mIntent.setClass(this,ToolBarBaseActivity.class);
break;
}
startActivity(mIntent);
}
}
ToolBarBaseActivity
/**
* ToolaBar的使用
*/
public class ToolBarBaseActivity extends AppCompatActivity {
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);//如果继承的是AppCompatActivity 使用supportRequestWindowFeature(Window.FEATURE_NO_TITLE)
//如果继承的Activity使用requestWindowFeature(Window.FEATURE_NO_TITLE);,必须要在setContentView之前
setContentView(R.layout.activity_tool_bar_base);
mToolbar = (Toolbar) findViewById(R.id.toolaBar);
//设置ToolBar的导航图标
mToolbar.setNavigationIcon(R.mipmap.ic_launcher);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(ToolBarBaseActivity.this, "我是导航图标", Toast.LENGTH_SHORT).show();
}
});
//设置logo
mToolbar.setLogo(R.mipmap.ic_launcher);
mToolbar.setTitle("大标题");
mToolbar.setSubtitle("子标题");
//设置menu
mToolbar.inflateMenu(R.menu.toolbarmenu);
mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.item_1:
Toast.makeText(ToolBarBaseActivity.this, "标题一", Toast.LENGTH_SHORT).show();
break;
case R.id.item_2:
Toast.makeText(ToolBarBaseActivity.this, "标题二", Toast.LENGTH_SHORT).show();
break;
case R.id.item_3:
Toast.makeText(ToolBarBaseActivity.this, "标题三", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
});
}
}