【问题标题】:Android: CustomTabsAndroid:自定义选项卡
【发布时间】:2011-01-14 08:14:57
【问题描述】:

我决定为我的标签界面使用视图... 我遵循一个标签界面教程,本教程的结果是我的标签下没有内容(文本)的 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);

     }
    }

【问题讨论】:

    标签: java android xml tabs android-tabhost


    【解决方案1】:

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

    【讨论】:

    • 不,我不这么认为.. 我想创建一个可以说的视图方法.. example public void ()setcontent View v { TextView textview = new TextView(this); textview.setText("test") } 但我不知道如何创建这个方法。
    • 对不起,我真的不明白你的问题。你问如何设置标签内容,对吧?创建一个将执行您的特定类的意图, Intent intent = new Intent().setClass(this, your_class.class);然后在 your_class 中,您可以定义 setText 或任何您想做的事情。
    【解决方案2】:

    如果我理解正确,您想将活动添加为选项卡的内容,因此您需要在 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);
    

    如果这不是您想要的,那么您需要重申您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-20
      • 2012-05-03
      • 1970-01-01
      • 2014-04-08
      • 1970-01-01
      • 1970-01-01
      • 2018-06-23
      相关资源
      最近更新 更多