试图动态生成标签和自己的观点 [英] Trying to generate tabs and their views dynamically

查看:191
本文介绍了试图动态生成标签和自己的观点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

编译错误得到解决,我发现有些事情我没有做正确的orgianlly基于关我下面的例子并纠正他们。然而,当我尝试运行code我碰到下面的错误。当我通过code线由我已经注意到,NewTab.setContent内createTabContent不执行,因为我在那里断点永远不会跳闸。

  10月1日至一十二日:11:21.285:E / AndroidRuntime(14528):致命异常:主要
十月1日至12日:11:21.285:E / AndroidRuntime(14528):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.jackman.universalav / com.jackman.universalav.MainActivity}:android.content.res.Resources $ NotFoundException:资源编号为0x0
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在android.app.ActivityThread.access $ 600(ActivityThread.java:140)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1227)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在android.os.Handler.dispatchMessage(Handler.java:99)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在android.os.Looper.loop(Looper.java:137)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在android.app.ActivityThread.main(ActivityThread.java:4898)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在java.lang.reflect.Method.invokeNative(本机方法)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在java.lang.reflect.Method.invoke(Method.java:511)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1008)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在dalvik.system.NativeStart.main(本机方法)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):$ android.content.res.Resources NotFoundException:由造成资源编号为0x0
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在android.content.res.Resources.getValue(Resources.java:1026)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在android.content.res.Resources.loadXmlResourceParser(Resources.java:2131)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在android.content.res.Resources.getLayout(Resources.java:865)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在android.view.LayoutInflater.inflate(LayoutInflater.java:394)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在android.widget.TabHost $ LabelIndicatorStrategy.createIndicatorView(TabHost.java:554)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在android.widget.TabHost.addTab(TabHost.java:234)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在com.jackman.universalav.MainActivity.onCreate(MainActivity.java:71)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在android.app.Activity.performCreate(Activity.java:5191)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
十月1日至12日:11:21.285:E / AndroidRuntime(14528):... 11个

