关于TabHost的问题
想做一个简单的标签页,即TabHost,可编译完后,运行模拟器时,总是出现如下的情况。(我用的版本时2.3.3,开发工具是ADT)
下面是我的activity_main.xml文件
程序代码:
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas. android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout xmlns:android="http://schemas. android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/text1" /> </LinearLayout> <LinearLayout android:id="@+id/tab2" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/textView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/text2"/> </LinearLayout> </TabHost> 下面则是Mainfest.xml文件 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas. package="com.example.stone" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.stone.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> </application> </manifest>下面是java主程序
程序代码:
package com.example.stone; import android.os.Bundle; //import android.app.Activity; import android.app.TabActivity; //import android.content.Intent; //import android.view.Menu; import android.view.LayoutInflater; import android.widget.TabHost; import android.widget.TabHost.OnTabChangeListener; import android.widget.Toast; public class MainActivity extends TabActivity { private TabHost mTabHost; //private Adapter1 ad1; //private ListView ListView1; //private Intent mIntent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); //ad1 = new Adapter1(MainActivity.this,4); //mIntent = new Intent(); TabHost tabHost = this.getTabHost(); LayoutInflater.from(this).inflate(R.layout.activity_main, mTabHost.getTabContentView(), true); tabHost.addTab(tabHost.newTabSpec("1") .setIndicator("OneTab",null) .setContent(R.id.tab1)); tabHost.addTab(tabHost.newTabSpec("2") .setIndicator("TwoTab",null) .setContent(R.id.tab2)); mTabHost.setOnTabChangedListener(new OnTabChangeListener(){ public void onTabChanged(String tabId){ if(tabId == "1"){ DisplayToast("Tab1"); } else{ DisplayToast("Tab2"); } } }); } protected void DisplayToast(String s) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show(); } /*@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } */ }