android TabHost选项卡示例
2015-12-14 00:00
471 查看
1. 继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab中的内容在布局文件中定义即可。
tabactivity.xml:
2. 不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的ID必须是
@android :id/tabs,FrameLayout的ID必须是
@android :id/tabcontent,TabHost的ID可自定义。
tabxml.xml:
MyTabActivity.java 文件:
TabXmlActivity.java文件:
tabactivity.xml:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/firstTab" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="这是第一个选项卡" android:textSize="20px"/> </LinearLayout> <LinearLayout android:id="@+id/secondTab" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="这是第二个选项卡" android:textSize="20px"/> </LinearLayout> <LinearLayout android:id="@+id/thirdTab" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="这是第三个选项卡" android:textSize="20px"/> </LinearLayout> </FrameLayout>
2. 不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的ID必须是
@android :id/tabs,FrameLayout的ID必须是
@android :id/tabcontent,TabHost的ID可自定义。
tabxml.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="使用布局文件中定义TabHost的方式实现TabHost" android:textSize="15px"/> <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabWidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/view1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="这是第一个选项卡" android:textSize="20px"/> <TextView android:id="@+id/view2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="这是第二个选项卡" android:textSize="20px"/> <TextView android:id="@+id/view3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="这是第三个选项卡" android:textSize="20px"/> </FrameLayout> </LinearLayout> </TabHost> </LinearLayout>
MyTabActivity.java 文件:
package com.example.baseexample; import android.app.TabActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.TabHost; public class MyTabActivity extends TabActivity { private TabHost myTabHost ; protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); myTabHost = this.getTabHost(); LayoutInflater.from(this).inflate(R.layout.tabactivity, myTabHost.getTabContentView(),true); myTabHost.addTab(myTabHost.newTabSpec("选项卡1").setIndicator("选项卡1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.firstTab)); myTabHost.addTab(myTabHost.newTabSpec("选项卡2").setIndicator("选项卡2",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.secondTab)); myTabHost.addTab(myTabHost.newTabSpec("选项卡3").setIndicator("选项卡3",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.thirdTab)); } }
TabXmlActivity.java文件:
[/code]package com.example.baseexample; import android.app.Activity; import android.os.Bundle; import android.widget.TabHost; public class TabXmlActivity extends Activity { protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.tabxml); TabHost tabHost = (TabHost)findViewById(R.id.tabhost); tabHost.setup(); tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.view1)); tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.view2)); tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.view3)); } }
相关文章推荐
- android Service:分别继承IntetnService与Service示例
- android SQLite数据库使用实例
- androidpn消息下推(1)
- android文件访问实例
- androidpn消息下推2
- android Bound Service使用:使用Message类绑定服务
- android ContextMenu 上下文菜单示例
- android Bound Service使用:继续Binder类绑定服务
- android GridView视图的案例
- android 开发LayoutInflater之我见
- android Menu 选项菜单示例
- android ContentProvider 使用实例
- android Notification 状态栏通知使用示例
- android 备忘录之简单实现
- android Toast 消息提示 例子
- android gallery 画廊控件示例
- android 发送e-mail实例
- android Dialog对话框使用示例
- listItem.measure(0, 0) 空指针问题
- android百度地图入门中碰到的2个难题