Android TabHost的使用(Tab为Layout)
Posted on 2013-03-16 17:35 work hard work smart 阅读(532) 评论(0) 编辑 收藏 举报Android TabHost的使用,这里采用继承TabActivity的方法。
这里分别定制三个Tab,分别为american.xml, chinese.xml, japanese.xml三个Layout。
american.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" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="American1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="American2" /> </LinearLayout>
chinese.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" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Chinese button 1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Chinese button 2" /> </LinearLayout>
japanese.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" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Japanese button 1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Japanese button 2" /> </LinearLayout>
三个Layout对应的java文件为AmericanActivity.java, ChinaActivity.java, JapanActivity.java
AmericanActivity.java文件
public class AmericanActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.american); } }
ChinaActivity.java文件
public class ChinaActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chinese); } }
JapanActivity.java文件
public class JapanActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.japanese); } }
MainActivity.java 继承TabActivity。
public class MainActivity extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); Resources resources = getResources(); TabHost tabHost = getTabHost(); TabHost.TabSpec spec; Intent intent = new Intent(this,AmericanActivity.class ); spec = tabHost.newTabSpec("American"); spec.setIndicator("Ameican Tab"); spec.setContent(intent); tabHost.addTab(spec); Intent intent2 = new Intent(this,ChinaActivity.class ); spec = tabHost.newTabSpec("China"); spec.setIndicator("China Tab") spec.setContent(intent2); tabHost.addTab(spec); Intent intent3 = new Intent(this,JapanActivity.class ); spec = tabHost.newTabSpec("Japanese"); spec.setIndicator("Japanese Tab"); spec.setContent(intent3); tabHost.addTab(spec); tabHost.setCurrentTab(1); } }
注意:在AndroidManifest文件中加入下面三行代码。
<activity android:name="com.example.app1.AmericanActivity" android:label="@string/app_name"></activity> <activity android:name="com.example.app1.JapanActivity" android:label="@string/app_name"></activity> <activity android:name="com.example.app1.ChinaActivity" android:label="@string/app_name"></activity>
效果图:
作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明!