Tab_Host

来源:互联网 发布:淘宝店铺装修全套多钱 编辑:程序博客网 时间:2024/06/14 12:21

 

TabHost

Container for a tabbed window view. This object holds two children:
a set of tab labels that the user clicks to select a specific tab,
and a FrameLayout object that displays the contents of that page.
The individual elements are typically controlled using this container object,
rather than setting values on the child elements themselves.

TabHost是整个Tab的容器,包括两部分,TabWidget和FrameLayout。TabWidget就是每个tab的标签,FrameLayout则是tab内容。

 

02-01 07:30:15.442: E/AndroidRuntime(560): java.lang.RuntimeException: Unable to start activity ComponentInfo
{com.example.tab_test/com.example.tab_test.MainActivity}: java.lang.RuntimeException: Your content must
have a TabHost whose id attribute is 'android.R.id.tabhost'

02-01 07:53:07.833: E/AndroidRuntime(285): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.tab_test/com.example.tab_test.MainActivity}: java.lang.RuntimeException:
Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'

02-01 08:02:45.452: E/AndroidRuntime(378): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.tab_test/com.example.tab_test.MainActivity}: java.lang.RuntimeException:
Your TabHost must have a FrameLayout whose id attribute is 'android.R.id.tabcontent'

 

 

Tab标签页是界面设计时经常应用的界面控件,可以实现多个分页之间的快速切换,每个分页可以显示不同的内容

Tab标签页的应用
      1.首先要设计所有的分页的界面布局
      2.在分页设计完成后,应用代码建立Tab标签页,并给每个分页添加标识和题目
      3.最后断定每个分页所显示的界面布局
  
每个分页建立一个XML文件,用以编辑和保存分页的界面布局,应用的办法与设计通俗用户界面没有什么差别
 

应用Tab标签页的一般步骤:
1.首先要设计所有的分页的界面布局

Activity继承自TabActivity
调用TabActivity的getTabHost()办法获得TabHost对象
经由过程TabHost创建Tab
 
================================网上不少例子,一堆错误,无法运行============================================
http://blog.sina.com.cn/s/blog_8cc425ca010180oi.html

http://www.byywee.com/page/M0/S693/693976.html

修改:

 

protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 声明TabHost,然后用LayoutInflater过滤出布局来,给TabHost加上含有Tab页面的FrameLayoutTabHost th = getTabHost();th.setup();// from(this)从这个TabActivity获取LayoutInflater// 1------R.layout.main 存放Tab布局// 2-----通过TabHost获得存放Tab标签页内容的FrameLayout// 3-----是否将inflate 拴系到根布局元素上// LayoutInflater.from(this).inflate(R.layout.activity_main,----------------报空指针// th.getTabContentView(), true);//经由过程TabHost获得存放Tab标签页内容的FrameLayout,// newTabSpecd的作用是获取一个新的 TabHost.TabSpec,并关联到当前 TabHost// setIndicator的作用是指定标签和图标作为选项卡的指示符.// setContent的作用是指定用于显示选项卡内容的视图 ID.th.addTab(th.newTabSpec("all").setIndicator("所有通话记录",getResources().getDrawable(R.drawable.call)).setContent(R.id.TextView01));th.addTab(th.newTabSpec("ok").setIndicator("已接来电",getResources().getDrawable(R.drawable.call)).setContent(R.id.TextView01));th.addTab(th.newTabSpec("cancel").setIndicator("未接来电",getResources().getDrawable(R.drawable.call)).setContent(R.id.TextView01));// setOnTabChangeListener的作业是注册一个回调函数,当任何一个选项卡的选中状态发生改变时调用.th.setOnTabChangedListener(new TabHost.OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) {Toast.makeText(MainActivity.this, tabId,Toast.LENGTH_LONG).show();}});}


<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@android:id/tabhost"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="vertical" >        <TabWidget            android:id="@android:id/tabs"            android:layout_width="fill_parent"            android:layout_height="wrap_content" />        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="fill_parent"            android:layout_height="fill_parent" >            <TextView                android:id="@+id/TextView01"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="所有通话记录" />            <TextView                android:id="@+id/TextView02"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="已接来电" />            <TextView                android:id="@+id/TextView03"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="未接来电" />        </FrameLayout>    </LinearLayout></TabHost>

=======================文字显示,有问题==========================


可以参考这里的:
http://blog.csdn.net/flowingflying/article/details/6304289

http://www.cnblogs.com/janken/archive/2012/07/19/2598710.html

原创粉丝点击