创新源于模仿之一:TabActivity的美化

简介:
 

今天开始一个新专题:创新源于模仿。

第一篇从TabActivity着手,一直以为Android中的TabActivity只能放在上面,只能如此丑陋,直到有一天看到“米聊”。



咋一看,软件下面的那个菜单栏,觉得像是用LinearLayout+Button来实现的,但事实上,它却是一个Tab!
怎么看出来的?我就不多说了,你懂的。

下面我们来抽丝剥茧,一步步分析它的实现过程。

1.TabActivity的布局


  1. <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:gravity="center"
             android:id="@android:id/tabcontent"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1.0" >
             
             <LinearLayout android:id="@+id/first"
                    android:orientation="vertical"
                    android:background="#ffffff"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                   >
                    <TextView android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:text="first tab" />
                </LinearLayout>
                <LinearLayout android:id="@+id/second"
                    android:orientation="vertical"
                    android:background="#ffffff"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    >
                    <TextView android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:text="second tab" />
                </LinearLayout>
             
            </FrameLayout>
           
            <TabWidget
             android:id="@android:id/tabs"
             android:background="@drawable/tab_bottom_bg"
             android:padding="3.0dip"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="0.0" />
           
        </LinearLayout>   
    </TabHost>  

 

 

我们看到,要自定义一个位于屏幕下方的TAB标签,首先我们不能使用缺省的TabActivity实现了,啥事都要自己亲力亲为。
很好理解,把tabs放在tabcontent下面就可以了。其它不多说了。


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) {
     String str = getResources().getString(title);
     
     TabHost.TabSpec localTabSpec=tabhost.newTabSpec(str).setIndicator(str,getResources().getDrawable(icon)).setContent(view);
     tabhost.addTab(localTabSpec);
    }

 

可以测试一下,效果出来了吧,虽然有点丑,但它真的在屏幕下方了。

 

3.美化TAB标签

 

现在我们来定制这个TAB的标签。先看代码:

    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);
     
    }

 

注意到这个main_activity_tab的layout了吧,看看它的内容:

 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:gravity="center"   
  4.     android:orientation="vertical"   
  5.     android:id="@id/tabsLayout"   
  6.     android:background="@drawable/tab_item_bkg"   
  7.     android:padding="3.0dip"   
  8.     android:layout_width="wrap_content"   
  9.     android:layout_height="wrap_content"   
  10.     android:layout_marginTop="3.0dip"   
  11.     android:layout_marginBottom="3.0dip"  
  12.   >  
  13.       
  14.     <FrameLayout   
  15.         android:layout_width="fill_parent"   
  16.         android:layout_height="fill_parent"   
  17.         android:layout_weight="0.6">  
  18.           
  19.         <ImageView   
  20.             android:layout_gravity="center"   
  21.             android:id="@id/main_activity_tab_image"   
  22.             android:layout_width="wrap_content"   
  23.             android:layout_height="wrap_content"   
  24.             android:layout_marginTop="2.0dip"   
  25.             android:scaleType="center" />  
  26.           
  27.     </FrameLayout>  
  28.       
  29.     <TextView   
  30.         android:textSize="12.0dip"   
  31.         android:textColor="@drawable/tab_text_selector"   
  32.         android:id="@id/main_activity_tab_text"   
  33.         android:layout_width="wrap_content"   
  34.         android:layout_height="wrap_content"   
  35.         android:text="Title" />  
  36. </LinearLayout  

 

这个文件定义了每个TAB标签的样式,tab_item_bkg定义每个item的背景(包括focused/selected/pressed),每个item上面的图标和下面的文字都在代码中动态定义即可。其中tab_text_selector则定义文字的颜色。

 

剩下的事情就越发明显了,不用多说了。

相关文章
|
1月前
|
算法 程序员 UED
探索编程之道:从功能实现到艺术创造
【2月更文挑战第18天】 在数字世界的构建中,编程已不仅仅是逻辑与算法的堆砌,它正逐步演变成一种创造性表达的手段。本文将探讨编程从基础的功能实现向高级的艺术创造的转变过程,分析编程者如何通过技术深入、创新思维和持续实践,提升其技艺至艺术境界。我们将审视几个关键要素——技术的深度理解、设计的美学融入以及代码的工艺精神,并讨论它们如何共同作用于编程实践中,以培养出能够编织数字世界之美的编程艺术家。
|
4月前
|
前端开发 JavaScript 数据可视化
元宇宙基础案例 | 大帅老猿threejs特训
元宇宙基础案例 | 大帅老猿threejs特训
|
Web App开发 前端开发 开发者
新时代布局中一些有意思的特性
新时代布局中一些有意思的特性
181 0
新时代布局中一些有意思的特性
|
机器学习/深度学习 人工智能 编解码
一周AI最火论文 | 点点手指变换UI设计风格,斯坦福发布基于计算机视觉的UI设计工具
一周AI最火论文 | 点点手指变换UI设计风格,斯坦福发布基于计算机视觉的UI设计工具
170 0
|
人工智能
带你读《少儿人工智能趣味入门动画与游戏编程一本通》之二:角色的基础:“运动”“外观”“声音”模块
Scratch是图形化的编程语言,它具有学习环境趣味性强、操作简单且直观等特点,很好适合6-12岁的孩子学习。本书是立足于Scratch 3.0版本的少儿编程入门书,能让孩子轻松愉快地掌握编程技能,锻炼和提高思维能力和创造力,为迎接人工智能时代的到来做好准备。本书以对Scratch中积木块的分类讲解作为主线,并将编程的核心思想融入大量精心设计的案例,让孩子在实际动手操作中更直观、更深刻地理解不同积木块的运用。本书对积木块的功能和用法解释详尽,语言通俗易懂,能够减少孩子对编程的畏惧心理,没有编程基础的家长也能陪伴孩子一起阅读,在融洽的亲子互动氛围中,自信、愉快地完成学习。
|
移动开发 Android开发 架构师
Android开发还有前景吗?
    “现在学习Android开发还有前景吗?” “Android开发还有什么可以研究的?” 近半年来,很多做开发不久的朋友都问过我这样的问题。
1819 0

热门文章

最新文章