0
我试图动态地创建和删除选项卡。通常应该为TabSpec中创建的每个选项卡设置活动。但是如何在动态创建标签时执行此操作?这里我使用框架布局来显示标签内容。如果我尝试通过设置标签内容来使用相同的活动,则文本会变得重叠。在这里,我必须从EditText视图中读取文本,并将其设置为选项卡内容,并且每当导航到该选项卡时都应显示内容。如何为动态创建的每个选项卡创建活动?
我试图动态地创建和删除选项卡。通常应该为TabSpec中创建的每个选项卡设置活动。但是如何在动态创建标签时执行此操作?这里我使用框架布局来显示标签内容。如果我尝试通过设置标签内容来使用相同的活动,则文本会变得重叠。在这里,我必须从EditText视图中读取文本,并将其设置为选项卡内容,并且每当导航到该选项卡时都应显示内容。如何为动态创建的每个选项卡创建活动?
试试这个
protected TabHost tabs;
// ...
/**
* Init tabs.
*/
private void initTabs() {
tabs = (TabHost) findViewById(R.id.tabhost);
tabs.setup();
tabs.setBackgroundResource(R.drawable.bg_midgray);
TabHost.TabSpec spec;
// Location info
txtTabInfo = new TextView(this);
txtTabInfo.setText("INFO");
txtTabInfo.setPadding(0, 0, 0, 0);
txtTabInfo.setTextSize(14);
txtTabInfo.setBackgroundResource(R.drawable.bg_tab_left_inactive_right_inactive);
txtTabInfo.setTextColor(Color.DKGRAY);
txtTabInfo.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP);
txtTabInfo.setHeight(39);
spec = tabs.newTabSpec("tabInfo");
spec.setContent(R.id.tabInfo);
spec.setIndicator(txtTabInfo);
tabs.addTab(spec);
// Maps
txtTabMap = new TextView(this);
txtTabMap.setText("MAP");
txtTabMap.setTextSize(14);
txtTabMap.setPadding(0, 0, 0, 0);
txtTabMap.setBackgroundResource(R.drawable.bg_tab_middle_inactive_right_active);
txtTabMap.setTextColor(Color.DKGRAY);
txtTabMap.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP);
txtTabMap.setHeight(39);
spec = tabs.newTabSpec("tabMap");
spec.setContent(R.id.tabMap);
spec.setIndicator(txtTabMap);
tabs.addTab(spec);
tabs.setCurrentTab(0);
tabs.setOnTabChangedListener(this);
}
// ...
“平时的活动应在则tabspec创建的每个选项卡中设置。”不,通常应该为在TabSpec中创建的每个选项卡设置一个视图。 *可以*使用活动作为标签内容,但我不推荐它,因为它增加了没有价值的开销。 – CommonsWare 2010-03-23 14:24:34
如何为所有标签页使用相同的活动。我尝试过,但数据重叠。即使我们想要处理巨大的标签页内容,它也会让我头脑发热或麻烦。 – Kantesh 2010-03-24 04:38:07
@Kantesh现在使用片段:http://www.androidbegin.com/tutorial/implementing-fragment-tabs-in-android/ – 2014-02-17 03:25:19