簡體   English   中英

Android:CustomTabs

[英]Android: CustomTabs

我決定將視圖用於我的選項卡界面...我遵循一個選項卡界面教程,本教程的結果是4個選項卡,而這些選項卡下沒有內容(文本)。

我想知道視圖如何工作..我怎樣才能建立一種將內容設置為另一個類的選項卡的方法。所以main.java是帶有視圖(選項卡)的主文件。 Tab1.java具有谷歌地圖導航代碼。

如何調用方法setupTab並將導航功能設置為選項卡1。

在這里您可以看到我的代碼:
提前致謝!

 package CustomTabs;  
    import android.app.Activity;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TabHost;
    import android.widget.TextView;
    import android.widget.TabHost.TabContentFactory;
    import android.widget.TabHost.TabSpec;

    public class CustomTabs extends Activity {

     private TabHost mTabHost;

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

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      // construct the tabhost
      setContentView(R.layout.main);

      setupTabHost();

      setupTab(new TextView(this), "Tab 1", getResources().getDrawable(R.drawable.ic_tab_artists));
      setupTab(new TextView(this), "Tab 2", getResources().getDrawable(R.drawable.ic_tab_artists));
      setupTab(new TextView(this), "Tab 3", getResources().getDrawable(R.drawable.ic_tab_artists));
      setupTab(new TextView(this), "Tab 4", getResources().getDrawable(R.drawable.ic_tab_artists));
      {
       final View v = mTabHost.getTabWidget().getChildAt(0);
       v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg_selector));
       TextView tv = (TextView) v.findViewById(android.R.id.title);
       tv.setTextColor(this.getResources().getColorStateList(R.drawable.tab_text_selector));
      }
      {
       final View v = mTabHost.getTabWidget().getChildAt(1);
       v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg_selector));
       TextView tv = (TextView) v.findViewById(android.R.id.title);
       tv.setTextColor(this.getResources().getColorStateList(R.drawable.tab_text_selector));
      }
      {
       final View v = mTabHost.getTabWidget().getChildAt(2);
       v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg_selector));
       TextView tv = (TextView) v.findViewById(android.R.id.title);
       tv.setTextColor(this.getResources().getColorStateList(R.drawable.tab_text_selector));
      }
      {
       final View v = mTabHost.getTabWidget().getChildAt(3);
       v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_bg_selector));
       TextView tv = (TextView) v.findViewById(android.R.id.title);
       tv.setTextColor(this.getResources().getColorStateList(R.drawable.tab_text_selector));
      }

     }

     private void setupTab(final View view, final String tag, Drawable icon) {
      TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tag, icon).setContent(new TabContentFactory() {
       public View createTabContent(String tag) {return view;}
      });
      mTabHost.addTab(setContent);

     }
    }

就是你想要的嗎? 基本上,您只需要設置意圖並將其與相關選項卡相關聯。

如果我理解此權利,則希望將活動添加為選項卡的內容,因此需要在setupTab方法中添加意圖。 例:

setupTab(new TextView(this), "title", getResources().getDrawable(R.drawable.icon), new Intent(this, yourclass.class));

這將啟動選項卡的需求。 現在,為了使Intent可見,您必須將其添加到setupTab方法中,如下所示:

private void setupTab(final View view, final String tag, Drawable drawable, Intent     intent) {
    View tabview = createTabView(mTabHost.getContext(), tag, 0);
    TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent);
    mTabHost.addTab(setContent);

如果這不是您要查找的內容,則需要重新陳述您的問題。

暫無
暫無

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

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