EmptyActivite 基础上添加Toolbar
MainActivity.java
import android.app.Activity;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// EmptyActivite 基础上添加Toolbar
// step1,AppCompatActivity
// step2,noActionBar manifest
// step3,activitymain.xml
// step4, new Toolbar
Toolbar tb = (Toolbar) findViewById(R.id.my_toolbar);
tb.setTitle("newToolbar");
setSupportActionBar(tb);
// 背景图标 or 背景颜色
tb.setBackgroundResource(R.drawable.firefox);
tb.setBackgroundResource(android.R.color.holo_blue_bright);
// 添加先上导航,在ActionBar上添加
ActionBar ab =getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
// 测试
// ab.hide();
}
// 添加ToolBar 的选项菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mif = new MenuInflater(this);
mif.inflate(R.menu.optmenu, menu);
menu.add("addMenu1");
menu.add("addMenu2");
System.out.println(menu.size());
return super.onCreateOptionsMenu(menu);
}
// ToolBar 选项菜单事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
System.out.println("item");
System.out.println(item.getTitle());
System.out.println(item.getItemId());
System.out.println(item.getMenuInfo());
return super.onOptionsItemSelected(item);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.java.m1004c">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.java.m1004c.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</RelativeLayout>
optmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu1"
android:icon="@android:drawable/ic_dialog_email"
android:title="menu1.title"
android:showAsAction="ifRoom"
/>
<item
android:id="@+id/menu2"
android:title="menu2.title"
android:showAsAction="always"
/>
</menu>