微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > android TabHost的使用介绍

android TabHost的使用介绍

时间:09-12 来源:互联网 点击:

最近使用tabhost,有一些新的,遂写在下面:

tabhost的使用有一些固定的格式,首先要求在布局文件的格式为tabhost标签里面添加framelayout,在里面添加相应的控件,至少包括一个framelayout和tabwidget,framelayout必须命名为@android:id/tabcontent,tabwidget必须命名为@android:id/tabs,这里,tabcontent里面存放的是加载的多个activity,tabs里面存放的是与各个activity相对应的下面的按钮,这里需要注意的是,我刚开始的时候布局文件设定完毕,tabs就是显示不出来,最后发现需要在tabcontent里面设置android:layout_weight=1之后就可以了,不知道是什么原因,有大神知道请告知小弟。

android:layout_width=match_parent

android:layout_height=match_parent

android:id=@android:id/tabhost>

android:layout_width=fill_parent

android:layout_height=fill_parent>

android:layout_width=fill_parent

android:layout_height=fill_parent

android:orientation=vertical

>

android:layout_width=fill_parent

android:layout_height=fill_parent

android:id=@android:id/tabcontent

android:background=#fff00000

android:layout_weight=1>

android:id=@+id/tvTabShpw

android:layout_width=fill_parent

android:layout_height=wrap_content

android:text=textviewshow

android:layout_gravity=bottom

android:gravity=center/>

android:layout_width=fill_parent

android:layout_height=wrap_content

android:id=@android:id/tabs

android:visibility=gone>

之后就只需要在对应的activity里面添加相应的activity就行了

tabhost = this.getTabHost();

tabhost.addTab(tabhost.newTabSpec(INDEX_HOME)

.setIndicator(INDEX_HOME)

.setContent(new Intent(this, NewHomeActivity.class)));

tabhost.addTab(tabhost.newTabSpec(INDEX_MESSAGE)

.setIndicator(INDEX_MESSAGE).setContent(new Intent(this,MessageActivityNew.class)));

tabhost.addTab(tabhost.newTabSpec(INDEX_PROFILE).setIndicator(INDEX_PROFILE)

.setContent(new Intent(this, SelfProfileActivity.class)));

tabhost.addTab(tabhost.newTabSpec(INDEX_SQUARE).setIndicator(INDEX_SQUARE)

.setContent(new Intent(this, SquareActivityNew.class)));

其中INDEX_SQUARE为activity的唯一标示,setIndicator设置的是tabs上现实的文字,setContent是显示的activity。

然后通过点击下面的tabs或者通过tabhost.setCurrentTab(0);来切换view。

同时setIndicator里面除了添加字符串,也可以添加view,美化一下。但是一般这样还是不够美观,所以我就自定义了一个view盖住tabs,通过点击里面的事件出发tabhost.setCurrentTab(0);来切换view。

同时view切换的时候也可以添加适当的动画效果:

Animation inAnim = null, outAnim = null;

if (tabhost.getCurrentTab() itemPos) {

outAnim = AnimationUtils

.loadAnimation(MainActivity.this,

R.anim.push_left_out);

inAnim = AnimationUtils.loadAnimation(

MainActivity.this, R.anim.push_left_in);

tabhost.getCurrentView().startAnimation(

outAnim);

tabhost.setCurrentTab(itemPos);

tabhost.getCurrentView()

.startAnimation(inAnim);

} else if (tabhost.getCurrentTab() > itemPos) {

outAnim = AnimationUtils.loadAnimation(

MainActivity.this,

R.anim.push_right_out);

inAnim = AnimationUtils

.loadAnimation(MainActivity.this,

R.anim.push_right_in);

tabhost.getCurrentView().startAnimation(

outAnim);

tabhost.setCurrentTab(itemPos);

tabhost.getCurrentView()

.startAnimation(inAnim);

} else {

tabhost.setCurrentTab(itemPos);

}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top