我在互联网上找到了这个自定义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;
}
}
答案 0 :(得分:1)
您可以通过创建自己的文件,在清单中声明内容并将其加入setContentView(R.layout.activitylayout);
来将内容放入活动中。如果内容是静态的,则以适当的布局完整地描述。
好的,明白了。然后返回view
这里公开View createTabContent(String tag) {return view;}
那个你要放在那里的布局。一些listViewOfMyPictures或其他东西。
答案 1 :(得分: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);
无论如何,您可能会更好地关注该教程,而不是您在问题中发布的代码。
答案 2 :(得分:0)
为特定的Activity创建一个intent,并通过“setContent”将intent添加到tabSpec,而不是当前创建TabContentFactory的位置 - 就像这样(从Android Dev中的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);
答案 3 :(得分: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()方法,具有不同的活动。
它对我有用..!希望这可以帮到你。