2012-01-26 85 views
1

我在互联网上发现了这个自定义的TabWidget布局。问题是,我不知道如何将内容插入到标签中。有人可以帮忙吗?假设我有activity1,activity2和activity3。继承人的代码:如何将活动分配为标签内容?

public class MainActivity 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); 
    setContentView(R.layout.main); 

    setupTabHost(); 
    mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider); 

    setupTab(new TextView(this), "EASY"); 
    setupTab(new TextView(this), "MEDIUM"); 
    setupTab(new TextView(this), "INTENSE"); 
} 

private void setupTab(final View view, final String tag) { 
    View tabview = createTabView(mTabHost.getContext(), tag); 

    TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() { 
     public View createTabContent(String tag) {return view;} 
    }); 
    mTabHost.addTab(setContent); 

} 

private static View createTabView(final Context context, final String text) { 
    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null); 
    TextView tv = (TextView) view.findViewById(R.id.tabsText); 
    tv.setText(text); 
    return view; 
} 
} 

回答

1

您通过使自己的文件,清单中宣布他们,并把他们的onCreate的setContentView(R.layout.activitylayout);把内容分成活动。如果内容是静态的,则在适当的布局中对其进行全面描述。

好的,理解。然后返回view这里公开View createTabContent(String tag) {return view;}您想要放置在那里的布局。一些listViewOfMyPictures或东西。

+0

嗨,thx的答案。我很了解如何将活动设置为内容。我浏览了android dev网站上的教程。但是这是一个自定义tabwidget,其代码与教程不同。你能否正确地告诉我如何在我的代码中为3种不同的活动编写“setContent”部分? – borislemke 2012-01-26 22:47:11

+0

我会返回视图在这里公共查看createTabContent(字符串标记){返回视图;}布局,你想放在那里。你还没试过吗? – Gangnus 2012-01-26 22:53:44

+0

这是不正确的。你应该说你看到了什么错误,它怎么不起作用。没有人会在这里做你的工作。要求这是非常不礼貌的。我们都有我们的工作要做。 – Gangnus 2012-01-27 09:30:26

0

请尝试查看TabHost.TabSpec的文档,特别是setContent(Intent intent)

创建意图,例如Intent tabIntentA = new Intent(this, SomeActivity.class);并将它们传递给setContent(...)方法。

编辑:

看那Tab Layout tutorial第6 - 特别是这个代码...

Intent intent; // Reusable Intent for each tab 

// Create an Intent to launch an Activity for the tab (to be reused) 
intent = new Intent().setClass(this, ArtistsActivity.class); 

// Initialize a TabSpec for each tab and add it to the TabHost 
spec = tabHost.newTabSpec("artists").setIndicator("Artists", 
        res.getDrawable(R.drawable.ic_tab_artists)) 
        .setContent(intent); 
tabHost.addTab(spec); 

你可能会被关闭之后该教程反正不是你发布的代码更好在你的问题。

+0

你可以看看我的代码,并告诉我在哪里我必须输入这些? – borislemke 2012-01-26 22:57:07

+0

不能得到它的工作。你可以请复制我的代码,粘贴你的代码并发布在这里?我会接受你的答案,如果它的工作.. -_- – borislemke 2012-01-26 23:44:11

+0

@borislemke:看我编辑的答案 – Squonk 2012-01-26 23:53:30

0

为特定活动创建一个意图,并通过“setContent”将意图添加到tabSpec,而不是您当前创建TabContentFactory的位置 - 如下所示(从Android开发人员指南中的Hello Tabwidget教程复制/粘贴):

Intent intent; // Reusable Intent for each tab 

// Create an Intent to launch an Activity for the tab (to be reused) 
intent = new Intent().setClass(this, ArtistsActivity.class); 

// Initialize a TabSpec for each tab and add it to the TabHost 
spec = tabHost.newTabSpec("artists").setIndicator("Artists", 
        res.getDrawable(R.drawable.ic_tab_artists)) 
       .setContent(intent); 
tabHost.addTab(spec); 
0

我也面临同样的问题,并通过修改下面给出的代码来解决它。

setupTab1(new TextView(this), "MEDIUM"); 
setupTab2(new TextView(this), "INTENSE"); 


private void setupTab1(final View view, final String tag) { 

View tabview = createTabView(mTabHost.getContext(), string); 

Intent intent1 = new Intent().setClass(this, DummyActivity1.class); 
TabSpec tab1 = mTabHost 
    .newTabSpec("TAB1") 
    .setIndicator(tabview) 
    .setContent(intent1); 

mTabHost.addTab(tab1); 

}

private void setupTab2(final View view, final String tag) { 
View tabview = createTabView(mTabHost.getContext(), string); 

Intent intent2 = new Intent().setClass(this, DummyActivity2.class); 
TabSpec tab2 = mTabHost 
    .newTabSpec("TAB2") 
    .setIndicator(tabview) 
    .setContent(intent2); 

mTabHost.addTab(tab2); 

}

之前,我们必须对所有TabView的setupTab()方法。现在我们有不同的setupTab()方法和不同的活动。

它为我工作..!希望这可以帮助你。

相关问题