簡體   English   中英

在其他活動中添加標簽

[英]Putting tabs in other activities

我有5個標簽的tabactivity 每個類都有自己的意圖。 現在,我想做的是將同一選項卡放入tabhosts tabspec未包括的其他活動中。 我是Android的初學者。 請多多包涵。

hometab.java:

public class HomeTab extends TabActivity {

private static final String LOG_TAG = null;
@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.activity_home_tab);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
    Resources ressources = getResources(); 

    final TabHost tabHost = getTabHost(); 
    Intent intentHome= new Intent().setClass(this, Home.class);
    TabSpec tabSpecHome = tabHost
      .newTabSpec("Home")
      .setIndicator("", ressources.getDrawable(R.drawable.home))
      .setContent(intentHome);

    Intent intentShopInfo= new Intent().setClass(this, ShopInfo.class);
    TabSpec tabSpecShopInfo = tabHost
      .newTabSpec("ShopInfo")
      .setIndicator("", ressources.getDrawable(R.drawable.home))
      .setContent(intentShopInfo);


    // Android tab  
    Intent intentDiscover= new Intent().setClass(this, MainAct.class);
    TabSpec tabSpecDiscover = tabHost
      .newTabSpec("MainAct")
      .setIndicator("", ressources.getDrawable(R.drawable.discover))
      .setContent(intentDiscover);

    // Apple tab
    Intent intentMode = new Intent().setClass(this, Mode.class);
    TabSpec tabSpecMode = tabHost
      .newTabSpec("Mode")
      .setIndicator("", ressources.getDrawable(R.drawable.mode))
      .setContent(intentMode);

    // Windows tab
    Intent intentExplore = new Intent().setClass(this, Explore.class);
    TabSpec tabSpecExplore = tabHost
      .newTabSpec("Explore")
      .setIndicator("", ressources.getDrawable(R.drawable.explore))
      .setContent(intentExplore);
    Intent intentSearch = new Intent().setClass(this, SearchView.class);
    TabSpec tabSpecSearch = tabHost
      .newTabSpec("SearchView")
      .setIndicator("", ressources.getDrawable(R.drawable.search))
      .setContent(intentSearch);

    // Blackberry tab


    // add all tabs 
    tabHost.addTab(tabSpecHome);
    tabHost.addTab(tabSpecDiscover);
    tabHost.addTab(tabSpecExplore);
    tabHost.addTab(tabSpecSearch);
    tabHost.addTab(tabSpecMode);

     for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
            tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#eb8114")); //unselected


    tabHost.setOnTabChangedListener(new OnTabChangeListener() {
        @Override
        public void onTabChanged(String arg0) {         
             WifiManager wifi;
            wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
            setTabColor(tabHost);
            ConnectionDetector cd = new ConnectionDetector(getApplicationContext());


            Boolean isInternetPresent = cd.isConnectingToInternet();

            //Checking whether  Wifi is on or off
            if(isInternetPresent)
            {

            }
            else
            {
                if(tabHost.getCurrentTab() == 1)
                {
                Toast.makeText(HomeTab.this,
                          "No Internet Connection Please Enable Wifi Mode to Enjoy Discover Service "  , Toast.LENGTH_LONG)
                          .show();
                tabHost.setCurrentTab(2);

                }

            }
        }       
    });  

    }
public void setTabColor(TabHost tabhost) {


    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
        tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#eb8114")); //unselected

    if(tabhost.getCurrentTab()==0)
    {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);


    }
    else if(tabhost.getCurrentTab()==1)
    {

            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.dis);
    }
    else if(tabhost.getCurrentTab()==2)
    {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.exp);
    }
    else if(tabhost.getCurrentTab()==3)
    {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.sea);
    }
    else if(tabhost.getCurrentTab()==4)
    {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mod);
    }
}

 public void Back(View v)
 {
    Toast.makeText(HomeTab.this,
          "No Internet Connection Please Enable Wifi Mode to Enjoy Discover Service "  , Toast.LENGTH_LONG)
          .show();
 }
 public void Next(View v)
 {
    Intent intent = new Intent(HomeTab.this,Dbase.class);
startActivity(intent);
 }

}

XML

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="20dp"
        android:padding="5dp" />
          <TabWidget
        android:id="@android:id/tabs"

        android:layout_marginTop="20dp"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    </RelativeLayout>
</TabHost>
</RelativeLayout>

首先十分感謝。

使用此參考。 使用ActivityGroup然后用意圖代碼替換視圖,如下所示:

Intent intent = new Intent(Events.this,EventInfo.class);
replaceContentView("activity3", intent);

使用的方法是:

private void replaceContentView(String id, Intent newIntent) {
        // TODO Auto-generated method stub
        View view = getLocalActivityManager().startActivity(id,
                newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                .getDecorView();
        this.setContentView(view);
    }

那就是為我解決問題的原因。 我希望這對很多人有幫助。

暫無
暫無

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

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