java - Android 删除操作栏标题保留工具栏菜单

标签 java android tabs android-actionbar toolbar

我需要将选项卡式工具栏保留在应用程序中,并删除操作栏。现在是这样的: enter image description here

但我希望它看起来像这样:

enter image description here

我的代码 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";
            }
        }
    }


}

我怎样才能实现它? 谢谢指教

最佳答案

只需将其添加到您的 MainActivity 中,这将隐藏工具栏的文本

getSupportActionBar().setTitle("");

要删除工具栏,您可以将其可见性设置为消失

android:visibility="gone"

关于java - Android 删除操作栏标题保留工具栏菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33692388/

相关文章:

Android:当 Activity 通过浏览器链接启动时,onActivityResult Intent 为空

android - Android开发中如何正确处理View State

c# - 如何从 TabCointainer (AjaxToolKit) 中选择一个选项卡

xcode - 构建阶段选项卡隐藏或丢失

java - svn: checkout 项目时....jar 的校验和不匹配

java - 如何获取 TYPE_3BYTE_BGR 中 jpeg 图像的 rgb 值?

java - .apk 不会安装在 AVD 上。 list 未更新?

angularjs - 将选项卡 View 重新定位到 nativescript Angular 的底部

java - Cobertura 和 jetty

java - 重复打印输出