簡體   English   中英

android tabhost標簽和視圖不匹配

[英]android tabhost tab and view not matching

我的tabwidget中有兩個標簽,每個標簽顯示一個列表視圖。 最初,我的第一個標簽顯示為選中狀態,但列表下方對應於第二個標簽。 單擊選項卡后,我將獲得正確的顯示。

private static final String LIST_TAB_TAG1 = "UpcomingEvents";
private static final String LIST_TAB_TAG2 = "PastEvents";

tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG1)
            .setIndicator(LIST_TAB_TAG1)
            .setContent(new TabContentFactory() {
                public View createTabContent(String arg) {
                    return listView1;
                }
            }));
    tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG2)
            .setIndicator(LIST_TAB_TAG2)
            .setContent(new TabContentFactory() {
                public View createTabContent(String arg) {
                    return listView2;
                }
            }));
    tabHost.setCurrentTab(0);

啟動此腳本時,LIST_TAB_TAG1高亮顯示,但顯示的列表為listview2。 僅在活動開始時才出現此問題。 單擊選項卡后,其工作正常

可以幫我解決這個問題。 謝謝你的時間

我通常使用docs中給出的代碼,該代碼對我來說效果很好,請嘗試使用此代碼。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab

    Intent intent;  // Reusable Intent for each tab
    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, UpcomingEvents.class);
    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("UpcomingEvents").setIndicator("UpcomingEvents",getResources().getDrawable(R.drawable.homebutton1)).setContent(intent);
    tabHost.addTab(spec);
    // Do the same for the other tabs
    intent = new Intent().setClass(this,PastEvents.class);
    spec = tabHost.newTabSpec("PastEvents").setIndicator("PastEvents",
                      getResources().getDrawable(R.drawable.bank_transaction1))
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM