注册 登录
编程论坛 Android开发

关于TabHost的问题

yu1543054075 发布于 2015-11-12 21:57, 3421 次点击
1.MainActivity.java
package com.example.tabhost_1;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

public class MainActivity extends TabActivity {
    private TabHost th;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        th = getTabHost();//TabHost  getTabHost() 这是TabActivity中的方法,返回一个TabHost
        th.addTab(th.newTabSpec("小狗").setIndicator("tab1", getResources().getDrawable(R.drawable.p_12)));
        th.addTab(th.newTabSpec("马尔大夫").setIndicator("tab2",getResources().getDrawable(R.drawable.p_9)));
        th.addTab(th.newTabSpec("林志颖").setIndicator("tab3",getResources().getDrawable(R.drawable.p_10)));
        
        setContentView(R.layout.tab);
        
        th.setOnTabChangedListener(new OnTabChangeListener() {
            public void onTabChanged(String tabId) {
               if(tabId.equals("tab1")) {
                   Intent intent = new Intent();
                   intent.setClass(MainActivity.this, DongWu.class);
                   startActivity(intent);
                  
               }
               if(tabId.equals("tab1")) {
                   Intent intent = new Intent();
                   intent.setClass(MainActivity.this, FengJin.class);
                   startActivity(intent);
               }
               if(tabId.equals("tab1")) {
                   Intent intent = new Intent();
                   intent.setClass(MainActivity.this, RenWu.class);
                   startActivity(intent);
                }
            }
        });
        
    }

    @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;
    }
}
2.DongWu.java
package com.example.tabhost_1;

import android.app.Activity;
import android.os.Bundle;

public class DongWu extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dongwu);
    }
}
3.FengJin.java
package com.example.tabhost_1;

import android.app.Activity;
import android.os.Bundle;

public class FengJin extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fengjing);
    }
}
4.RenWu.java
package com.example.tabhost_1;

import android.app.Activity;
import android.os.Bundle;

public class RenWu extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.renwu);
    }
}

1.activity_main.xml
<LinearLayout xmlns:android="http://schemas.
    xmlns:tools="http://schemas.
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
</LinearLayout>

2.tab.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
     <RelativeLayout android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">
      
       <!-- TabWidget组件id值不可变-->
       <TabWidget android:id="@android:id/tabs"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_alignParentBottom="true">
       </TabWidget>
      
       <!-- FrameLayout布局,id值不可变-->
       <FrameLayout android:id="@android:id/tabcontent"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:layout_above="@android:id/tabs">
       </FrameLayout>
      
    </RelativeLayout>
   

</TabHost>
3.AndroidManifiest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.
    package="com.example.tabhost_1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.tabhost_1.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="DongWu"></activity>
        <activity android:name="RenWu"></activity>
        <activity android:name="FengJin"></activity>
    </application>

</manifest>
结果在虚拟手机上一运行就自动退出了,不知道哪里出错了,还请高手帮一下忙


1 回复
#2
kevin_012015-11-29 21:32
你最好是把错误log 也发出来,这样好直接看错误 ,不然就只有码来找错误也比较困难呀,又是直接在网页上的。
还有现在谷歌官方已经不推荐使用tabhost 的,而有实现tabhost的标签样式,可以有很多方式,如fragment、viewpage ,还有二者的结合。
1