Android的删除动作条标题保持工具栏菜单 [英] Android remove actionbar title keeping toolbar menu

查看:396
本文介绍了Android的删除动作条标题保持工具栏菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要保持我的标签工具栏在我的应用程序移除动作条。这是它的外观像现在:

I need to keep my tabbed toolbar in my application removing the actionbar. This is how it looks like now:

但我希望它看起来是这样的:

But I want it to look like this:

输入图像的描述在这里

我的code XML是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

这是我的.java:

public class MyActivity extends AppCompatActivity {
    @Override
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

        if (toolbar != null) {
            setSupportActionBar(toolbar);
        }

        viewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);
    }

    public class SectionPagerAdapter extends FragmentPagerAdapter {

        public SectionPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new myFragment1();
                case 1:
                default:
                    return new myFragment2();
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "myfragment1";
                case 1:
                default:
                    return "myfragment2";
            }
        }
    }


}

我怎样才能实现呢? 感谢您的建议

How can I achieve it? Thanks in advice

推荐答案

简单地把MainActivity添加此这将隐藏工具栏的文本

Simply add this in your MainActivity this will hide the text of your toolbar

getSupportActionBar().setTitle("");

要删除的工具栏,你可以设置它的知名度,走了样。

To remove the toolbar you can set it's visibility to gone like

android:visibility="gone"

这篇关于Android的删除动作条标题保持工具栏菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