Android tabHost是不可见的?为什么不用这个XML显示?

时间:2012-04-27 08:37:08

标签: android xml tabs android-tabhost tabwidget

在Eclipse上的XML编辑器中,我看到了tabhost,它看起来就像我想要的那样,但是当我运行它时,我根本看不到tabHost,我怎样才能确保它是可见的?

这是我的XML。再次,编辑器显示它,但是当我运行它时它没有,膨胀它的活动还没有做太多,只需更改两个文本字段。有什么想法吗?

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/name"
        android:layout_width="238dp"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:layout_gravity="top|left"
        android:layout_marginTop="7dp"
        android:layout_marginLeft="3dp"
        android:layout_row="0"
        android:gravity="top"
        android:maxLines="1"
        android:padding="3dp"
        android:text="Motherfucking Acer Laptop X314134"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <View
        android:layout_height="2dp"
        android:layout_width="fill_parent"
        android:layout_row="1"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:background="#FF000000"
        />

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:layout_gravity="center_horizontal"
        android:layout_row="2" >

        <LinearLayout
            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" >

                <LinearLayout
                    android:id="@+id/Info"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/Specs"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/Fotos"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/Prijzen"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

    <TextView
        android:id="@+id/score"
        android:layout_column="1"
        android:layout_marginRight="3dp"
        android:layout_marginTop="4dp"
        android:layout_gravity="right"
        android:layout_row="0"
        android:gravity="center_vertical"
        android:padding="3dp"
        android:text="100%"
        android:textSize="24sp" />

    <Space
        android:id="@+id/space2"
        android:layout_width="1dp"
        android:layout_height="40dp"
        android:layout_column="0"
        android:layout_gravity="left"
        android:layout_row="0" />

    <Space
        android:id="@+id/space1"
        android:layout_width="1dp"
        android:layout_height="415dp"
        android:layout_column="0"
        android:layout_gravity="left"
        android:layout_row="2" />

</GridLayout>

2 个答案:

答案 0 :(得分:7)

您必须以编程方式执行此操作,例如:

 tabhost = (TabHost) findViewById(android.R.id.tabhost);
    tabhost.setup();
    TabSpec ts = tabhost.newTabSpec("tag1"); 
    ts.setContent(R.id.tab1);
    ts.setIndicator("First Tab");
    tabhost.addTab(ts);

    ts = tabhost.newTabSpec("tag2"); 
    ts.setContent(R.id.tab2);
    ts.setIndicator("Second Tab");  
    tabhost.addTab(ts);
    ts= tabhost.newTabSpec("tag3");
    ts.setContent(R.id.tab3);
    ts.setIndicator("Third Tab");
    tabhost.addTab(ts);

答案 1 :(得分:-2)

有关详情,请参阅此页面:

http://mobileorchard.com/android-app-development-tabbed-activities/

有一个很好的例子,你可以复制粘贴,在那里他们用XML设置标签并用Java充气,所有这些都只有一个Java活动。 (虽然你想将他们的xml文件的“px”单位改为“dp”,以使它在所有手机上看起来都很好。)