Android : why custom action bar and action bar tabs combine?

帅比萌擦擦* 提交于 2019-12-12 02:45:48

问题


I have problem with the custom actionbar and actionbar tabs for android 4.0.when my application run in the 4.4(in nexus 7.0 tabs)it works fine,but the problem with 4.0 device.the custom actionbar and tabbar are combined and it shown in the whole actionbar. like this

Class

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

    }
}

please help me ,thanks in advance.


回答1:


I had same issues with landscape mode and I found the solution here: http://andreimihu.com/blog/2013/10/17/android-always-embed-tabs-in-actionbar/

Basically, the writer wants the tab inside actionbar while you and I wants it outside. So, just change the method call to false (code from the above link, but a bit modified):

// 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.
    }
}


来源:https://stackoverflow.com/questions/23930264/android-why-custom-action-bar-and-action-bar-tabs-combine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!