自定义TabActivity,TabActivity的美化

使用tabhost实现脚下的菜单栏:

1.TabActivity的布局

 1 <TabHost xmlns:android="http://schemas.android.com/apk/res/android" 2  android:id="@android:id/tabhost" 3  android:layout_width="fill_parent" 4  android:layout_height="fill_parent" 5   > 6  <LinearLayout 7   android:orientation="vertical" 8   android:layout_width="fill_parent" 9   android:layout_height="fill_parent">10   11          12         <FrameLayout13          android:gravity="center"14          android:id="@android:id/tabcontent"15          android:layout_width="fill_parent"16          android:layout_height="wrap_content"17          android:layout_weight="1.0" >18          19          <LinearLayout android:id="@+id/first"20                 android:orientation="vertical"21                 android:background="#ffffff"22                 android:layout_width="fill_parent"23                 android:layout_height="fill_parent"24                >25                 <TextView android:layout_width="fill_parent"26                     android:layout_height="fill_parent"27                     android:text="first tab" />28             </LinearLayout>29             <LinearLayout android:id="@+id/second"30                 android:orientation="vertical"31                 android:background="#ffffff"32                 android:layout_width="fill_parent"33                 android:layout_height="fill_parent"34                 >35                 <TextView android:layout_width="fill_parent"36                     android:layout_height="fill_parent"37                     android:text="second tab" />38             </LinearLayout>39          40         </FrameLayout>41        42         <TabWidget43          android:id="@android:id/tabs"44          android:background="@drawable/tab_bottom_bg"45          android:padding="3.0dip"46          android:layout_width="fill_parent"47          android:layout_height="wrap_content"48          android:layout_weight="0.0" />49        50     </LinearLayout>   51 </TabHost>  
View Code

分析一下这个文件:

1、首先是<tabhost/> ,定义一个tabhost,他需要两部分组成:

  (1)<FrameLayoutandroid:id="@android:id/tabcontent"/>

  (2)<TabWidgetandroid:id="@android:id/tabs" >

要注意这个标签单位id,不能修改。能把tab放到footer的地方是因为android:layout_weight="1.0"把他挤下来。

2、(1)<LinearLayoutandroid:id="@+id/first"...

  (2)<LinearLayoutandroid:id="@+id/second" ...

这两标签内是摆放tab选项的内容的。也可单独做一个xml文件然后查找ID,或者使用activity intent的方式写对应类的xml。

2、MainActivity的实现代码

缺省的实现代码:

private void setIndicator(int icon, int title, int view) {         String str = getResources().getString(title);                  TabHost.TabSpec localTabSpec=tabhost.newTabSpec(str).setIndicator(str,getResources().getDrawable(icon)).setContent(view);         tabhost.addTab(localTabSpec);     } 

修改成美化的代码:

private void setIndicator(int icon, int title, int view) {  View localView = LayoutInflater.from(this.tabhost.getContext()).inflate(R.layout.main_activity_tab, null); ((ImageView)localView.findViewById(R.id.main_activity_tab_image)).setBackgroundResource(icon); ((TextView)localView.findViewById(R.id.main_activity_tab_text)).setText(title);  String str = getResources().getString(title);  TabHost.TabSpec localTabSpec = tabhost.newTabSpec(str).setIndicator(localView).setContent(view); tabhost.addTab(localTabSpec); } 

注意到第二句代码的R.layout.main_activity_tab了吧,下面是ayout.main_activity_tab.xml的代码:

<?xml version="1.0" encoding="UTF-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"       android:gravity="center"       android:orientation="vertical"       android:id="@+id/tabsLayout"       android:background="@drawable/tab_item_bkg"   <!-- 它是在res/drawable/tab_item_bkg.xml 实现tab Item的样式 -->    android:padding="3.0dip"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:layout_marginTop="3.0dip"       android:layout_marginBottom="3.0dip"    >            <FrameLayout           android:layout_width="fill_parent"           android:layout_height="fill_parent"           android:layout_weight="0.6">                    <ImageView               android:layout_gravity="center"               android:id="@+id/main_activity_tab_image"   <!-- 定义图标ico -->            android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:layout_marginTop="2.0dip"               android:scaleType="center" />                </FrameLayout>            <TextView           android:textSize="12.0dip"           android:textColor="@drawable/tab_text_selector"   <!-- 在drawable/tab_text_selector.xml 定义文字的样式 -->        android:id="@+id/main_activity_tab_text"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="Title" />  </LinearLayout>

本文参考:http://www.oschina.net/question/54100_32909

更多相关文章

  1. Java代码设置Android全屏
  2. ListView设置分割线Divider样式
  3. 换掉整个程序的bitton样式,以及button的父样式
  4. android4.0自定义标题报错 -----断点记录
  5. [置顶] Android 代码混淆 选项说明
  6. android秒表计时代码
  7. android代码片段一
  8. Android 自定义progressDialog
  9. Android Camera子系统代码流程1

随机推荐

  1. Android海康监控视频调用demo
  2. Android中TabHost的原理及使用
  3. Android(安卓)记录一次开发微信分享功能
  4. Android面试时的问题,实现半透明的popupw
  5. Android(安卓)给Button加个监听
  6. Android(安卓)程式开发:(二十)内容提供者 —
  7. MPAndroidChart 3.0——BarChart(一)
  8. Android(安卓)BaseActivity和BaseFragmen
  9. 动画---放缩,旋转,移动,渐变
  10. android apk如何引用系统framework.jar,s