Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

android - How does one remove default Toolbar menu items and replace with different icons?

I am using a toolbar from v7.widget.Toolbar support library and v7.app.ActionBarActivity. When the onCreateOptionsMenu is inflated on the toolbar, I see a standard "Back" arrow on the leftmost side of the toolbar and standard "Overflow" dots on the rightmost portion of the toolbar.

I would like to hide or disable these default icons and replace with another set of Android recommended icons. Any ideas on how to fix?

MyActiviy.java:

public class MyActivity extends 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);
...
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is how I use the Toolbar. It is a standalone toolbar which give you more control over its functionality within the class.

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>

Styles

<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");

    }
}

Working Example of changing everything inside the toolbar

Layout

<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>

Menu 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>

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>

Activity

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));
            }
            }
        });
    }
}

Switch Not Checked

enter image description here

Switch Checked

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...