| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3418 人关注过本帖
标题:关于TabHost的问题
只看楼主 加入收藏
yu1543054075
Rank: 1
等 级:新手上路
帖 子:102
专家分:8
注 册:2015-4-30
结帖率:90.24%
收藏
 问题点数:0 回复次数:1 
关于TabHost的问题
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>
结果在虚拟手机上一运行就自动退出了,不知道哪里出错了,还请高手帮一下忙


搜索更多相关主题的帖子: private public import 
2015-11-12 21:57
kevin_01
Rank: 2
等 级:论坛游民
帖 子:36
专家分:83
注 册:2013-11-23
收藏
得分:0 
你最好是把错误log 也发出来,这样好直接看错误 ,不然就只有码来找错误也比较困难呀,又是直接在网页上的。
还有现在谷歌官方已经不推荐使用tabhost 的,而有实现tabhost的标签样式,可以有很多方式,如fragment、viewpage ,还有二者的结合。

Hello  World !
2015-11-29 21:32
快速回复:关于TabHost的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020214 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved