在Android的动态添加标签 [英] adding tabs dynamically in android

查看:479
本文介绍了在Android的动态添加标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我想添加的标签动态button.how的OB点击我可以补充的吗?
剩下的GUI建议立即进行删除是相同的,因为它is.only标签应予以补充。
这里是xml文件 -

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:背景=@色/米色
机器人:可聚焦=真
机器人:方向=垂直>
    <的LinearLayout
    机器人:ID =@ + ID / linearLayout3
    机器人:layout_width =match_parent
    机器人:layout_height =50dp
    机器人:layout_alignLeft =@ + ID / linearLayout2
    机器人:layout_below =@ + ID / linearLayout2>
    < TabHost
        机器人:ID =@ + ID /日
        机器人:layout_width =match_parent
        机器人:layout_height =50dp>
        < TabWidget
            机器人:ID =@ + ID /标签
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT/>         <的FrameLayout机器人:ID =@ + ID / tabcontent
          机器人:layout_width =FILL_PARENT
          机器人:layout_height =FILL_PARENT
            机器人:paddingTop =62px>
       <按钮机器人:ID =@ + ID / buttontab
       机器人:layout_width =FILL_PARENT
       机器人:layout_height =FILL_PARENT
         机器人:文字=一个半随机按钮
        />
       < /&的FrameLayout GT;
    < / TabHost>
< / LinearLayout中>
<的LinearLayout
    机器人:ID =@ + ID / linearLayout4
    机器人:layout_width =match_parent
    机器人:layout_height =380dp
    机器人:layout_alignLeft =@ + ID / linearLayout3
    机器人:layout_below =@ + ID / linearLayout3
    机器人:方向=垂直>
    <滚动型
        机器人:ID =@ + ID / scrollView1
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent>
        < Horizo​​ntalScrollView
            机器人:ID =@ + ID / HV
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT>
            < ImageView的
                机器人:ID =@ + ID / IMV
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT/>
        < / Horizo​​ntalScrollView>
    < /滚动型>
< / LinearLayout中>
 <的LinearLayout
    机器人:layout_width =match_parent
    机器人:layout_height =60dp
    机器人:layout_alignLeft =@ + ID / linearLayout4
    机器人:layout_below =@ + ID / linearLayout4>
    <按钮
        机器人:ID =@ + ID / addtab
        机器人:layout_width =80dp
        机器人:layout_height =50dp
        安卓的onClick =addtab
        机器人:文字=@字符串/ addtab
        机器人:文字颜色=@彩/黑/>
  < / LinearLayout中>
  < / RelativeLayout的>

剩下的滚动视图,滚动视图水平应该是相同的每个选项卡。

按钮的点击 -

 公共无效addtab(视图v){    TabHost tabHost =新TabHost(本);
    tabHost =(TabHost)findViewById(R.id.th);    TabWidget tabWidget =新TabWidget(本);
    tabWidget =(TabWidget)findViewById(R.id.tabs);
    tabHost.addView(tabWidget);
    的FrameLayout tabContent =新的FrameLayout(本);
    tabContent =(的FrameLayout)findViewById(R.id.tabcontent);
    tabContent.setPadding(0,65,0,0);
    tabHost.addView(tabContent);
    TextView的内容=新的TextView(本);
    content.setText(这是框架内容);
    content.setId(100);
    tabHost.setup();
    则tabspec tspec1 = tabHost.newTabSpec(TAB1);
    tspec1.setIndicator(按钮);
    tspec1.setContent(新preExistingViewFactory(内容));
    tabHost.addTab(tspec1);    }类preExistingViewFactory实现TabContentFactory {
    私人最终查看preExisting;
    保护preExistingViewFactory(查看视图){
    preExisting =视图;
    }
    公共查看createTabContent(字符串标签){
    返回preExisting;
    }
    }

如何添加标签?
请大家帮我

例外 -

  02-18 16:02:14.375:E / AndroidRuntime(737):java.lang.IllegalStateException:无法执行活动的方法
02-18 16:02:14.375:E / AndroidRuntime(737):在android.view.View $ 1.onClick(View.java:2072)
02-18 16:02:14.375:E / AndroidRuntime(737):在android.view.View.performClick(View.java:2408)
02-18 16:02:14.375:E / AndroidRuntime(737):在android.view.View $ PerformClick.run(View.java:8816)
02-18 16:02:14.375:E / AndroidRuntime(737):在android.os.Handler.handleCallback(Handler.java:587)
02-18 16:02:14.375:E / AndroidRuntime(737):在android.os.Handler.dispatchMessage(Handler.java:92)
02-18 16:02:14.375:E / AndroidRuntime(737):在android.os.Looper.loop(Looper.java:123)
02-18 16:02:14.375:E / AndroidRuntime(737):在android.app.ActivityThread.main(ActivityThread.java:4627)
02-18 16:02:14.375:E / AndroidRuntime(737):在java.lang.reflect.Method.invokeNative(本机方法)
02-18 16:02:14.375:E / AndroidRuntime(737):在java.lang.reflect.Method.invoke(Method.java:521)
02-18 16:02:14.375:E / AndroidRuntime(737):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
 02-18 16:02:14.375:E / AndroidRuntime(737):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-18 16:02:14.375:E / AndroidRuntime(737):在dalvik.system.NativeStart.main(本机方法)
02-18 16:02:14.375:E / AndroidRuntime(737):由:java.lang.reflect.InvocationTargetException
02-18 16:02:14.375:E / AndroidRuntime(737):在com.my.zproject.Work.addtab(Work.java:169)
02-18 16:02:14.375:E / AndroidRuntime(737):在java.lang.reflect.Method.invokeNative(本机方法)
02-18 16:02:14.375:E / AndroidRuntime(737):在java.lang.reflect.Method.invoke(Method.java:521)
02-18 16:02:14.375:E / AndroidRuntime(737):在android.view.View $ 1.onClick(View.java:2067)
02-18 16:02:14.375:E / AndroidRuntime(737):11 ...更多
02-18 16:02:14.375:E / AndroidRuntime(737):java.lang.IllegalStateException:产生的原因指定的小孩已经有一个父。您必须先对孩子的父母打电话removeView()。
02-18 16:02:14.375:E / AndroidRuntime(737):在android.view.ViewGroup.addViewInner(ViewGroup.java:1970)


解决方案

覆盖getPagetitle方法FragmentPagerAdapter作为,

  @覆盖
公共CharSequence的getPageTitle(INT位置){
    返回myFragmentTitleList.get(位置);
}

在哪里myFragmentTitleList是你的头衔的ArrayList

in my application i want to add tabs dynamically ob click of button.how can i add? the rest gui shoul be same as it is.only tabs should be added. here is xml file-

<?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>

the rest scrollview ,horizontal scrollview should be same for each tab.

on click of button-

    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;
    }
    }

how can add tabs?? please help me

exceptions-

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)

解决方案

override getPagetitle method in FragmentPagerAdapter as,

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

where myFragmentTitleList is your arraylist of titles.

这篇关于在Android的动态添加标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