简体   繁体   English

动作栏内还是下方的导航标签?

[英]Navigation tabs inside action bar or below it?

I want to know if navigation tabs are inside action bar or below it. 我想知道导航选项卡是在操作栏中还是在其下方。 In a first (non-valid) approach I assume action bar is inside action bar in landscape and below it on portrait. 在第一种(无效)方法中,我假设动作栏在横向时位于动作栏内,而在纵向时位于其下方。 Here it is the testing activity code: 这是测试活动代码:

public class TabTestActivity extends FragmentActivity implements
        ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;
    private AlertDialog.Builder dialogBuilder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_test);
        dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.setTitle("Tabs inside or below action bar?");
        dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.tab_test, menu);
        return true;
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
        mViewPager.setCurrentItem(tab.getPosition());
        AlertDialog notifyTabsLocationDialog = dialogBuilder.create();
        // First approach: assume that tabs are inside action bar in landscape
        // and below action bar in landscape, but they are always inside action
        // bar for my tablet
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            notifyTabsLocationDialog.setMessage("Tabs below action bar");
        } else {
            notifyTabsLocationDialog.setMessage("Tabs inside action bar");            
        }
        notifyTabsLocationDialog.show();
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
            }
            return null;
        }
    }

    public static class DummySectionFragment extends Fragment {
        public static final String ARG_SECTION_NUMBER = "section_number";

        public DummySectionFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_tab_test_dummy,
                    container, false);
            TextView dummyTextView = (TextView) rootView
                    .findViewById(R.id.section_label);
            dummyTextView.setText(Integer.toString(getArguments().getInt(
                    ARG_SECTION_NUMBER)));
            return rootView;
        }

    }

}

From the ActionBar guide: Adding Navigation Tabs 从ActionBar指南: 添加导航选项卡

The tabs provided by the ActionBar are ideal because they adapt to different screen sizes. ActionBar提供的选项卡非常理想,因为它们可以适应不同的屏幕尺寸。 For example, when the screen is wide enough the tabs appear in the action bar alongside the action buttons (such as when on a tablet, shown in figure 7), while when on a narrow screen they appear in a separate bar (known as the "stacked action bar", shown in figure 8). 例如,当屏幕足够宽时,选项卡将显示在操作栏中的动作按钮旁边(例如,在平板电脑上,如图7所示),而在狭窄的屏幕上,它们将出现在单独的栏中(称为“堆叠的动作栏”,如图8所示。 In some cases, the Android system will instead show your tab items as a drop-down list to ensure the best fit in the action bar. 在某些情况下,Android系统会改为将您的标签项显示为下拉列表,以确保最适合操作栏。

And yes, your assumption is quiet right. 是的,您的假设是正确的。 But the parameter is not portrait or landscape, but is there enough or not enough room to merge the tabs with the actionbar. 但是参数不是纵向或横向,而是有足够或不足的空间来合并选项卡和操作栏。

I've found a workaraound using reflection to access a boolean field mHasEmbeddedTabs of ActionBar : 我发现了一个使用反射来访问ActionBar的布尔字段mHasEmbeddedTabs

Class<?> actionBarClass = getActionBar().getClass();
Field mHasEmbeddedTabs = actionBarClass.getDeclaredField("mHasEmbeddedTabs");
mHasEmbeddedTabs.setAccessible(true);
if (mHasEmbeddedTabs.getBoolean(getActionBar())) {
    notifyTabsLocationDialog.setMessage("Tabs inside action bar");            
} else {
    notifyTabsLocationDialog.setMessage("Tabs below action bar");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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