我刚刚从 Eclipse 跳转到 Android Studio,我正在修复一些出现的问题。其中之一是删除 ActionBar 提供的选项卡导航并切换到 AD 支持库提供的 Tablayout。我的一切工作都很好,但是当屏幕上出现特定片段时,我无法弄清楚如何将菜单项添加到工具栏。
我认为部分问题是我的 MainActivity 扩展了采用 setActionBar() 的 FragmentActivity,并且工具栏来自 v7 支持库 - 所以当然该工具栏与该方法不兼容。我试过 setSupportActionBar() 但这不是在 FragmentActivity 中找到的方法 - 在这里我需要扩展 ActionBarActivity 以访问该方法,这两种方法都已弃用(类和方法)。
所以问题仍然存在,如何让我的各个片段更新工具栏选项菜单中可用的选项?
活动主.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
android:orientation="vertical">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:layout_gravity="top"
android:background="@color/blue"
app:layout_scrollFlags="scroll|enterAlways"
app:layout_collapseMode="pin" />
<android.support.design.widget.TabLayout
android:id="@+id/main_sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@color/blue"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.v4.view.ViewPager
android:id="@+id/main_viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white" />
</LinearLayout>
MainActivity.java:
public class MainActivity extends FragmentActivity implements
TabLayout.OnTabSelectedListener {
private Toolbar mToolbar;
private ViewPager mViewPager;
private TabsPagerAdapter mTabsPagerAdapter;
private TabLayout mTabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// * unrelated code omitted ...
// * Initialize main navigation scheme
mToolbar = (Toolbar)findViewById(R.id.main_toolbar);
mToolbar.setTitleTextColor(getResources().getColor(R.color.white));
mToolbar.setLogo(R.drawable.icon_toolbar);
mViewPager = (ViewPager)findViewById(R.id.main_viewpager);
mTabsPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mTabsPagerAdapter);
mTabLayout = (TabLayout)findViewById(R.id.main_sliding_tabs);
mTabLayout.setupWithViewPager(mViewPager);
mTabLayout.setOnTabSelectedListener(this);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
}
}
ExampleFragment.java:
public class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_athlete, container, false);
// * unrelated code omitted ...
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
// * unrelated code omitted ...
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.example_action_menu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// * unrelated code omitted ...
return super.onOptionsItemSelected(item);
}
}