更新code是:

 公共类MainActivity扩展活动
{
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        //创建一个包含包含制表符插件的选项卡主机
        TabHost选项卡=新TabHost(本);
        Tabs.setLayoutParams(新LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
        //创建标签控件容器
        TabWidget TabWidget =新TabWidget(本);
        TabWidget.setId(android.R.id.tabs);
        Tabs.addView(TabWidget,新LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
        //标签主机需要与每个可见选项卡关联的视图的框架布局
        的FrameLayout的FrameLayout =新的FrameLayout(本);
        frameLayout.setId(android.R.id.tabcontent);
        frameLayout.setPadding(0,65,0,0);
        Tabs.addView(的FrameLayout,新LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        Tabs.setup();
        //创建标签
        DatabaseHelper dbHelper =新DatabaseHelper(本);
        SQLiteDatabase分贝= dbHelper.getWritableDatabase();
        字符串的DBQuery =SELECT * FROM+ dbHelper.System_Table +AS我们携手+ dbHelper.Devices_Table +为D在S。 + dbHelper.Attribute_Device_ID +
            = d的。 + dbHelper.Attribute_Device_ID +;;
        光标C = db.rawQuery(的DBQuery,NULL);
        如果(c.getCount()大于0)
        {
            而(c.moveToNext())
            {
                  INT iColumnDeviceType = c.getColumnIndex(dbHelper.Attribute_Device_Type);
                  INT iColumnDeviceName = c.getColumnIndex(dbHelper.Attribute_Device_Name);
                  最后弦乐的devicetype = c.getString(iColumnDeviceType);
                  字符串设备名称= c.getString(iColumnDeviceName);
                  串DeviceNameLabel = DeviceName.replaceAll(,);
                  则tabspec NewTab = Tabs.newTabSpec(标签+ DeviceNameLabel);
                  NewTab.setIndicator(DeviceNameLabel);
                  NewTab.setContent(新TabHost.TabContentFactory()
                  {
                    公共查看createTabContent(字符串标签)
                    {
                        LL的LinearLayout =新的LinearLayout(MainActivity.this);
                        的LayoutParams PARAMS =新LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
                        ll.setLayoutParams(PARAMS);
                        ll.setOrientation(LinearLayout.VERTICAL);
                        如果(的devicetype ==接收器)
                        {                        }
                        否则,如果(的devicetype ==视频播放器)
                        {                        }
                        返回LL;
                     }
                   });
                   Tabs.addTab(NewTab);
             }
        }
    }
}

结束更新

低于原单的问题:

我还没有到该进程结束还没有的地步。

我试图按照code的样本从这个链接。 http://www.pocketmagic.net/2010/01/android-动态制表控制/

然而,当我试图创建正在创建的编译器怪胎对我的选项卡的LinearLayout。我不知道为什么它是引发错误。这就像我做了一个行前不存在的对象。

返回的错误是:

 在该行多个标记
- 令牌(S),错位结构(S)语法错误
- 令牌PARAMS语法错误,VariableDeclaratorId预计此令牌后

反正在 code 如下。任何人都可以看到我在做什么错了?

  //创建一个包含包含制表符插件的选项卡主机
TabHost选项卡=新TabHost(本);
Tabs.setLayoutParams(新LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
//创建标签控件容器
TabWidget TabWidget =新TabWidget(本);
TabWidget.setId(android.R.id.tabs);
Tabs.addView(TabWidget,新LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
Tabs.setup();
//创建标签
DatabaseHelper dbHelper =新DatabaseHelper(本);
SQLiteDatabase分贝= dbHelper.getWritableDatabase();
字符串的DBQuery =SELECT * FROM+ dbHelper.System_Table +AS我们携手+
    dbHelper.Devices_Table +为D在S。 + dbHelper.Attribute_Device_ID +
    = d的。 + dbHelper.Attribute_Device_ID +;;
光标C = db.rawQuery(的DBQuery,NULL);
如果(c.getCount()大于0)
{
    而(c.moveToNext())
    {
        INT iColumnDeviceType = c.getColumnIndex(dbHelper.Attribute_Device_Type);
        INT iColumnDeviceName = c.getColumnIndex(dbHelper.Attribute_Device_Name);
        最后弦乐的devicetype = c.getString(iColumnDeviceType);
        字符串设备名称= c.getString(iColumnDeviceName);
        串DeviceNameLabel = DeviceName.replaceAll(,);
        则tabspec NewTab = Tabs.newTabSpec(标签+ DeviceNameLabel);
        NewTab.setIndicator(设备名称);
        NewTab.setContent(新TabHost.TabContentFactory()
        {
            LL的LinearLayout =新的LinearLayout(MainActivity.this);
            的LayoutParams PARAMS =新LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
            ll.setLayoutParams(PARAMS); //此线有两个错误
            公共查看createTabContent(字符串标签)
            {
                    如果(的devicetype ==接收器)
                    {                    }
                    否则,如果(的devicetype ==视频播放器)
                    {                    }
                    返回null; //占位,直到我结束程序
                }
            });
            Tabs.addTab(NewTab);
        }
    }


解决方案

我想通了很多的试验和错误和谷歌搜索的例子广泛ammount的。后问题

首先,我放弃了试图创建的标签和窗口小部件通过Java的XML,而不是在该活动的框架布局。

在我的活动布局的xml文件我加了一个标签控制和XML视图删除标签离开XML code这样看。

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=MainActivity。>    < TabHost
    机器人:ID =@机器人:ID / tabhost
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_alignParentLeft =真
    机器人:layout_alignParentTop =真正的>        <的LinearLayout
        机器人:ID =@ + ID / MainLayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直>        < TabWidget
            机器人:ID =@机器人:ID /标签
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT>
        < / TabWidget>        <的FrameLayout
            机器人:ID =@机器人:ID / tabcontent
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent>
            < /&的FrameLayout GT;
        < / LinearLayout中>
    < / TabHost>
< / RelativeLayout的>

然后在活动Java文件我更新了常规的OnCreate是这样的。

  super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);//创建标签
TabHost标签=(TabHost)findViewById(android.R.id.tabhost);
Tabs.setup();
DatabaseHelper dbHelper =新DatabaseHelper(本);
SQLiteDatabase分贝= dbHelper.getWritableDatabase();
字符串的DBQuery =SELECT * FROM+ dbHelper.System_Table +AS我们携手+
    dbHelper.Devices_Table +为D在S。 + dbHelper.Attribute_Device_ID +
    = d的。 + dbHelper.Attribute_Device_ID +;;
光标C = db.rawQuery(的DBQuery,NULL);
诠释计数= 0;
如果(c.getCount()大于0)
{
    而(c.moveToNext())
    {
        计数=计数+ 1;
        INT iColumnDeviceType = c.getColumnIndex(dbHelper.Attribute_Device_Type);
        INT iColumnDeviceName = c.getColumnIndex(dbHelper.Attribute_Device_Name);
        最后弦乐的devicetype = c.getString(iColumnDeviceType);
        最后弦乐设备名称= c.getString(iColumnDeviceName);
        最后弦乐DeviceNameLabel = DeviceName.replaceAll(,);
        Log.d(创建标签,则tabspec:标签+ DeviceNameLabel);
        Log.d(创建标签,指标:+设备名称);
        最后则tabspec NewTab = Tabs.newTabSpec(标签+ DeviceNameLabel);
        NewTab.setIndicator(设备名称);
        NewTab.setContent(新TabHost.TabContentFactory()
        {
            @覆盖
            公共查看createTabContent(字符串标签)
            {
                LL的LinearLayout =新的LinearLayout(MainActivity.this);
                的LayoutParams PARAMS =新LinearLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
                ll.setLayoutParams(PARAMS);
                ll.setOrientation(LinearLayout.VERTICAL);
                如果(DeviceType.equals(接收))
                {
                    最终按钮btnVolumeUp =新按钮(MainActivity.this);
                    btnVolumeUp.setText(增加音量);
                    btnVolumeUp.setLayoutParams(新的LayoutParams(
                        LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                    btnVolumeUp.setGravity(Gravity.LEFT);
                    ll.addView(btnVolumeUp);
                }
                否则,如果(DeviceType.equals(视频播放器))
                {
                    最终按钮btnPlay =新按钮(MainActivity.this);
                    btnP​​lay.setText(播放);
                    btnP​​lay.setLayoutParams(新的LayoutParams(
                        LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                    btnP​​lay.setGravity(Gravity.LEFT);
                    ll.addView(btnPlay);
                }
                返回LL;
            }
        });        Tabs.addTab(NewTab);    }
}

Update:

The compile error is resolved and I found some things I didn't do right orgianlly based off the example I was following and corrected them. However when I try to run the code I get the following error. When I line by through the code I have noticed that the createTabContent inside the NewTab.setContent doesn't execute because my breakpoint in there is never tripped.

01-12 10:11:21.285: E/AndroidRuntime(14528): FATAL EXCEPTION: main
01-12 10:11:21.285: E/AndroidRuntime(14528): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jackman.universalav/com.jackman.universalav.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.app.ActivityThread.access$600(ActivityThread.java:140)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.os.Looper.loop(Looper.java:137)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.app.ActivityThread.main(ActivityThread.java:4898)
01-12 10:11:21.285: E/AndroidRuntime(14528): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 10:11:21.285: E/AndroidRuntime(14528): at java.lang.reflect.Method.invoke(Method.java:511)
01-12 10:11:21.285: E/AndroidRuntime(14528): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
01-12 10:11:21.285: E/AndroidRuntime(14528): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
01-12 10:11:21.285: E/AndroidRuntime(14528): at dalvik.system.NativeStart.main(Native Method)
01-12 10:11:21.285: E/AndroidRuntime(14528): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.content.res.Resources.getValue(Resources.java:1026)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2131)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.content.res.Resources.getLayout(Resources.java:865)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.widget.TabHost$LabelIndicatorStrategy.createIndicatorView(TabHost.java:554)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.widget.TabHost.addTab(TabHost.java:234)
01-12 10:11:21.285: E/AndroidRuntime(14528): at com.jackman.universalav.MainActivity.onCreate(MainActivity.java:71)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.app.Activity.performCreate(Activity.java:5191)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
01-12 10:11:21.285: E/AndroidRuntime(14528): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
01-12 10:11:21.285: E/AndroidRuntime(14528): ... 11 more

The updated code is:

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Create the tab host that contains the tab widget that contains the tabs
        TabHost Tabs = new TabHost(this);
        Tabs.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        //Create the tab widget  container
        TabWidget TabWidget = new TabWidget(this);
        TabWidget.setId(android.R.id.tabs);
        Tabs.addView(TabWidget, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        //the tab host needs a frame layout for the views associated with each visible tab
        FrameLayout frameLayout = new FrameLayout(this);
        frameLayout.setId(android.R.id.tabcontent);
        frameLayout.setPadding(0, 65, 0, 0);
        Tabs.addView(frameLayout, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        Tabs.setup();
        //Create the tabs
        DatabaseHelper dbHelper = new DatabaseHelper(this);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String dbQuery = "SELECT * FROM " + dbHelper.System_Table + " AS s JOIN " + dbHelper.Devices_Table + " AS d ON s." + dbHelper.Attribute_Device_ID +
            " = d." + dbHelper.Attribute_Device_ID + ";";
        Cursor c = db.rawQuery(dbQuery, null);
        if (c.getCount() > 0)
        {
            while (c.moveToNext())
            {
                  int iColumnDeviceType = c.getColumnIndex(dbHelper.Attribute_Device_Type);
                  int iColumnDeviceName = c.getColumnIndex(dbHelper.Attribute_Device_Name);
                  final String DeviceType = c.getString(iColumnDeviceType);
                  String DeviceName = c.getString(iColumnDeviceName);
                  String DeviceNameLabel = DeviceName.replaceAll(" ", "");
                  TabSpec NewTab = Tabs.newTabSpec("tab" + DeviceNameLabel);
                  NewTab.setIndicator(DeviceNameLabel);
                  NewTab.setContent(new TabHost.TabContentFactory()
                  {
                    public View createTabContent(String tag)
                    {
                        LinearLayout ll = new LinearLayout(MainActivity.this);
                        LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                        ll.setLayoutParams(params);
                        ll.setOrientation(LinearLayout.VERTICAL);
                        if (DeviceType == "Receiver")
                        {

                        }
                        else if (DeviceType == "Video Player")
                        {

                        }
                        return ll;
                     }
                   });
                   Tabs.addTab(NewTab);
             }
        }
    }
}

End Update

Orginal question below:

I haven't got to the point where this routine is finished yet.

I was trying to follow a sample of code from this link. http://www.pocketmagic.net/2010/01/android-dynamic-tab-control/

However when I attempt to create a linearlayout for the tab being created the compiler freaks out on me. I'm not sure why it is throwing the error. It's like the object I made one line ago doesn't exist.

The returned errors are:

Multiple markers at this line
- Syntax error on token(s), misplaced construct(s)
- Syntax error on token "params", VariableDeclaratorId expected after this token

Anyways the code is below. Can anyone see what I am doing wrong?

//Create the tab host that contains the tab widget that contains the tabs
TabHost Tabs = new TabHost(this);
Tabs.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//Create the tab widget  container
TabWidget TabWidget = new TabWidget(this);
TabWidget.setId(android.R.id.tabs);
Tabs.addView(TabWidget, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Tabs.setup();
//Create the tabs
DatabaseHelper dbHelper = new DatabaseHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
String dbQuery = "SELECT * FROM " + dbHelper.System_Table + " AS s JOIN " +
    dbHelper.Devices_Table + " AS d ON s." + dbHelper.Attribute_Device_ID +
    " = d." + dbHelper.Attribute_Device_ID + ";";
Cursor c = db.rawQuery(dbQuery, null);
if (c.getCount() > 0)
{
    while (c.moveToNext())
    {
        int iColumnDeviceType = c.getColumnIndex(dbHelper.Attribute_Device_Type);
        int iColumnDeviceName = c.getColumnIndex(dbHelper.Attribute_Device_Name);
        final String DeviceType = c.getString(iColumnDeviceType);
        String DeviceName = c.getString(iColumnDeviceName);
        String DeviceNameLabel = DeviceName.replaceAll(" ", "");
        TabSpec NewTab = Tabs.newTabSpec("tab" + DeviceNameLabel);
        NewTab.setIndicator(DeviceName);
        NewTab.setContent(new TabHost.TabContentFactory()
        {
            LinearLayout ll = new LinearLayout(MainActivity.this);
            LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            ll.setLayoutParams(params); //this line has both of the errors
            public View createTabContent(String tag)
            {
                    if (DeviceType == "Receiver")
                    {

                    }
                    else if (DeviceType == "Video Player")
                    {

                    }
                    return null; //place holder until I finish the routine
                }
            });
            Tabs.addTab(NewTab);
        }
    }

解决方案

I figured out the issue after much trial and error and googling an extensive ammount of examples.

First I gave up attempting to create the tab widget and the frame layout through java instead of xml for the activity..

In my activities layout xml file I added a tab control and in the xml view deleted the tabs leaving the xml code looking like this.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

    <TabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

        <LinearLayout
        android:id="@+id/MainLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >


            </FrameLayout>
        </LinearLayout>
    </TabHost>
</RelativeLayout>

Then in the java file for the activity I updated the oncreate routine to be this.

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Create the tabs
TabHost Tabs = (TabHost) findViewById(android.R.id.tabhost);
Tabs.setup();
DatabaseHelper dbHelper = new DatabaseHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
String dbQuery = "SELECT * FROM " + dbHelper.System_Table + " AS s JOIN " +
    dbHelper.Devices_Table + " AS d ON s." + dbHelper.Attribute_Device_ID +
    " = d." + dbHelper.Attribute_Device_ID + ";";
Cursor c = db.rawQuery(dbQuery, null);
int Count = 0;
if (c.getCount() > 0)
{
    while (c.moveToNext())
    {
        Count = Count + 1;
        int iColumnDeviceType = c.getColumnIndex(dbHelper.Attribute_Device_Type);
        int iColumnDeviceName = c.getColumnIndex(dbHelper.Attribute_Device_Name);
        final String DeviceType = c.getString(iColumnDeviceType);
        final String DeviceName = c.getString(iColumnDeviceName);
        final String DeviceNameLabel = DeviceName.replaceAll(" ", "");
        Log.d("Creating Tab", "TabSpec: tab" + DeviceNameLabel);
        Log.d("Creating Tab", "Indicator: " + DeviceName);
        final TabSpec NewTab = Tabs.newTabSpec("tab" + DeviceNameLabel);
        NewTab.setIndicator(DeviceName);
        NewTab.setContent(new TabHost.TabContentFactory()
        {
            @Override
            public View createTabContent(String tag)
            {
                LinearLayout ll = new LinearLayout(MainActivity.this);
                LayoutParams params = new LinearLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                ll.setLayoutParams(params);
                ll.setOrientation(LinearLayout.VERTICAL);
                if (DeviceType.equals("Receiver"))
                {
                    final Button btnVolumeUp = new Button(MainActivity.this);
                    btnVolumeUp.setText("Volume Up");
                    btnVolumeUp.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    btnVolumeUp.setGravity(Gravity.LEFT);
                    ll.addView(btnVolumeUp);
                }
                else if (DeviceType.equals("Video Player"))
                {
                    final Button btnPlay = new Button(MainActivity.this);
                    btnPlay.setText("Play");
                    btnPlay.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                    btnPlay.setGravity(Gravity.LEFT);
                    ll.addView(btnPlay);
                }
                return ll;
            }
        });

        Tabs.addTab(NewTab);

    }
}

这篇关于试图动态生成标签和自己的观点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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