通过活动在列表上方排列tabHost

时间:2012-01-02 15:08:24

标签: java android list tabs android-tabhost

我想设置一个tabHost和一个在另一个下面的列表,所以我将它添加到垂直线性布局但是当我尝试添加选项卡主机

时它崩溃了
    public TabHost peopleTabHost;

private void CreateNewTab(String tagName, String displayedName, Class<?> intentClass)
{
    Intent intent = new Intent().setClass(this,  intentClass);
    TabHost.TabSpec spec = peopleTabHost.newTabSpec(tagName).setIndicator(displayedName);
    spec.setContent(intent);
    peopleTabHost.addTab(spec);     
}

public void onCreate(Bundle savedInstanceState)
{
      super.onCreate(savedInstanceState);
      LayoutParams rootParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
      LinearLayout rootLayout = new LinearLayout(this);
      rootLayout.setLayoutParams(rootParams);
      rootLayout.setOrientation(LinearLayout.VERTICAL);
      LinearLayout.LayoutParams listParams = new LinearLayout.LayoutParams(0, LayoutParams.FILL_PARENT);
      listParams.weight = 1;
      ListView lv_chosen = new ListView(this);
      lv_chosen.setLayoutParams(listParams);
      lv_chosen.setTextFilterEnabled(true);

      peopleTabHost = getTabHost();
      CreateNewTab("groups", "Groups", GroupsActivity.class);
      CreateNewTab("everyone", "Everyone", EveryoneActivity.class);
      CreateNewTab("contacts", "Contacts", ContactsActivity.class);

      peopleTabHost.setCurrentTabByTag("everyone");
      rootLayout.addView(peopleTabHost);
      rootLayout.addView(lv_chosen);
}

感谢

2 个答案:

答案 0 :(得分:0)

OnCreate()中,您必须先创建TabHost:

// instead of peopleTabHost = getTabHost();
peopleTabHost = new TabHost(this);  
setTabHost(peopleTabHost());

此外,您必须创建TabWidget和内容(FrameLayout)的内部布局。因此继续

LinearLayout tabhostLinearLayout = new LinearLayout(this);
tabhostLinearLayout.setOrientation(LinearLayout.VERTICAL);
peopleTabHost.addView(tabhostLinearLayout);

TabWidget tabWidget = new TabWidget(this);
tabhostLinearLayout.addView(tabWidget); 

// the FrameLayout for the content
FrameLayout frameLayout = new FrameLayout(this);
tabhostLinearLayout.addView(frameLayout); 

// and finally
peopleTabHost.setup(); 

这似乎也在OnCreate()

结束时遗漏了
 setContentView(rootLayout);

因为这是很多工作,所以我建议使用XML布局。

答案 1 :(得分:0)