android 封装好的BottomTabBar

来源:互联网 发布:欠淘宝贷款2年了没还上 编辑:程序博客网 时间:2024/05/29 00:33

 1 在android app开发过程中,会发现很多App的底部(顶部一样) 会仿效IPHONE的设计。。做一个导航。  如下图黑色部分:

                                   

                                 (这个是实现效果)                                                                 这个是设计原型


        1.1 要是中间部分没有那个拱起来的部分,那么可能设计还简单一些。。 仔细分析下设计原型,,,中间那个 摇摇 的图标要大一些,而且和其他图标没在一个水平线上。


      1.2  总的思想就是 先封装一个 没有 凸起 的通用,自动适配的组建。。。然后。。。在上面 通过 FrameLayout 放置一个 突起的组建。。。


       1.3 设计细节

          1.3.1  自己写一个  BottomTabBar   (extends) LinearLayout .  通过 Inflater 加载一个 自定义的 View (通过xml配置), 在View 的xml 中,注意书写好适配就OK了。

                   注意 layout_weight 的使用。(参见上一篇 listview)

       


           自定义LinearLayout:

          

[java] view plaincopy
  1. package com.dp.android.widget;  
  2.   
  3. import com.dp.android.elong.shake.R;  
  4.   
  5. import android.content.Context;  
  6. import android.util.AttributeSet;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.widget.LinearLayout;  
  10.   
  11. public class BottomTabBar extends LinearLayout {  
  12.     //--implements two kinds of constructions--  
  13.     public  BottomTabBar(Context context) {  
  14.         super(context);  
  15.           
  16.         init(context);  
  17.     }  
  18.   
  19.   
  20.     public BottomTabBar(Context context, AttributeSet attrs) {  
  21.         super(context, attrs);  
  22.           
  23.         init(context);  
  24.     }  
  25.       
  26.     private void init(Context context) {  
  27.         LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);  
  28.         setLayoutParams(lp);  
  29.           
  30.         LayoutInflater mInflater = LayoutInflater.from(context);  
  31.           
  32.         View v = mInflater.inflate(R.layout.bottom_tabs, null);  
  33.           
  34.         addView(v,lp);  
  35.     }  
  36. }  

     

           自定义的View(xml 形式)

[java] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.     <LinearLayout   
  3.        xmlns:android="http://schemas.android.com/apk/res/android"   
  4.         android:layout_width="fill_parent"  
  5.         android:layout_height="fill_parent"  
  6.         >  
  7.            
  8.         <!-- 搜索 -->  
  9.         <LinearLayout    
  10.             android:id="@+id/ll_bottom_tabls_search"   
  11.             style = "@style/bottom_tabs_ll"  
  12.             >  
  13.               
  14.             <ImageView   
  15.                 android:id="@+id/im_search"  
  16.                 style = "@style/bottom_tabs_image"  
  17.                 android:src="@drawable/bottom_tab_search" />  
  18.               
  19.               
  20.             <TextView   
  21.                 style = "@style/bottom_tabs_tv"  
  22.                 android:text="@string/bottom_tab_search"  
  23.                 />         
  24.               
  25.          </LinearLayout>       
  26.            
  27.         <!-- 摇过 -->  
  28.         <LinearLayout   
  29.             android:id="@+id/ll_bottom_tabls_history"   
  30.             style = "@style/bottom_tabs_ll"  
  31.             >  
  32.       
  33.             <ImageView   
  34.                 android:id="@+id/im_history"  
  35.                 style = "@style/bottom_tabs_image"  
  36.                 android:src="@drawable/bottom_tab_history" />  
  37.               
  38.             <TextView   
  39.                 style = "@style/bottom_tabs_tv"  
  40.                 android:text="@string/bootom_tab_shake"  
  41.                 />             
  42.                       
  43.          </LinearLayout>  
  44.            
  45.         <!-- 换一个 -->  
  46.         <LinearLayout   
  47.             android:id="@+id/ll_bottom_tabls_change"   
  48.             style = "@style/bottom_tabs_ll"  
  49.             >  
  50.             <ImageView   
  51.                 android:id="@+id/im_change"  
  52.                 style = "@style/bottom_tabs_image"  
  53.                 android:src="@drawable/bottom_tab_blank"  
  54.                 />  
  55.               
  56.             <TextView   
  57.                 style = "@style/bottom_tabs_tv"  
  58.                 android:text="@string/bottom_tab_change"  
  59.                 />  
  60.          </LinearLayout>  
  61.            
  62.         <!-- 收藏 -->  
  63.         <LinearLayout   
  64.             android:id="@+id/ll_bottom_tabls_collection"   
  65.             style = "@style/bottom_tabs_ll"  
  66.             >  
  67.               
  68.             <ImageView  
  69.                 android:id="@+id/im_collection"  
  70.                 style = "@style/bottom_tabs_image"  
  71.                 android:src="@drawable/bottom_tab_collection"  
  72.                 />      
  73.                   
  74.             <TextView   
  75.                 style = "@style/bottom_tabs_tv"  
  76.                 android:text="@string/bootom_tab_collection"  
  77.                 />     
  78.          </LinearLayout>    
  79.            
  80.         <!-- 更多 -->     
  81.         <LinearLayout   
  82.             android:id="@+id/ll_bottom_tabls_more"   
  83.             style = "@style/bottom_tabs_ll"  
  84.             >  
  85.               
  86.             <ImageView  
  87.                 android:id="@+id/im_more"  
  88.                 style = "@style/bottom_tabs_image"  
  89.                 android:src="@drawable/bottom_tab_more"  
  90.                 />  
  91.       
  92.             <TextView   
  93.                 style = "@style/bottom_tabs_tv"  
  94.                 android:text="@string/bootom_tab_more"  
  95.                 />             
  96.          </LinearLayout>  
  97.       
  98.     </LinearLayout>  

  

         view 中用到的 三个 style

        

