2013-02-18 100 views
3

動態button.how的OB點擊我可以補充的嗎? 其餘gui應該是相同的,只需添加標籤。 這裏是一個XML文件 -添加標籤動態地在我的應用我想添加標籤的Android

<?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) 
+0

你在Work.java的169線有哪些? (應該在com.my.zproject.Work.addtab方法中) – DigCamara 2013-03-22 17:45:02

回答

0

覆蓋getPagetitle方法FragmentPagerAdapter如,

@Override 
public CharSequence getPageTitle(int position) { 
    return myFragmentTitleList.get(position); 
} 

其中myFragmentTitleList是你的標題數組列表。