Android中的标签,如何创建内容

时间:2013-02-01 10:25:40

标签: android eclipse tabs fragment

嘿,我是创建Android应用程序的新手。我想在Actionbar中创建三个Tabs。我已经通过使用New-> Android Activity->创建它们来完成此操作。使用滑动和标签进行导航。

这就是我的MainActivity代码(使用了android.com&#39教程之一):

 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.
            int current_section = getArguments().getInt(ARG_SECTION_NUMBER);

            switch(current_section){
            case 1:
            {

                EditText element1 = new EditText(getActivity());
                element1.setHint(R.string.edit_text_hint);
                element1.setGravity(Gravity.TOP);
                element1.setPadding(40,40,40,0);
                element1.setSingleLine();

                TextView textView = new TextView(getActivity());
                textView.setGravity(Gravity.CENTER);
                textView.setText("Search");


                return element1;
            }
            case 2:
            {
                TextView textView = new TextView(getActivity());
                textView.setGravity(Gravity.CENTER);
                textView.setText("User Account");
                return textView;
            }
            case 3:
            {
                TextView textView = new TextView(getActivity());
                textView.setGravity(Gravity.CENTER);
                textView.setText("Last Updated");
                return textView;
            }
            }
            TextView textView1 = new TextView(getActivity());
            textView1.setGravity(Gravity.CENTER);
//          textView.setText(Integer.toString(getArguments().getInt(
//                  ARG_SECTION_NUMBER)));
            return textView1;
        }
    }

因此,在三个标签中的每个标签中都有一个不同的项目显示,不幸的是它只能显示一个 - 而不是更多。我认为这是一个相当简单的问题,但我不知道这是如何起作用的。 希望你能帮助我。

1 个答案:

答案 0 :(得分:2)

在本教程中,您将返回 textview 。尝试将textview添加到Layout,然后返回布局。

我认为你想要的是为不同的标签使用不同的片段。然后,您可以在片段本身中指定布局和其他代码,而不是使用switch case构建一个BIG片段。

对于每个片段,您都会创建一个新类。此片段可以添加到 TabBarListener

片段 onCreateView 中,您可以通过向视图参数添加对象来以编程方式创建布局。您还可以使用充气程序来扩充XML布局。

我创建了一个示例项目,1 Fragment 视图被夸大,另一个是以编程方式创建的:

here

如果你阅读了android文档,那就更清楚了。我认为他们甚至有一个示例代码项目。

Actiobar, the tab sections

More info about fragments

相关问题