简体   繁体   中英

Android: CustomTabs

I have decided to use views for my tab interface... I follow a tab interface tutorial and the result of this tutorial is 4 tabs without content (text) under my tabs..

I was wondering how views working.. how can I make a method to set content to tab from an another class.. So main.java is my main file with the views (tabs). Tab1.java has google maps navigate code.

How I can invoke the method setupTab and set the navigation function to tab 1.

Here you can see my code:
Thanks in advance!

 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);

     }
    }

Is this what you want? Basically you just need to set the intent and associate it with the relevant tab.

If I understand this right, you want to add your activities as the content of your tabs, there fore you need to add the intent in the setupTab method. Example:

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

This starts the intents need for your tabs. Now in order for the Intents to be visible you have to add it to your setupTab method like so:

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);

If this is not what you are looking for, then you need to restate your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM