应用选项卡的意见,而不是单独的活动
我有麻烦试图找出如何将我当前的选项卡设置转换为一个使用视图,而不是单独的活动......我有调用我的搜索功能的问题,我认为它是由于我创建我的标签的方式。应用选项卡的意见,而不是单独的活动
我主要的发射活动是public class Menu extends TabActivity
它创建的标签
intent = new Intent().setClass(this, TabGroup1.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("codes").setIndicator("All Codes",res.getDrawable(R.drawable.ic_tab_codes))
.setContent(intent);
tabHost.addTab(spec);
`TabGroup1' 做的每个选项卡
public class TabGroup1 extends TabGroupActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startChildActivity("Category", new Intent(this,Category.class));
}
}
然后调用ListActivity
这下面显示了有内容时的项目被点击,另一个意图被创建,然后开始一个新的活动,这使得我可以在用户沿着列表进入时在每个级别上都有标签。
这与下面的代码
public void onListItemClick(ListView parent, View view, int position, long id) {
Intent intent = new Intent(getParent(), SubCategory.class);
Cursor cursor = (Cursor) adapter.getItem(position);
intent.putExtra("CATEGORY", cursor.getString(cursor.getColumnIndex("_id")));
/*startActivity(intent);*/
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("SubCategory", intent);
}
TabGroupActivity
做的是一类我从tutorial,可以让你有相同的标签布局下的多个活动中。
我在努力的是将我必须使用的视图转换为使用setContent来更改视图。
我找到了this的例子,但它没有提供足够的细节让我继续。 还发现this一个,以及...
可有人请给我办下来我需要什么改变,也我怎么使用我listactivities setContent ...
在此先感谢
几件事... setContent定义了内容,它不会导致切换选项卡。如果要强制更改某个选项卡,请使用TabHost.setCurrentTab(tabid);
,否则默认为第一个选项卡,然后用户选择任何选项卡。
来自我自己的项目的一个例子(这个项目实际上有四个选项卡,但我已经删除了一些尝试并保持这一点)。有几种方法可以做到这一点,但我发现最简单的方法是创建一个方法来填充每个选项卡,这样我可以根据需要刷新选项卡,方法是调用适当的方法来选择我需要的选项卡(下面的所有Java都包含在内在TabActivity中)。
setupdetailmain.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/setupheader"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:textSize="15dp" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:gravity="bottom" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<!-- General Info Tab -->
<LinearLayout
android:id="@+id/note_tab"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<!-- Tool Tab -->
<LinearLayout
android:id="@+id/offset_tab"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/list2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
标签设置代码(延伸TabActivity)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setupdetailmain);
// ***************************************************************
// Set up the tabs in the tabhost
// ***************************************************************
tabHost = getTabHost();
TabHost.TabSpec spec;
spec = tabHost.newTabSpec("Offsets").setIndicator("Offsets")
.setContent(R.id.offset_tab);
tabHost.addTab(spec);
spec = tabHost.newTabSpec("Notes").setIndicator("Notes")
.setContent(R.id.note_tab);
tabHost.addTab(spec);
populateTabs(StartTab);
}
标签填充方法
// ***************************************************************
// Fill the Notes tab
// ***************************************************************
private void notestab() {
notes = (LinearLayout) findViewById(R.id.note_tab);
notestxt = new TextView(this);
notestxt.setText(SETUPINFO_NOTES);
notestxt.setTextSize(15);
notes.addView(notestxt);
}
// ***************************************************************
// Fill the Offset tab
// ***************************************************************
private void offsettab() {
wpccount = 0;
for (int i = 0; i < 20; i++) {
if (wpcdesc[i] != null) {
wpcdisplayhold[wpccount] = wpcid[i] + " - " + wpcdesc[i];
wpcidhold[wpccount] = wpcid[i];
wpcdeschold[wpccount] = wpcdesc[i];
wpccount++;
}
}
wpcdisplay = new String[wpccount];
for (int i = 0; i < wpccount; i++) {
wpcdisplay[i] = wpcdisplayhold[i];
}
mWPCView = (ListView) findViewById(R.id.list2);
mWPCView.setAdapter(new ColorizingListAdapter(SetupDisplay.this,
wpcdisplay, "Offset"));
registerForContextMenu(mWPCView);
}
这使用了一个自定义适配器,但希望它能够实现如何在选项卡视图中设置列表而不必执行新活动的想法。
我很困惑...你每次改变标签页时都会创建新的标签页? – Barak 2012-03-30 22:29:42
嗨@Barak我已经编辑了我的问题,上面有一些进一步的代码,说明每个活动如何调用下一个代码,以便选项卡位于顶端......它可以工作,但是我想从此转换,因为我已经读取它不是是最好的方法,并且ActivityGroup已被弃用。这[教程](http://ericharlow.blogspot.ca/2010/09/experience-multiple-android-activities.html)是我用来创建我的标签的人。 – DeucePie 2012-03-30 22:42:38