在TabHost中创建具有不同内容的选项卡

时间:2014-02-06 21:00:26

标签: android android-tabhost

在我的应用程序中,我可以使用以下代码为TabHost视图创建选项卡:

TabSpec spec1 = tabHost.newTabSpec("1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("1");   

tabHost.addTab(spec1);

我现在的问题是我不知道要创建多少个标签(数字是在运行时生成的)。对于每个选项卡,我必须使用布局ID调用setContent,但如果我对不同的选项卡使用相同的布局ID,那么这些选项卡总是具有相同的内容。如何根据需要创建包含不同内容的标签?

这是我目前的代码,它创建了两个标签,但具有相同的布局:

TabSpec spec1=tabHost.newTabSpec("1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("1");

LinearLayout v = (LinearLayout) tabHost.findViewById(R.id.tab1);
TextView tv = new TextView(getActivity());
tv.setText("Hello World");
v.addView(tv);

tabHost.addTab(spec1);

TabSpec spec2=tabHost.newTabSpec("2");
spec2.setContent(R.id.tab1);
spec2.setIndicator("2");      

tabHost.addTab(spec2);

1 个答案:

答案 0 :(得分:2)

我正在为您添加一种动态创建标签的方法。做这些没有任何困难,你只需要清楚你正在做什么。我正在尽可能多地添加评论:

// This function defines the layout itself. In my case, my tab bar is made
// of a customized layout called 'tabs_bg', so I simply inflate my layout.
// You don't need to do this actually if you've enough with the default
// Android's layout. You don't need to call this function explicitely, the 
// function below does that.
private View createTabView(final Context context, final String text) {
  final View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);

  // In my customized layout, I have a TextView and an ImageView, I just set
  // some parameters on them.
  final TextView tv = (TextView) view.findViewById(R.id.tabsText);
  final ImageView iv = (ImageView) view.findViewById(R.id.tabsIcon);

  tv.setText(text);
  iv.setLayoutParams(new LinearLayout.LayoutParams(..., ...));

  return view;
}

// Each time you want to create a new tab, call this method, which
// will call the above one. So if you want to have a TextView as a content,
// you'd call: setupTab(new TextView(this), "My personal tab");
private void setupTab(final TextView view, final String tag) {
  final TabHost th = (TabHost) findViewById(android.R.id.tabhost);

  // The line below will create the tab in the tab bar
  final View tabview = createTabView(th.getContext(), tag);

  // And this code will create the content, take a look at the TabContentFactory part
  final TabSpec setContent = th.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
    public View createTabContent(String tag) {
      // Here tabs_content is created in a similar way than the tab layout itself,
      // with a layout file which is later inflated. All those ID's are from that file.
      final View ll_view = LayoutInflater.from(context.inflate(R.layout.tabs_content, null);
      final TextView view = (TextView) ll_view.findViewById(R.id.tabsContent);

      view.setMovementMethod(new ScrollingMovementMethod());

      // Anything else you may need... This is the content that should vary
      // on each tab!!!
      view.setText("Hello, this is my tab number: " + th.getChildCount() + 1);

      return ll_view;
    }
  });

  // There you assign your content you just created.
  th.addTab(setContent);
}