我正在使用v7.widget.Toolbar支持库和v7.app.ActionBarActivity的工具栏。当onCreateOptionsMenu在工具栏上膨胀时,我看到一个标准" Back"工具栏最左侧的箭头和标准"溢出"工具栏最右侧的圆点。
我想隐藏或禁用这些默认图标,并替换为另一组Android推荐图标。关于如何解决的任何想法?
MyActiviy.java:
公共类MyActivity扩展了ActionBarActivity {
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home); {
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("sc");
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
...
答案 0 :(得分:16)
这就是我使用工具栏的方式。它是一个独立的工具栏,可让您更好地控制其在类中的功能。
<强> activity_home.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
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" />
<FrameLayout
android:layout_below="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Main Content-->
</FrameLayout>
</RelativeLayout>
<强>样式强>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/secondary</item>
<item name="colorAccent">@color/black</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="windowActionBarOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="windowActionModeOverlay">true</item>
</style>
** 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");
}
}
更改工具栏内所有内容的工作示例
<强>布局强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
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" />
<FrameLayout
android:layout_below="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Main Content-->
<android.support.v7.widget.SwitchCompat
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" />
</FrameLayout>
</RelativeLayout>
菜单1
<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">
<item
android:id="@+id/toast"
android:title="Toast"
android:orderInCategory="100"
android:icon="@drawable/ic_done_black"
app:showAsAction="always" />
</menu>
菜单2
<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">
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
<强>活动强>
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));
}
}
});
}
}
未选中切换
切换检查
答案 1 :(得分:-1)
toolbar.getMenu().clear; //是的,它对我有用。(自定义工具栏)