0

我是 android 编程新手,需要高级用户来检查我的工作。出于某种原因,当我运行此应用程序时,我得到一个空白屏幕,而不是我想要的两个选项卡。非常感谢!

MainActivity.java

package com.example.storeitemfinder3;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
        tabHost.setup();

        TabSpec spec1=tabHost.newTabSpec("Tab 1");
        spec1.setContent(R.layout.tab1);
        spec1.setIndicator("Tab 1");

        TabSpec spec2=tabHost.newTabSpec("Tab 2");
        spec2.setIndicator("Tab 2");
        spec2.setContent(R.layout.tab2);

        tabHost.addTab(spec1);
        tabHost.addTab(spec2);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabHost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

    <include layout="@layout/tab1" />

    <include layout="@layout/tab2" />

</TabHost>

tab1.xml

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

    <CheckBox
        android:id="@+id/Avocados"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Avocados" />

    <CheckBox
        android:id="@+id/Cashew_Nuts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cashew_Nuts"

        />
 ////android:checked="true"

</LinearLayout>

tab2.xml

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.81"
        android:src="@drawable/tjs" />

</LinearLayout>
4

2 回答 2

1

这取自文档

选项卡式窗口视图的容器。该对象包含两个子对象:一组选项卡标签,用户单击以选择特定选项卡,以及一个显示该页面内容的 FrameLayout 对象。单个元素通常使用此容器对象进行控制,而不是在子元素本身上设置值。

你需要有一个FrameLayout作为孩子TabHost

于 2013-09-08T04:42:52.623 回答
0

可能是因为这两个都有全屏所以可能是隐藏选项卡刚刚尝试检查布局

tab1.xml

android:layout_height="fill_parent"

tab2.xml

android:layout_height="fill_parent"

所以改成这个

tab1.xml

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

    <CheckBox
        android:id="@+id/Avocados"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Avocados" />

    <CheckBox
        android:id="@+id/Cashew_Nuts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cashew_Nuts"

        />
 ////android:checked="true"

</LinearLayout>

tab2.xml

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.81"
        android:src="@drawable/tjs" />

</LinearLayout>
于 2013-09-08T04:26:54.370 回答