“setSupportActionBar(toolbar)” inside FragmentActi

2020-05-21 07:34发布

问题:

How do I access setSupportActionBar(Toolbar toolbar) inside FragmentActivity? I can't access it inside FragmentActivity

toolbar = (Toolbar) findViewById(R.id.search_bar);
setSupportActionBar( toolbar);

回答1:

With the latest version of the support library you should make your Activity extend AppCompatActivity as ActionBarActivity has been deprecated.

It provides the same functionality as your ActionBarActivity previously did. You shouldn't need to make any further changes.



回答2:

You can just extend your class with AppCompatActivity, since AppCompatActivity extends FragmentActivity internally. Also, ActionBarActivity is deprecated.



回答3:

Use ActionBarActivity from support library, ActionBarActivity extends FragmentActivity, So that you can get SupportFragmentManager and set toolbar as actionbar

Ex:

public class MainActivity extends ActionBarActivity
{

 Toolbar toolbar = (Toolbar) findViewById(R.id.search_bar);
 setSupportActionBar( toolbar);

 FragmentManager manager=this.getSupportFragmentManager();
}


回答4:

If your class extends FragmentActivity

and if the toolbar is inside the layout you used, it will be set by default. To access it simply do

(Toolbar) findViewById(R.id.toolbar)



回答5:

AppCompatActivity extends FragmentActivity

public class AppCompatActivity extends FragmentActivity implements AppCompatCallback, SupportParentable, DelegateProvider

you can use AppCompatActivity instead



回答6:

Use this methods your activity need to extend ActionBarActivity instead of FragmentActivity

toolbar = (Toolbar) findViewById(R.id.search_bar);
setSupportActionBar( toolbar);

Hope it helps



回答7:

If you want your ViewPager to add fragments, you can extend AppCompatActivity, it also works. (Often, we extend FragmentActivity, but after that setSupportActionBar( toolbar) doesn't work) So, we can extend AppCompatActivity instead of extending FragmentActivity.



回答8:

For FragmenrtActivity, you should look into FragmentTabHost, and to add tabs simply:

tab = (FragmentTabHost)findViewById(android.R.id.tabhost);
tab.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
tab.addTab(tabs.newTabSpec("tab1").setIndicator("TAB1"), tab1.class, null);


回答9:

Instead of using setSupportActionBar use setActionBar Eg:

android.widget.Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);    
setActionBar(toolbar);