2012-02-26 110 views
0

我用這段代碼構建了我自己的TabHost: 我有一個問題,當我從這個TabHost的活動之一打開一個新的活動,以便在沒有TabSelector的情況下打開新活動。帶活動的TabHost

public void onCreate(Bundle savedInstanceState) { 
    /*super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); */ 

    super.onCreate(savedInstanceState); 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.tabhost); 
    setupTabHost(); 
    mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider); 

    setupTab(new TextView(this), "Search", new Intent(this,Search.class),R.drawable.icon_search); 
    setupTab(new TextView(this), "Info",new Intent(this,Info.class),R.drawable.icon_info); 
    setupTab(new TextView(this), "About", new Intent(this,About.class),R.drawable.icon_about); 

    mTabHost.setCurrentTab(2); 
} 
private void setupTab(final View view, final String tag, final Intent i_TagIntent, int i_Icon) { 
    View tabview = createTabView(mTabHost.getContext(), tag,i_Icon); 

    TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(
      new Intent(i_TagIntent)); 
    mTabHost.addTab(setContent); 
} 

private static View createTabView(final Context context, final String text, int i_Icon) { 
    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null); 
    TextView tv = (TextView) view.findViewById(R.id.tabsText); 
    tv.setText(text); 

    ImageView icon = (ImageView) view.findViewById(R.id.tabsIcon); 
    icon.setImageResource(i_Icon); 

    return view; 
} 
private void setupTabHost() { 
    mTabHost = (TabHost) findViewById(android.R.id.tabhost); 
    mTabHost.setup(); 
} 

回答

0

我目前也在選項卡主機上工作,我還沒有診斷您的具體問題。但是,我確實有一點我希望是有用的建議。您已經設置了代碼以編程方式隱藏您的動作/標題欄。還有另一種方法可以做到這一點,我建議更清潔。隨着你這樣做的方式,你第一次在你的模擬器上啓動這個應用程序,你會看到酒吧彈出片刻,然後消失。第二次打開應用程序時,它不會出現。除此之外,當您切換活動時,有時會在該特定行代碼執行之前重新出現。所以,更好的方式來獲得無動作/標題欄的結果是通過AndroidManifest.xml中的文件:

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:theme="@android:style/Theme.Holo.NoActionBar" 
     android:name=".Polling" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 

正如你所看到的,第五(5)這行代碼是我剛纔提到。我想你可以稱之爲設計哲學或最佳實踐,但是當你在XML中添加行而不是在代碼中時,它在屏幕上顯得更好。這樣做可以防止動作/標題欄出現的時間 - 它會一直消失。

我最後的注意事項是關於你的標籤問題。我鼓勵你看看這個頁面:http://developer.android.com/guide/topics/ui/actionbar.html並且滾動/ Ctrl + f到關於添加導航標籤的部分。這些是一些較新的Google應用程序如何完成角色選項卡在您的應用程序中所扮演的角色。不幸的是,它比TabHost更復雜。我幾天前和你在同一條船上:做一個應用程序,我擺脫了我的行動欄。然後我發現了這個動作欄選項卡系統(在我自己的android手機上看到它並喜歡它之後),並決定我想用它來代替標籤主機。但是......就像我說的那樣,它還有點兒凌亂,所以我必須先學習一些前體。