Android:为什么自定义操作栏和操作栏标签组合在一起?

时间:2014-05-29 09:39:18

标签: android tabs android-actionbar overlay

我的android 4.0的自定义操作栏和操作栏选项卡有问题。当我的应用程序在4.4(在nexus 7.0选项卡中)运行时,它工作正常,但4.0 device.the自定义操作栏和tabbar的问题被合并,它显示在整个操作栏中。像这样

package com.android.timeline;

@SuppressLint({ "SimpleDateFormat", "NewApi" })
public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{
    public int width;
    private ActionBar actionBar;
    private TextView actionBarTitle;
    private TabPagerAdapter TabAdapter;
            ArrayList<Fragment> fragmentlist = new ArrayList<Fragment>();
    private ViewPager Tab;
    private String[] tabs = { "About", "Watch Next", "Related" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentlist.add(new AboutDetail());
        fragmentlist.add(new WatchNextDetail());
        fragmentlist.add(new RelatedDetail());
        currentAboutDetail = fragmentlist.get(0);
        TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
        Tab = (ViewPager) findViewById(R.id.pager);
        Tab.setOffscreenPageLimit(2);
        pagerchangeListener();
        setupActionBar();
    }
    private void pagerchangeListener() {

        Tab.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                 actionBar.setSelectedNavigationItem(position);
            }
        });
        Tab.setAdapter(TabAdapter);
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putInt("mMyCurrentPosition", Tab.getCurrentItem());
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        mMyCurrentPosition = savedInstanceState.getInt("mMyCurrentPosition");
        // where mMyCurrentPosition should be a public value in your activity.
    }

    private void setupActionBar() {
        actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        View cView = getLayoutInflater().inflate(R.layout.actionbartitle, null);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBarTitle = (TextView) cView.findViewById(R.id.timeline);
        getActionBar().setIcon(R.drawable.log);
        getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#f16c81")));
        actionBar.setCustomView(cView);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        final ImageView actionBarDropDownImg = (ImageView) cView
                .findViewById(R.id.pageback);
        final ImageView share = (ImageView) cView.findViewById(R.id.setting);

        final ImageView font = (ImageView) cView.findViewById(R.id.font);

        OnClickListener neww = new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (v == actionBarDropDownImg) {
                    finish();
                    overridePendingTransition(R.anim.anim_left,R.anim.anim_right);
                }

                if (v == font) {
                    ((AboutDetail) currentAboutDetail).fontIncreament();
                }
            }
        };

        actionBarDropDownImg.setOnClickListener(neww);
        share.setOnClickListener(neww);
        font.setOnClickListener(neww);

        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }
    }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
    }



    public class TabPagerAdapter extends FragmentStatePagerAdapter {
        public TabPagerAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        @Override
        public Fragment getItem(int i) {
            Log.e("LOG", "Position || " + i);
            return fragmentlist.get(i);
        }

        @Override
        public int getCount() {
            return fragmentlist.size(); // No of Tabs
        }

        @Override
        public void destroyItem(View container, int position, Object object) {

        }
    }

    @Override
    public void onTabSelected(android.app.ActionBar.Tab tab,
            FragmentTransaction ft) {
        // TODO Auto-generated method stub
          Tab.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(android.app.ActionBar.Tab tab,
            FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabReselected(android.app.ActionBar.Tab tab,
            FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }
}

请提前帮助我。

1 个答案:

答案 0 :(得分:0)

我在横向模式下遇到了同样的问题,我在这里找到了解决方案: http://andreimihu.com/blog/2013/10/17/android-always-embed-tabs-in-actionbar/

基本上,当你和我想在外面时,作者想要操纵栏内的标签。所以,只需将方法调用更改为false(来自上述链接的代码,但稍作修改):

// This is where the magic happens!
public void forceTabs() {
    try {
        final ActionBar actionBar = getActionBar();
        final Method setHasEmbeddedTabsMethod = actionBar.getClass()
            .getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
        setHasEmbeddedTabsMethod.setAccessible(true);
        setHasEmbeddedTabsMethod.invoke(actionBar, false);
    }
    catch(final Exception e) {
        // Handle issues as needed: log, warn user, fallback etc
        // This error is safe to ignore, standard tabs will appear.
    }
}