Android remove app icon action bar

2019-04-05 07:05发布

问题:

Hello everyone i create ActionBar. viewpages and custom action bar.Now i want to remove(hide) app icon in ActionBar i used setDisplayShowHomeEnabled(false); but nothing happened. This is a my code

public class MainActivity extends FragmentActivity implements TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "test1", "test1", "test1", "test1",
        "test1" };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    actionBar = getActionBar();
    actionBar.setHomeButtonEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);
    //actionBar.setIcon(R.color.white);
    actionBar.setDisplayShowTitleEnabled(true);
    Drawable d = getResources().getDrawable(R.drawable.acttitle);
    getActionBar().setBackgroundDrawable(d);

    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
            | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);

    viewPager = (ViewPager) findViewById(R.id.vp_main);
    viewPager.setAdapter(mAdapter);

    getActionBar().setCustomView(R.layout.menu_example);
     actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM |
     ActionBar.DISPLAY_SHOW_HOME );
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {

            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // actionBar.setStackedBackgroundDrawable(getResources().getDrawable(
    // R.drawable.background)); background viewpager

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {

    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

}

what is wrong ? if anyone know solution please help me thank you.

回答1:

You use

 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
            | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE); 

So You Are getting Home icon.

If You want to hide app icon in Particular Activity Use

getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent))); 

If You want to hide app icon in Full Application Use setDisplayShowHomeEnabled(false) and setDisplayShowTitleEnabled(false)



回答2:

Try this ..

setDisplayShowHomeEnabled(false);

More Reference

UPDATE ::- If you have customized your action bar using XML then you can try some thing like ..

<item name="android:displayOptions"></item>

This eventually will hide your app icon and title ..

As suggested on stack you can also try some combination like ..

getSupportActionBar().setHomeButtonEnabled(false);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(false);
    getSupportActionBar().setIcon(R.color.transparent);
    getSupportActionBar().setDisplayShowTitleEnabled(true);

UPDATE2

Action Bar automatically adjusts every stuff for you. For Ex: If you have lot of stuffs in you title bar then Action Bar adjusts title some thing like YourPr... instead of your complete title like YourProject.

You need not to wonder for that.

UPDATE 3

If some one want to customize ActionBar then it can easily be accomplished like ..

actionBar = getSupportActionBar();

        actionBar.setCustomView(R.layout.action_bar_confirm_cabs);

        fare_ll = (TextView) actionBar.getCustomView().findViewById(
                R.id.fare_ll);
        confirm_back = (LinearLayout) actionBar.getCustomView().findViewById(
                R.id.confirm_cabs_back);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

Then now here you have your textview .. or any thing you wish like you may have a btn over here ..

Simply now apply click listener or any thing you like ..

Hope it helps!



回答3:

There are multiple way to do it:

Runtime handling:

getActionBar().setIcon(
       new ColorDrawable(getResources().getColor(android.R.color.transparent)));  


getActionBar().setDisplayShowHomeEnabled(false);

From xml:

<item name="android:icon">@android:color/transparent</item>

find more information on following thread: Remove icon/logo from action bar on android



回答4:

Use

getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent))); 

or

getActionBar().setCustomView(R.layout.menu_example);
         actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM |
         ActionBar.DISPLAY_SHOW_HOME ); 

will display the home icon. So Use Only getSupportActionBar().setCustomView(R.layout.menu_example);

and to hide the app icon use

getSupportActionBar().setDisplayShowHomeEnabled(false); getSupportActionBar().setDisplayShowTitleEnabled(false);



回答5:

My requirement is to keep back button and action bar title without app icon/logo. This worked for me:

//For Displaying Back button

getActionBar().setDisplayHomeAsUpEnabled(true);

//For hiding logo:

getActionBar().setLogo(new ColorDrawable(getResources().getColor(android.R.color.transparent)));