MainActivity.java

package com.example.cmsui;import com.googlecode.androidannotations.annotations.AfterViews;import com.googlecode.androidannotations.annotations.EActivity;import com.googlecode.androidannotations.annotations.NoTitle;import android.app.TabActivity;import android.content.Intent;import android.view.LayoutInflater;import android.view.View;import android.widget.ImageView;import android.widget.TabHost;import android.widget.TextView;@NoTitle@EActivity(R.layout.activity_main)public class MainActivity extends TabActivity {@AfterViewsvoid setTabs(){addTab("首页", R.drawable.tab_home, TestActivity_.class);addTab("分类", R.drawable.tab_catalog, TestActivity_.class);addTab("我的", R.drawable.tab_profile, TestActivity_.class);}void addTab(String labelId, int drawableId, Class<?> c){TabHost tabHost = getTabHost();Intent intent = new Intent(this, c);TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);TextView title = (TextView) tabIndicator.findViewById(R.id.title);title.setText(labelId);ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);icon.setImageResource(drawableId);spec.setIndicator(tabIndicator);spec.setContent(intent);tabHost.addTab(spec);}}

layout/activity_main.xml

<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:orientation="vertical"android:layout_width="fill_parent" android:layout_height="fill_parent"><FrameLayout android:id="@android:id/tabcontent"android:layout_width="fill_parent" android:layout_height="0dip"android:layout_weight="1" /> <TabWidget android:id="@android:id/tabs"android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_weight="0"  android:background="@drawable/tabbar_background" /></LinearLayout></TabHost> 

layout/tab_indicator.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="0dip"    android:layout_height="45dip"        android:layout_weight="1"    android:orientation="vertical"        android:background="@drawable/tab_indicator"    android:padding="2dp">    <ImageView android:id="@+id/icon"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"            />     <TextView android:id="@+id/title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"         android:layout_centerHorizontal="true"        android:textSize="10sp"        android:textColor="#ffffff"                style="?android:attr/tabWidgetStyle"    />    </RelativeLayout>

drawable/tab_indicator.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"><!-- Non focused states --><item android:state_focused="false" android:state_selected="false"android:state_pressed="false" android:drawable="@drawable/tab_unselected" /><item android:state_focused="false" android:state_selected="true"android:state_pressed="false" android:drawable="@drawable/tab_selected" /><!-- Focused states --><item android:state_focused="true" android:state_selected="false"android:state_pressed="false" android:drawable="@drawable/tab_selected" /><item android:state_focused="true" android:state_selected="true"android:state_pressed="false" android:drawable="@drawable/tab_selected" /><!-- Pressed --><item android:state_selected="true" android:state_pressed="true"android:drawable="@drawable/tab_selected" /><item android:state_pressed="true" android:drawable="@drawable/tab_selected" /></selector>

drawable/tab_selected.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"><item android:left="@dimen/tab_space" android:right="@dimen/tab_space"> <shape android:shape="rectangle"><gradient android:angle="0" android:startColor="@color/tabMedium"android:endColor="@color/tabMedium" /></shape></item></layer-list>

drawable/tab_unselected.xml (效果自定义,这里不作效果)

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"></layer-list>

drawable/tab_home.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/tabbar_home_highlighted"android:state_selected="true" /><item android:drawable="@drawable/tabbar_home" /></selector> 

drawable/tab_catalog.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/tabbar_discover_highlighted"android:state_selected="true" /><item android:drawable="@drawable/tabbar_discover" /></selector>

drawable/tab_profile.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/tabbar_profile_highlighted"android:state_selected="true" /><item android:drawable="@drawable/tabbar_profile" /></selector>

图标下载地址:http://www.oschina.net/code/snippet_783576_23825

更多相关文章

  1. 自定义的跑马灯控件
  2. 让Android的手机QQ崩溃未响应的指令---亲测有效果
  3. Android(安卓)ExpandableListView分组效果
  4. Android(安卓)-- Notification
  5. 意见反馈 页面布局的设计
  6. Android(安卓)Material Design(7) 转场动画的使用
  7. Android实现控件动画效果
  8. 回弹效果listview
  9. Android图片预览效果,支持缩放、平移切换

随机推荐

  1. [译] Android应用程序基础 >> 应用程序的
  2. RecyclerView机制解析: ChildHelper
  3. Android内置应用到系统的方法总结
  4. android 的handler 机制
  5. Android中RecyclerView如何像iOS的UITabl
  6. AsyncTask(介绍篇二)
  7. 深入理解Android消息处理系统——Looper
  8. Android完全结束Activity并退出程序
  9. Android6.0 ueventd
  10. Android(安卓)的生命周期基础知识