Android:自定义选项卡

发布于 2024-10-11 23:48:38 字数 3095 浏览 3 评论 0原文

我决定在我的选项卡界面中使用视图...... 我遵循选项卡界面教程,本教程的结果是我的选项卡下有 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);

     }
    }

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

     }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

通知家属抬走 2024-10-18 23:48:39

这是您想要的吗?基本上,您只需要设置意图并将其与相关选项卡关联即可。

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

红衣飘飘貌似仙 2024-10-18 23:48:39

如果我理解正确,您想要将活动添加为选项卡的内容,因此您需要在 setupTab 方法中添加意图。示例:

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

这将启动您的选项卡所需的意图。现在,为了使 Intents 可见,您必须将其添加到 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文