首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

android小结之TabHost

2012-07-03 
android总结之TabHost很多android应用都会用到TabHost组件。有人用系统自带的,有人自定义。我个人比较懒,同

android总结之TabHost

很多android应用都会用到TabHost组件。有人用系统自带的,有人自定义。

我个人比较懒,同时比较喜欢sping的“轮子理论”,有轮子就不再造轮子。所以我是使用系统自带的TabHost

关于TabHost,我是随着项目的开发,新的需求的不断提出,而不断认识的。

下面进入正题,讲一下我对TabHost的功能需求。

1.每个选项卡对应一个Activity

2.选项卡标签样式自定义

第一个功能在setContent中设定(参数为Intent对象)

第二个功能在setIndicator中设定(参数为View对象)

具体代码如下:

?
Intent intent;intent = new Intent(this, TabActivity1.class);LayoutInflater mInflater = LayoutInflater.from(this);View tab1Spec = mInflater.inflate(R.layout.tab1_spec, null);tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(tab1Spec).setContent(intent));

关于tab1_spec这个布局文件,肯定会涉及到选项卡背景颜色和文字颜色在选中和非选中状态的切换,这就需要在drawable文件夹下定义切换xml文件了。

上一下代码:

tab1_spec.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <RelativeLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_marginTop="1dp"        android:background="@drawable/tab_spec_background" >        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginTop="3dp"            android:layout_centerHorizontal="true"             android:background="@drawable/tab1_spec_icon"/>        <TextView android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginTop="25dp"            android:layout_centerHorizontal="true"             android:text="@string/tab1" android:textColor="@drawable/tab_spec_text_color"            />     </RelativeLayout></RelativeLayout>

?tab_spec_background为选项卡背景,xml文件如下:

<?xml version="1.0" encoding="utf-8"?><!-- 选项卡背景色--><selector xmlns:android="http://schemas.android.com/apk/res/android">    <!-- When selected, use grey -->    <item android:drawable="@drawable/main_tab_frame_tabspec_background_current" android:state_selected="true"           />    <!-- When not selected, use white    <item android:drawable="@drawable/main_tab_frame_product_category"  />--></selector>

?tab1_spec_icon为选项中的图标,其xml文件和tab_spec_background差不多

tab_spec_text_color为选项卡的文件颜色,xml文件和上面略有不同,需要将android:drawable属性换成android:color属性。

最后上一下源码(图片取自太平洋汽车android版),如果要转发,请注明来自

http://chenjinbo1983.iteye.com/admin/blogs/1326644

热点排行