0

我有 3 个标签,点击时显示不同的内容。我希望他们在点击时打开某些活动。这些活动只不过是需要显示的数据库报告。我使用 Web 视图并在 API 级别 11 的模拟器上运行良好,但Web page not found在我的设备上显示 API 级别 10 时出现错误。此外,这些选项卡仅在单击其他选项卡并返回第一个选项卡后才会显示数据。有没有办法可以直接启动活动而不是使用网络视图?

任何帮助表示赞赏。

xml

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

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/linearLayout1"
        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:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/tabs1"
            android:orientation="vertical"
            >
            <WebView
                android:id="@+id/txtGetFuelInfo"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="Get info from Database" />
                <TextView
android:id="@+id/txtGetFuel"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>
        </LinearLayout>
        <LinearLayout 
              android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/tabs2"
            android:orientation="vertical"
            >

          <WebView
                android:id="@+id/txtMainGetInfo"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="Get info from Database"/>
<TextView
                android:id="@+id/txtGetMain"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>


        </LinearLayout>

        <LinearLayout 
             android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/tabs3"
            android:orientation="vertical"
            >
            <WebView
                android:id="@+id/txtSpareGetInfo"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="Get info from Database"/>
<TextView
android:id="@+id/txtGetspare"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>

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

public class DetailsTabs extends Activity
{
    TabSpec tspec;
    FuelStoredInfo  fuelInfo;
    WebView fuelView,mainView,sparesView;
    TextView tvFuel, tvMain, tvSpares;
    WebSettings fuelSetting, mainSetting, spareSetting;
    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detailstabs);

        fuelView = (WebView)findViewById(R.id.txtGetFuelInfo);
        mainView = (WebView)findViewById(R.id.txtMainGetInfo);
        sparesView = (WebView)findViewById(R.id.txtSpareGetInfo);

        //tvFuel = (TextView)findViewById(R.id.totalFuelId);
        //tvMain = (TextView)findViewById(R.id.totalmainId);
        //tvSpares = (TextView)findViewById(R.id.totalspareId);

        fuelInfo = new FuelStoredInfo(this);
        fuelInfo.open();


        TabHost th = (TabHost)findViewById(R.id.tabhost);
        th.setup();

        // Fuel Expense tab
        tspec = th.newTabSpec("Tag 1");

        tspec.setContent(R.id.tabs1);
        tspec.setIndicator("Fuel"+"\n"+"Expense");
        String Data = fuelInfo.getData();
           double fuelTotal  =fuelInfo.getTotalFuel();
        fuelSetting = fuelView.getSettings();
        fuelSetting.setDefaultFontSize(10);
        //tvFuel.setText("Total fuel expense : Rs. "+ String.valueOf(fuelTotal));
        fuelView.loadData(Data,"text/html",null);
        th.addTab(tspec);

        //Maintenance Expense tab
        tspec = th.newTabSpec("Tag 2");
        tspec.setContent(R.id.tabs2);
        tspec.setIndicator("Maintenance"+"\n"+"Expense");
        mainSetting = mainView.getSettings();
        mainSetting.setDefaultFontSize(10);
        String mainData = fuelInfo.getMainData();
        double mainTotal  =fuelInfo.getTotalMain();
        tvMain.setText("Total maintenance expense : Rs. "+ String.valueOf(mainTotal));
        mainView.loadData(mainData,"text/html",null);

        th.addTab(tspec);

        //Spares Expense tab
        tspec = th.newTabSpec("Tag 3");

        tspec.setContent(R.id.tabs3);
        tspec.setIndicator("Spares"+"\n"+"Expense");
        spareSetting = sparesView.getSettings();
        spareSetting.setDefaultFontSize(10);
        String sparesData= fuelInfo.getSpareData();
        double spareTotal = fuelInfo.getTotalSpares();
        tvSpares.setText("Total spares expense : Rs."+ String.valueOf(spareTotal));
        sparesView.loadData(sparesData,"text/html",null);

        th.addTab(tspec);


        fuelInfo.close();



    }

}
4

2 回答 2

0

TabHost 已弃用,为什么不使用 ActionBar ?

无论如何..我认为你应该使用 TabHost 这样的东西

Resources ressources = getResources(); 
    TabHost tabHost = getTabHost(); 

    // Android tab
    Intent intentAndroid = new Intent().setClass(this, AndroidActivity.class);
    TabSpec tabSpecAndroid = tabHost
      .newTabSpec("Android")
      .setIndicator("", ressources.getDrawable(R.drawable.icon_android_config))
      .setContent(intentAndroid);

这个TabHost 示例有一个很好的教程

于 2013-08-21T04:22:35.957 回答
0

尝试这个

    tabHost = (TabHost) findViewById(android.R.id.tabhost);

    TabSpec Spec = null;
    Spec = tabHost.newTabSpec("home");
    Spec.setIndicator("Home", getResources().getDrawable(R.drawable.ic_tab_home));
    Spec.setContent(new Intent(this, MainList.class));
    tabHost.addTab(Spec);

    Spec = tabHost.newTabSpec("recent");
    Spec.setIndicator("Recent", getResources().getDrawable(R.drawable.ic_tab_recent));
    Spec.setContent(new Intent(this, Recent.class));
    tabHost.addTab(Spec);

    Spec = tabHost.newTabSpec("setting");
    Spec.setIndicator("Setting", getResources().getDrawable(R.drawable.ic_tab_setting));
    Spec.setContent(new Intent(this, Setting.class));
    tabHost.addTab(Spec);

    tabHost.getTabWidget().setCurrentTab(0);
于 2013-08-21T05:46:58.860 回答