我正在尝试让 Activity 显示在选项卡中,但无法使其正常工作。我已经检查过活动是否单独显示,但是一旦我尝试将其放入选项卡中,我就会得到一个空选项卡。
这是选项卡屏幕及其 XML 布局文件:
public class TestTabs extends TabActivity{
private TabHost tabHost; // The tabhost for all the tabs
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.tabbed_screen);
tabHost = getTabHost();
initTabs();
}
protected void addTab(String tabName, Intent tabIntent){
TabSpec tabSpec = tabHost.newTabSpec(tabName);
tabSpec.setIndicator(tabName);
tabSpec.setContent(tabIntent);
tabHost.addTab(tabSpec);
}
protected void initTabs() {
addTab("1",new Intent(this,TestActivity.class));
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost">
<LinearLayout android:id="@+id/LinearLayout01"
android:orientation="vertical" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget android:id="@android:id/tabs" android:layout_height="fill_parent" android:layout_width="fill_parent"></TabWidget>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_height="fill_parent" android:layout_width="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
这是“孩子”活动:
public class TestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
// The super.onCreate() call will set the general frame also for this Screen:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
布局:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ellipsize="end"
android:maxLines="1"
android:singleLine="true"
android:text="Hello World"
android:textSize="12sp"
android:textStyle="bold" />
正如我所说,儿童活动在单独时显示正常,但是当我将它放在选项卡中时,我得到的选项卡没有任何内容。
我可能遗漏了一些明显的东西,但这是我第一次在 Android 中使用选项卡做任何事情。