简体   繁体   English

Android:CustomTabs

[英]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.. 我决定将视图用于我的选项卡界面...我遵循一个选项卡界面教程,本教程的结果是4个选项卡,而这些选项卡下没有内容(文本)。

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). 我想知道视图如何工作..我怎样才能建立一种将内容设置为另一个类的选项卡的方法。所以main.java是带有视图(选项卡)的主文件。 Tab1.java has google maps navigate code. Tab1.java具有谷歌地图导航代码。

How I can invoke the method setupTab and set the navigation function to tab 1. 如何调用方法setupTab并将导航功能设置为选项卡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. 如果我理解此权利,则希望将活动添加为选项卡的内容,因此需要在setupTab方法中添加意图。 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: 现在,为了使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);

If this is not what you are looking for, then you need to restate your question. 如果这不是您要查找的内容,则需要重新陈述您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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