[java] view plaincopy
  1. <style name="bottom_tabs_ll" >  
  2.     <item name="android:layout_width">wrap_content</item>  
  3.     <item name="android:layout_height">wrap_content</item>  
  4.     <item name="android:background">@color/black</item>  
  5.     <item name="android:orientation">vertical</item>  
  6.     <item name="android:gravity">center</item>  
  7.     <item name="android:layout_gravity">bottom</item>  
  8.     <item name="android:layout_weight">1</item>  
  9. </style>  
  10.   
  11. <style name="bottom_tabs_image" >  
  12.     <item name="android:layout_width">fill_parent</item>  
  13.     <item name="android:layout_height">wrap_content</item>  
  14.     <item name="android:paddingTop">5dip</item>  
  15. </style>  
  16.   
  17. <style name="bottom_tabs_tv" >  
  18.     <item name="android:layout_width">fill_parent</item>  
  19.     <item name="android:layout_height">wrap_content</item>  
  20.     <item name="android:textSize">12sp</item>  
  21.     <item name="android:textColor">@color/common_white</item>  
  22.     <item name="android:paddingTop">3dip</item>  
  23.     <item name="android:gravity">center</item>  
  24. </style>  


//---------------------------------------------------------------下面是如何使用自己定义的 组建了-------------------------------------

1: 注意 BottomTabBar ,,, 这里面还用到了自定义的 “凸起” 组件。 MyBottmArc

    <FrameLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/shake_yellow_bg"
        android:orientation="vertical"
        >
        <com.dp.android.widget.BottomTabBar
            android:id="@+id/bottom_tab_bar1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:paddingTop="30dip"
            />  
            
        <com.dp.android.widget.MyBottmArc
            android:id="@+id/im_change"
            android:layout_width="80dip"
            android:layout_height="60dip"
            android:padding="15dip"
            android:src="@drawable/bottom_tab_search"
            android:layout_gravity="top|center_horizontal"
            />         
        
    </FrameLayout>


//--------------------------------------------------------------------------------------下面看看  凸起 是怎么弄的---------------------------------------------------------------------------------

  凸起 也是自己定义了一个 ImageView,  这个ImageView 的背景  就是绘制半个 圆弧。。。“拱形”


[java] view plaincopy
  1. package com.dp.android.widget;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Paint;  
  6. import android.graphics.Rect;  
  7. import android.graphics.RectF;  
  8. import android.util.AttributeSet;  
  9. import android.widget.ImageView;  
  10.   
  11. public class MyBottmArc extends ImageView {  
  12.   
  13.     private static Paint paint = new Paint();  
  14.       
  15.     public MyBottmArc(Context context) {  
  16.         super(context);  
  17.     }  
  18.   
  19.     public MyBottmArc(Context context, AttributeSet attrs) {  
  20.         super(context, attrs);  
  21.     }  
  22.       
  23.     public MyBottmArc(Context context, AttributeSet attrs, int defStyle) {  
  24.         super(context, attrs, defStyle);  
  25.     }  
  26.       
  27.     protected void onDraw(Canvas canvas) {  
  28.         Rect m_rect = canvas.getClipBounds();  
  29.         canvas.save();  
  30.           
  31.         paint.setColor(0xff000000);  
  32.           
  33.         float m_left = m_rect.left;  
  34.         float m_top = m_rect.top;  
  35.         float m_right = m_rect.right;  
  36.         float m_bottom = (1.2f)*(m_rect.bottom);  
  37.           
  38.         RectF m_rectf = new RectF(m_left,m_top,m_right,m_bottom);  
  39.         canvas.drawOval(m_rectf, paint);  
  40.           
  41.         canvas.restore();  
  42.           
  43.         super.onDraw(canvas);  
  44.     }  
  45. }  
2 0
原创粉丝点击