简体   繁体   中英

Android remove actionbar title keeping toolbar menu

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:

在此处输入图片说明

My code xml is:

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

This is my .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

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"

Here are your options:

1. It looks like you don't need Toolbar here at all. You can remove it from your xml file and add this line

getSupportActionBar().hide();

to your MyActivity.java instead of

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

2. If you want to keep the Toolbar in case of using it later, you can hide it in either xml file

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

or in your MyActivity.java after setting Toolbar as support action bar

if (toolbar != null) {
    setSupportActionBar(toolbar);
}
toolbar.setVisibility(View.GONE);

Add following code to disable actionbar and enable toolbar in following files :-

Add this code to styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
</style>

for custom toolbar create layout/toolbar.xml

<ImageView
    android:id="@+id/backButton"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_marginLeft="16dp"
    android:background="@drawable/back"
    android:padding="24dp"
    android:visibility="gone" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/toolbarText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textColor="@color/white" />

</LinearLayout>

in layout/activity.xml

<include
    android:id="@+id/mallToolbar"
    layout="@layout/toolbar" />

in MainActivity.java Add:-

    toolbar = (Toolbar) findViewById(R.id.mallToolbar);

    setSupportActionBar(toolbar);

Now you have added a custom toolbar to your android activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM