Android TabHost的使用(Tab为Layout)
2013-03-16 17:35
423 查看
Android TabHost的使用,这里采用继承TabActivity的方法。
这里分别定制三个Tab,分别为american.xml, chinese.xml, japanese.xml三个Layout。
american.xml文件
chinese.xml文件
japanese.xml文件
三个Layout对应的java文件为AmericanActivity.java, ChinaActivity.java, JapanActivity.java
AmericanActivity.java文件
ChinaActivity.java文件
JapanActivity.java文件
MainActivity.java 继承TabActivity。
注意:在AndroidManifest文件中加入下面三行代码。
效果图:
这里分别定制三个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>
效果图:
![](http://images.cnitblog.com/blog/158914/201303/16173240-8251388aac0647219f4699c1ab01dd7f.png)
相关文章推荐
- Android学习之Tab控件(使用TabHost和与Tab对应的Menu,动态更改Tabhost的Tab的背景图标,用到Ratingbar控件)
- android.support.design.widget.TabLayout的使用
- Android SupportDesign 只TabLayout使用详解
- AndroidSupportDesign之TabLayout使用详解
- Android 5.0新控件 Tablayout | 导航栏 介绍及使用详情
- Android TabLayout和ViewPager配套的简单使用
- 【Android界面实现】使用ActionBar和DrawerLayout纯原生控件,实现侧滑栏和滑动Tab界面
- Android基础控件——TabLayout的使用、仿爱奇艺导航条
- Android Tablayout使用示例及详解
- Android使用TabLayout+ViewPager实现顶部滑动菜单栏
- android TabLayout的简单使用
- Android-TabLayout使用小结
- Android 开发之 TabLayout + ViewPager 的使用
- android TabLayout的简单使用
- android学习-----使用TabLayout实现Tab选项卡以及遇到的一些问题(一)
- Android使用TabLayout实现选项卡
- android 使用TabLayout 自定义View布局作为底部菜单栏
- Android进阶之路 - Tablayout与Viewpager的使用(二)
- Android中的ViewPager、Fragment、TabLayout三者联合使用(将Fragment放置于ViewPager中)
- Android使用TabHost实现在首页tab再次点击首页tab时,刷新首页数据或者recyclerView回到顶部的需求