使用android.support.v7.app.ActionBar的标签

时间:2013-07-31 01:27:33

标签: android tabs android-actionbar android-support-library

我最近更新了我的Android应用程序,以便在android 2.3上使用v7支持actionbar和actionbar标签。更新代码后,我在4.2设备和模拟2.3设备上运行应用程序。操作栏和选项卡在真实硬件上显示正常,但在模拟设备上不显示。仅显示第一个片段,并剪切视图的左侧和顶部。这是我的主要行动主义。

  package com.rmsanger.minneapolishiltongargeninn;

  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentTransaction;
  import android.os.Bundle;
  import android.support.v7.app.ActionBar;
  import android.support.v7.app.ActionBar.Tab;
  import android.support.v7.app.ActionBarActivity;

  public class MainActivity extends ActionBarActivity {



@Override
public void onCreate(Bundle savedInstanceState) {           
    super.onCreate(savedInstanceState);

    android.support.v7.app.ActionBar actionBar = getSupportActionBar();


    //actionBar.setDisplayShowTitleEnabled(false);

    Tab tab = actionBar.newTab();   


    String label2 = getResources().getString(R.string.informationtab_label);
    tab = actionBar.newTab();
    tab.setText(label2);
    TabListener<InformationFragment> tl2 = new TabListener<InformationFragment>(this, label2, InformationFragment.class);
    tab.setTabListener(tl2);
    actionBar.addTab(tab);

    String label3 = getResources().getString(R.string.diningtab_label);
    tab = actionBar.newTab();
    tab.setText(label3);
    TabListener<DiningFragment> tl3 = new TabListener<DiningFragment>(this, label3, DiningFragment.class);
    tab.setTabListener(tl3);
    actionBar.addTab(tab);

    String label6 = getResources().getString(R.string.localattractionstab_label);
    tab = actionBar.newTab();
    tab.setText(label6);
    TabListener<LocalAttractionsFragment> tl6 = new TabListener<LocalAttractionsFragment>(this, label6, LocalAttractionsFragment.class);
    tab.setTabListener(tl6);
    actionBar.addTab(tab);


    String label7 = getResources().getString(R.string.localeventstab_label);
    tab = actionBar.newTab();
    tab.setText(label7);
    TabListener<LocalEventsFragment> tl7 = new TabListener<LocalEventsFragment>(this, label7, LocalEventsFragment.class);
    tab.setTabListener(tl7);
    actionBar.addTab(tab);

    String label5 = getResources().getString(R.string.faqtab_label);
    tab = actionBar.newTab();
    tab.setText(label5);
    TabListener<FAQFragment> tl5 = new TabListener<FAQFragment>(this, label5, FAQFragment.class);
    tab.setTabListener(tl5);
    actionBar.addTab(tab);

    String label8 = getResources().getString(R.string.dealstab_label);
    tab = actionBar.newTab();
    tab.setText(label8);
    TabListener<DealsFragment> tl8 = new TabListener<DealsFragment>(this, label8, DealsFragment.class);
    tab.setTabListener(tl8);
    actionBar.addTab(tab);

    String label4 = getResources().getString(R.string.socialmediatab_label);
    tab = actionBar.newTab();
    tab.setText(label4);
    TabListener<SocialMediaFragment> tl4 = new TabListener<SocialMediaFragment>(this, label4, SocialMediaFragment.class);
    tab.setTabListener(tl4);
    actionBar.addTab(tab);


    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.show();

    if (savedInstanceState != null) {
        actionBar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
    }   

}


@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
}   

private class TabListener<T extends Fragment> implements
    ActionBar.TabListener {
    private Fragment mFragment;
    private final ActionBarActivity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    /**
     * Constructor used each time a new tab is created.
     * 
     * @param activity
     *            The host Activity, used to instantiate the fragment
     * @param tag
     *            The identifier tag for the fragment
     * @param clz
     *            The fragment's Class, used to instantiate the fragment
     */
    public TabListener(ActionBarActivity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
        // Check if the fragment is already initialized
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(mFragment);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(mFragment);
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // User selected the already selected tab. Usually do nothing.
    }
}


  }

* 更新我更换了

     ft.add(android.R.id.content, mFragment, mTag);

     ft.add(R.id.fragView, mFragment, mTag);

其中fragView只是一个相对布局,现在显示了标签。然而,视图仍然在顶部和左侧被剪裁。

1 个答案:

答案 0 :(得分:3)

更新后使用此处描述的方法并解决了布局问题。

ActionBar with support library and Fragments overlay content