Android 移除应用图标操作栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21602713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:48:42  来源:igfitidea点击:

Android remove app icon action bar

androidandroid-actionbar

提问by user3272799

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

大家好,我创建了 ActionBar。视图页面和自定义操作栏。现在我想删除(隐藏)ActionBar 中的应用程序图标,我使用了 setDisplayShowHomeEnabled(false); 但什么也没发生。这是我的代码

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.

怎么了 ?如果有人知道解决方案请帮助我谢谢。

回答by android learner

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)

如果您想在完整应用程序使用中隐藏应用程序图标 setDisplayShowHomeEnabled(false)setDisplayShowTitleEnabled(false)

回答by AndroidHacker

Try this ..

尝试这个 ..

setDisplayShowHomeEnabled(false);

More Reference

更多参考

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

更新 ::- 如果您使用 XML 自定义了操作栏,那么您可以尝试一些类似 ..

<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

更新2

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.

操作栏会自动为您调整所有内容。例如:如果您的标题栏中有很多东西,那么操作栏会调整标题,例如 YourPr...而不是像 YourProject 这样的完整标题。

You need not to wonder for that.

你不必为此感到奇怪。

UPDATE 3

更新 3

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

如果有人想自定义 ActionBar 那么它可以很容易地完成,如 ..

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

然后现在在这里你有你的 textview .. 或者任何你想要的东西,你可以在这里有一个 btn ..

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

现在只需应用点击侦听器或任何您喜欢的东西..

Hope it helps!

希望能帮助到你!

回答by Subhalaxmi

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

将显示主页图标。所以只使用getSupportActionBar().setCustomView(R.layout.menu_example);

and to hide the app icon use

并隐藏应用程序图标使用

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

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

回答by CoDe

There are multiple way to do it:

有多种方法可以做到:

Runtime handling:

运行时处理:

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


getActionBar().setDisplayShowHomeEnabled(false);

From xml:

来自 xml:

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

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

找到有关以下线程的更多信息:从 android 上的操作栏中删除图标/徽标

回答by Ravi Yadav

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