“setSupportActionBar(toolbar)” inside FragmentActi

2020-05-21 07:32发布

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

9条回答
看我几分像从前
2楼-- · 2020-05-21 08:02

AppCompatActivity extends FragmentActivity

public class AppCompatActivity extends FragmentActivity implements AppCompatCallback, SupportParentable, DelegateProvider

you can use AppCompatActivity instead

查看更多
霸刀☆藐视天下
3楼-- · 2020-05-21 08:10

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

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

Hope it helps

查看更多
▲ chillily
4楼-- · 2020-05-21 08:10

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.

查看更多
疯言疯语
5楼-- · 2020-05-21 08:12

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)

查看更多
SAY GOODBYE
6楼-- · 2020-05-21 08:13

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

查看更多
劳资没心,怎么记你
7楼-- · 2020-05-21 08:15

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);
查看更多
登录 后发表回答