安卓:为什么自定义操作栏和操作栏选项卡结合?操作、自定义、选项卡

2023-09-12 03:28:07 作者:无梦为安

我有问题,自定义动作条和动作条的标签为Android 4.0.when在4.4(以承上启下7.0片)我的应用程序运行,它工作正常,但与4.0 device.the自定义动作条和TabBar中的问题相结合,它在整个动作条示出。像这样

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

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

    }
}

请帮帮我,先谢谢了。

推荐答案

我和横向模式同样的问题,我发现这里的解决方案: http://andreimihu.com/blog/一十分之二千零十三/ 17 / Android的始终嵌入-选项卡功能于动作条/

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/

基本上,笔者希望内部动作条的标签,而我和你外面想要它。所以,只要方法调用更改为false(code从上面的链接,但有些修改):

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