这就是我使用工具栏的方式.它是一个独立的工具栏,可让您更好地控制其在类中的功能.
activity_home.xml
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/tool_bar"
android:layout_height="56dp"
android:layout_width="match_parent"
android:elevation="5dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:background="@color/primary" />
android:layout_below="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="match_parent">
样式
@color/primary
@color/secondary
@color/black
@null
true
false
true
true
true
** MyActivity **
public class MyActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// Set navigation icon
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_launcher));
// Navigation onClickLister
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// finish(); // or your action here
}
});
//Inflate menu
toolbar.inflateMenu(R.menu.menu_my);
// menu items
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
// Your action here
return true;
}
return false;
}
});
// Clear toolbar icons
toolbar.getMenu().clear(); // I believe this is what you are looking for
// Set title
toolbar.setTitle("Title");
//Set SubTitle
toolbar.setSubtitle("Sub Title");
}
}
更改工具栏内所有内容的工作示例
布局
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/toolbar"
android:layout_height="56dp"
android:layout_width="match_parent"
android:elevation="5dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:background="@color/primary" />
android:layout_below="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="Change Menu "
android:gravity="center_vertical"
android:id="@+id/swChaneMenu" />
菜单1
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
android:id="@+id/toast"
android:title="Toast"
android:orderInCategory="100"
android:icon="@drawable/ic_done_black"
app:showAsAction="always" />
菜单2
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
活动
public class TestingActionBar extends ActionBarActivity {
SwitchCompat swChaneMenu;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testing_action_bar);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Toolbar Test 1");
toolbar.inflateMenu(R.menu.menu_testing_action_bar);
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.nav_tint));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if (id == R.id.toast) {
Toast.makeText(TestingActionBar.this, "Toasty", Toast.LENGTH_SHORT).show();
return true;
}
if (id == R.id.action_settings) {
Toast.makeText(TestingActionBar.this, "Settings", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
swChaneMenu = (SwitchCompat) findViewById(R.id.swChaneMenu);
swChaneMenu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toolbar.setTitle("Toolbar Test 2");
toolbar.getMenu().clear();
toolbar.inflateMenu(R.menu.menu_settings);
toolbar.setNavigationIcon(null);
toolbar.setBackgroundColor(getResources().getColor(R.color.red));
} else {
toolbar.setTitle("Toolbar Test 1");
toolbar.getMenu().clear();
toolbar.inflateMenu(R.menu.menu_testing_action_bar);
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.nav_tint));
toolbar.setBackgroundColor(getResources().getColor(R.color.primary));
}
}
});
}
}
未检查开关
切换检查