Android 如何为动态创建的每个选项卡创建活动?
我正在尝试动态创建和删除选项卡。通常,应该为在TabSpec中创建的每个选项卡设置一个活动。但是,当选项卡是动态创建的时,如何做到这一点呢?在这里,我使用框架布局来显示选项卡内容。如果我试图通过设置选项卡内容来使用相同的活动,则文本会重叠。在这里,我必须从EditText视图中读取文本,并将其设置为选项卡内容,每当我导航到该选项卡时,该内容都应显示出来。试试这个Android 如何为动态创建的每个选项卡创建活动?,android,tabwidget,Android,Tabwidget,我正在尝试动态创建和删除选项卡。通常,应该为在TabSpec中创建的每个选项卡设置一个活动。但是,当选项卡是动态创建的时,如何做到这一点呢?在这里,我使用框架布局来显示选项卡内容。如果我试图通过设置选项卡内容来使用相同的活动,则文本会重叠。在这里,我必须从EditText视图中读取文本,并将其设置为选项卡内容,每当我导航到该选项卡时,该内容都应显示出来。试试这个 protected TabHost tabs; // ... /** * Init tabs. */ private void
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);
}
// ...
试试这个
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中创建的每个选项卡设置一个视图。可以将活动用作选项卡内容,但我不建议这样做,因为这会增加开销,没有任何价值。如何对所有选项卡使用相同的活动。我尝试过,但数据会重叠。即使我们要处理大量选项卡内容,我想也会有些过头或麻烦。@Kantesh现在使用片段:“通常应为在TabSpec中创建的每个选项卡设置一个活动。否,通常应为在TabSpec中创建的每个选项卡设置一个视图。可以将活动用作选项卡内容,但我不建议这样做,因为这会增加开销,没有任何价值。如何对所有选项卡使用相同的活动。我尝试过,但数据会重叠。即使我们要处理大量选项卡内容,我想也会有些过头或麻烦。@Kantesh现在使用片段: