0

我想在操作栏中使用标签。为简单起见,现在只有 1 个选项卡。问题是没有可见的视图/用户界面。

Oncreate 包含选项卡的活动的功能 -

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tabs = new Vector<ActionBar.Tab>();
    ActionBar actionbar = getActionBar();
    actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab accountsTab = actionbar.newTab().setText("Accounts");
    Fragment accountsFragment = new AccountsFragment();
    accountsTab.setTabListener(new MyTabListener(accountsFragment));
    actionbar.addTab(accountsTab);
    tabs.add(accountsTab);
}

扩展 Fragment (android.app.Fragment) 的 Fragment AccountsFragment 的 oncreateview :

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    TextView textView = new TextView(getActivity());
    textView.setText("SimpleFragmentText");
    textView.setVisibility(View.VISIBLE);
    textView.setTextSize(20);
    return textView;
}

最后 Tab 监听器如下:

 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    if(fragment!=null) {
        fragmentTransaction.add(fragment,"accounts");
        fragmentTransaction.show(fragment);
    }
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    if(fragment!=null) {
        fragmentTransaction.remove(fragment);
    }
}

所有其他功能都不会被覆盖。除了标题为“帐户”的选项卡外,什么都没有显示。

我的 main.xml 包含线性布局。

4

2 回答 2

2

在你的 onTabSelected 试试这个 -

public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // Check if the fragment is already initialized
            if (mFragment == null) {
                // If not, instantiate and add it to the activity
                mFragment = (Fragment) 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);
            }
        }
于 2012-10-19T12:08:24.080 回答
1

试试这个:

   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tabs = new Vector<ActionBar.Tab>();
        ActionBar actionbar = getActionBar();
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab accountsTab = actionbar.newTab().setText("Accounts");
        accountsTab.setTabListener(new MyTabListener());
        actionbar.addTab(accountsTab);
        tabs.add(accountsTab);
    }

    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        if(fragment!=null) {
           AccountsFragment frag = new AccountsFragment();
           fragmentTransaction.add(android.R.id.content, frag);
        }
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        if(fragment!=null) {
            fragmentTransaction.remove(fragment);
        }
    }
于 2012-10-19T12:44:49.820 回答