【问题标题】:How to add content to a tab in Action Bar如何将内容添加到操作栏中的选项卡
【发布时间】:2013-02-07 19:41:25
【问题描述】:

我在操作栏中有一些选项卡,如下面的代码所示。但我想向每个选项卡添加一些按钮和 EditText 等内容。我不知道该怎么做。谁能告诉我应该把这个放在哪里。我会很高兴为您提供任何帮助。提前致谢。

public class MLkeyboardActivity extends FragmentActivity implements
    ActionBar.TabListener {

/**
 * The serialization (saved instance state) Bundle key representing the
 * current tab position.
 */
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mlkeyboard);

    // Set up the action bar to show tabs.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // For each of the sections in the app, add a tab to the action bar.
    actionBar.addTab(actionBar.newTab().setText(R.string.title_section1)
            .setTabListener(this));
    actionBar.addTab(actionBar.newTab().setText(R.string.title_section2)
            .setTabListener(this));
    actionBar.addTab(actionBar.newTab().setText(R.string.title_section3)
            .setTabListener(this));
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Restore the previously serialized current tab position.
    if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
        getActionBar().setSelectedNavigationItem(
                savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    // Serialize the current tab position.
    outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
            .getSelectedNavigationIndex());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_mlkeyboard, menu);
    return true;
}


@Override
public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, show the tab contents in the
    // container view.

    Fragment fragment = new DummySectionFragment();
    Bundle args = new Bundle();
    args.putInt(DummySectionFragment.ARG_SECTION_NUMBER,
            tab.getPosition() + 1);
    fragment.setArguments(args);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, fragment).commit();
}

@Override
public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Create a new TextView and set its text to the fragment's section
        // number argument value.
        TextView textView = new TextView(getActivity());
        textView.setGravity(Gravity.CENTER);
        textView.setText(Integer.toString(getArguments().getInt(
                ARG_SECTION_NUMBER)));
        return textView;
    }
}

}

【问题讨论】:

    标签: android tabs android-fragments android-actionbar


    【解决方案1】:

    但我想向每个选项卡添加一些按钮和一个 EditText 等内容。我不知道该怎么做。

    将它们放入您正在加载的片段中onTabSelected()。

    或者,修改 onTabSelected() 以执行其他操作,而不是替换片段,以影响您对 UI 的期望更改。

    【讨论】:

    • 我会试试这个并回复你。谢谢
    • 标签页没有名字怎么重新调用?
    • @user1995307:我不知道你在说什么,对不起。
    • @user1995307:也许您应该考虑学习如何开发更简单的 Android 用户界面,然后再开始使用操作栏选项卡,因为看起来您的 Android 开发经验有限。或者,也许您应该考虑使用更适合您的语言的 Android 开发人员支持站点——我在andglobe.com 列出了几个站点。除此之外,基于粘贴到 StackOverflow cmets 中的源代码不完整,我无法帮助您。
    • 我的问题终于解决了。一个教程对我有帮助,我会把它作为我的答案。如果我们只是寻找更小的东西,我们如何学习新东西?感谢您的支持
    【解决方案2】:

    你可以参考这个:

    http://developer.android.com/reference/android/app/ActionBar.html

    此外,您可以在此基础上创建 OptionCommand 和 setAction。

    【讨论】:

    • 是否应该在setTabListener下面给出?按钮和编辑文本应该在 main.xml 文件中?
    • 您可以将其添加到 main.xml。您必须更改 onTabSelected() 方法。
    • 谢谢。但是当我将按钮放在 main.xml 中时,它将显示在所有选项卡中。我希望每个标签都有单独的内容。
    【解决方案3】:

    本教程解决了我的问题。我们要做的是创建与每个选项卡相对应的片段数量(例如 FragmentA,B,C )并使用每个选项卡调用这些片段。代码可在下面的链接中下载 http://android-er.blogspot.in/2012/06/create-actionbar-in-tab-navigation-mode.html

    public class AndroidNavigationTabsActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    
        Tab tabA = actionBar.newTab();
        tabA.setText("Tab A");
        tabA.setTabListener(new TabListener<MyFragmentA>(this, "Tag A", MyFragmentA.class));
        actionBar.addTab(tabA);
    
        Tab tabB = actionBar.newTab();
        tabB.setText("Tab B");
        tabB.setTabListener(new TabListener<MyFragmentB>(this, "Tag B", MyFragmentB.class));
        actionBar.addTab(tabB);
    
        Tab tabC = actionBar.newTab();
        tabC.setText("Tab C");
        tabC.setTabListener(new TabListener<MyFragmentC>(this, "Tag C", MyFragmentC.class));
        actionBar.addTab(tabC);
    
        if (savedInstanceState != null) {
            int savedIndex = savedInstanceState.getInt("SAVED_INDEX");
            getActionBar().setSelectedNavigationItem(savedIndex);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      相关资源
      最近更新 更多