how to set toolbar on FragmentActivity?

后端 未结 4 1655
日久生厌
日久生厌 2021-02-19 06:52

I want to set toolbar on my activity which extends FragmentActivity. I know that for use setSuppoertActionBar(toolbar) method we e

相关标签:
4条回答
  • 2021-02-19 07:26

    This thing is good when you are using NavigationDrawer use this:-

     toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
    

    then set Toolbar Title according to different fragment with different Titles in onNavigationItemSelected :-

    @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
    
            sfm = getSupportFragmentManager();
            Fragment fragment = new Fragment();
            int id = item.getItemId();
    
            if (id == R.id.nav_id) {
                fragment = new YourFragment();
                toolbar.setTitle("SET TOOLBAR NAME");
            }else if (id == R.id.nav_id2) {
                fragment = new YourFragment();
                toolbar.setTitle("SET TOOLBAR NAME");
            } 
    

    For single fragment, first customize your style.xml like this :-

    <style name="YourStyleName" parent="Theme.AppCompat.Light.DarkActionBar">
    // ToDo as you want to do or as per your requirement
    
    </style>
    

    then apply into your custom toolbar:-

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/YourStyleName" >
    
      // ...........
    
    
    </android.support.v7.widget.Toolbar>
    
    0 讨论(0)
  • 2021-02-19 07:36

    You should extend your Activity from AppCompatActivity as this includes support for Fragments and the Toolbar. And then override the

    onCreateOptionsMenu() and onOptionsItemSelected() methods

    You shouldn't be overriding

    onMenuItemSelected()

    public class MainActivity extends AppCompatActivity { ...
    
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FragmentTransaction ft = getSupportFragmentManager.beginTransaction();
        ....
    
    
        ....
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
          ...
             ...
        }
    
    
       @Override
       public boolean onOptionsItemSelected(MenuItem item) {
    
       ...
      }
    
    0 讨论(0)
  • 2021-02-19 07:43
    First set style as  
    <style name="style_1" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
    
    </style>
    then
    
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
            actionBar.setTitle("Home");
    
    0 讨论(0)
  • 2021-02-19 07:45

    You can also create your own toolbar:

    First set up the main theme to extend Theme.AppCompat.Light.NoActionBar

    <style name="style_1" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
    
    </style>
    

    Remember to apply the theme:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.setTheme(R.style.style_1);
        // ...
    }
    

    then in your Activity's xml you can set your own custom toolbar:

    <include layout="@layout/my_toolbar"/>
    

    where @layout/my_toolbar may look like this:

    <android.support.v7.widget.Toolbar 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolbar_height"
        app:contentInsetStart="0dp"
        app:layout_collapseParallaxMultiplier="1.0">
    
        <!-- insert your views here -->
    </android.support.v7.widget.Toolbar>
    
    0 讨论(0)
提交回复
热议问题