我通过代码定义时,我的标签内容不会显示

时间:2014-01-19 06:54:24

标签: android tabs

我有一个标签布局,我已按代码实现了标签。这是布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TabHost
        android:id="@+id/themesTabHost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

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

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

        </LinearLayout>

    </TabHost>

</LinearLayout>

我已使用以下代码设置标签:

private void setupTab()
{
    themesHost = (TabHost)findViewById(R.id.themesTabHost);
    themesHost.setup();

    final LinearLayout.LayoutParams scrollViewLayoutParams =
            new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);

    final TableLayout.LayoutParams tableLayoutParams = 
            new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT);

    final TableRow.LayoutParams tableRowLayoutParams =
            new TableRow.LayoutParams(
                    TableRow.LayoutParams.WRAP_CONTENT);

    final LinearLayout.LayoutParams imageViewLayoutParams =
            new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    1f);

    imageViewLayoutParams.setMargins(20, 20, 20, 20);


    final TabSpec featuredSpec = themesHost.newTabSpec("tab_featured");
    featuredSpec.setIndicator("Featured", getResources().getDrawable(android.R.drawable.ic_menu_view));
    featuredSpec.setContent(new TabContentFactory() {

        @Override
        public View createTabContent(String tag) {

            ScrollView scrollView = new ScrollView(themesHost.getContext());

            scrollView.setLayoutParams(scrollViewLayoutParams);

            TableLayout tableLayout = new TableLayout(themesHost.getContext());

            tableLayout.setLayoutParams(tableLayoutParams);

            TableRow[] tableRows = new TableRow[3];

            ImageView[] imageViews = new ImageView[9];

            for (int i = 0; i < imageViews.length; i++)
            {
                imageViews[i] = new ImageView(themesHost.getContext());
                imageViews[i].setLayoutParams(imageViewLayoutParams);
                imageViews[i].setImageResource(R.drawable.theme_blank);
            }

            for (int i = 0; i < tableRows.length; i++)
            {
                for (int j = i; j < tableRows.length + i; j++)
                {
                    tableRows[i] = new TableRow(themesHost.getContext());
                    tableRows[i].setLayoutParams(tableRowLayoutParams);
                    tableRows[i].addView(imageViews[2 * i + j]);
                }

                tableLayout.addView(tableRows[i]);
            }

            scrollView.addView(tableLayout);

            return scrollView;
        }
    });

    final TabSpec installedSpec = themesHost.newTabSpec("tab_installed");
    installedSpec.setIndicator("Installed", getResources().getDrawable(android.R.drawable.ic_menu_view));
    installedSpec.setContent(new TabContentFactory() {

        @Override
        public View createTabContent(String tag) {

            ScrollView scrollView = new ScrollView(themesHost.getContext());

            scrollView.setLayoutParams(scrollViewLayoutParams);

            TableLayout tableLayout = new TableLayout(themesHost.getContext());

            tableLayout.setLayoutParams(tableLayoutParams);

            TableRow[] tableRows = new TableRow[3];

            ImageView[] imageViews = new ImageView[9];

            for (int i = 0; i < imageViews.length; i++)
            {
                imageViews[i] = new ImageView(themesHost.getContext());
                imageViews[i].setLayoutParams(imageViewLayoutParams);
                imageViews[i].setImageResource(R.drawable.theme_blank);
            }

            for (int i = 0; i < tableRows.length; i++)
            {
                for (int j = i; j < tableRows.length + i; j++)
                {
                    tableRows[i] = new TableRow(themesHost.getContext());
                    tableRows[i].setLayoutParams(tableRowLayoutParams);
                    tableRows[i].addView(imageViews[2 * i + j]);
                }

                tableLayout.addView(tableRows[i]);
            }

            scrollView.addView(tableLayout);

            return scrollView;

        }
    });

    themesHost.addTab(featuredSpec);
    themesHost.addTab(installedSpec);

}

虽然一切正常,但在运行应用程序时,我的标签内容不会显示。有趣的是,当我为内部createTabContent()方法设置类似文本视图的视图时,它会显示内容。 有什么想法吗?

0 个答案:

没有答案