在我的应用程序中,我想通过单击按钮动态添加选项卡。我该如何添加?其余的 gui 应该和它一样。应该只添加标签。这是xml文件-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/Beige"
android:focusable="true"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignLeft="@+id/linearLayout2"
android:layout_below="@+id/linearLayout2" >
<TabHost
android:id="@+id/th"
android:layout_width="match_parent"
android:layout_height="50dp" >
<TabWidget
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout android:id="@+id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="62px">
<Button android:id="@+id/buttontab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="A semi-random button"
/>
</FrameLayout>
</TabHost>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="match_parent"
android:layout_height="380dp"
android:layout_alignLeft="@+id/linearLayout3"
android:layout_below="@+id/linearLayout3"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<HorizontalScrollView
android:id="@+id/hv"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignLeft="@+id/linearLayout4"
android:layout_below="@+id/linearLayout4" >
<Button
android:id="@+id/addtab"
android:layout_width="80dp"
android:layout_height="50dp"
android:onClick="addtab"
android:text="@string/addtab"
android:textColor="@color/Black" />
</LinearLayout>
</RelativeLayout>
其余的滚动视图,每个选项卡的水平滚动视图应该相同。
点击按钮-
public void addtab(View v){
TabHost tabHost = new TabHost(this);
tabHost=(TabHost) findViewById(R.id.th);
TabWidget tabWidget = new TabWidget(this);
tabWidget=(TabWidget) findViewById(R.id.tabs);
tabHost.addView(tabWidget);
FrameLayout tabContent = new FrameLayout(this);
tabContent=(FrameLayout) findViewById(R.id.tabcontent);
tabContent.setPadding(0, 65, 0, 0);
tabHost.addView(tabContent);
TextView content = new TextView(this);
content.setText("This is the Frame Content");
content.setId(100);
tabHost.setup();
TabSpec tspec1 = tabHost.newTabSpec("Tab1");
tspec1.setIndicator("Button");
tspec1.setContent(new PreExistingViewFactory(content));
tabHost.addTab(tspec1);
}
class PreExistingViewFactory implements TabContentFactory{
private final View preExisting;
protected PreExistingViewFactory(View view){
preExisting = view;
}
public View createTabContent(String tag) {
return preExisting;
}
}
如何添加标签?请帮我
例外——
02-18 16:02:14.375: E/AndroidRuntime(737): java.lang.IllegalStateException: Could not execute method of the activity
02-18 16:02:14.375: E/AndroidRuntime(737): at android.view.View$1.onClick(View.java:2072)
02-18 16:02:14.375: E/AndroidRuntime(737): at android.view.View.performClick(View.java:2408)
02-18 16:02:14.375: E/AndroidRuntime(737): at android.view.View$PerformClick.run(View.java:8816)
02-18 16:02:14.375: E/AndroidRuntime(737): at android.os.Handler.handleCallback(Handler.java:587)
02-18 16:02:14.375: E/AndroidRuntime(737): at android.os.Handler.dispatchMessage(Handler.java:92)
02-18 16:02:14.375: E/AndroidRuntime(737): at android.os.Looper.loop(Looper.java:123)
02-18 16:02:14.375: E/AndroidRuntime(737): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-18 16:02:14.375: E/AndroidRuntime(737): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 16:02:14.375: E/AndroidRuntime(737): at java.lang.reflect.Method.invoke(Method.java:521)
02-18 16:02:14.375: E/AndroidRuntime(737): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-18 16:02:14.375: E/AndroidRuntime(737): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-18 16:02:14.375: E/AndroidRuntime(737): at dalvik.system.NativeStart.main(Native Method)
02-18 16:02:14.375: E/AndroidRuntime(737): Caused by: java.lang.reflect.InvocationTargetException
02-18 16:02:14.375: E/AndroidRuntime(737): at com.my.zproject.Work.addtab(Work.java:169)
02-18 16:02:14.375: E/AndroidRuntime(737): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 16:02:14.375: E/AndroidRuntime(737): at java.lang.reflect.Method.invoke(Method.java:521)
02-18 16:02:14.375: E/AndroidRuntime(737): at android.view.View$1.onClick(View.java:2067)
02-18 16:02:14.375: E/AndroidRuntime(737): ... 11 more
02-18 16:02:14.375: E/AndroidRuntime(737): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
02-18 16:02:14.375: E/AndroidRuntime(737): at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)