android自定义控件之底部自动切换Tab布局

     今天为大家分享一下,我自定义的底部切换tab布局。先看效果图吧。点击底部控件,自动变色,自动切换不同的布局。

  

一、首先要定义一个底部的item,layout_bottom_item.xml。也就是上图中的单个点击项目。并且居中显示。

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@color/text_white">  
  6.   
  7.     <RelativeLayout  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerInParent="true"  
  11.         >  
  12.         <ImageView  
  13.             android:id="@+id/tab_woicon"  
  14.             android:layout_centerHorizontal="true"  
  15.             android:layout_marginTop="@dimen/text_padding"  
  16.             android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content"  
  18.             android:background="@mipmap/tab_icon_normal"/>  
  19.         <TextView  
  20.             android:id="@+id/tab_wotext"  
  21.             android:layout_marginTop="@dimen/text_padding"  
  22.             android:layout_below="@id/tab_woicon"  
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"  
  25.             android:layout_centerHorizontal="true"  
  26.             android:textSize="@dimen/text_size16"  
  27.             android:text="@string/wode_task"/>  
  28.     </RelativeLayout>  
  29. </RelativeLayout>  
 效果如图

 

二、创建底部布局,layout_bottom.xml。使用include的标签,引用刚才的布局文件,并且为每一个引用布局定义自己的id。

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="@dimen/bottom_height"  
  5.     android:orientation="horizontal">  
  6.   
  7.     <include  
  8.         android:id="@+id/wode_order"  
  9.         android:layout_width="0dp"  
  10.         android:layout_weight="1"  
  11.         android:layout_height="match_parent"  
  12.         layout="@layout/layout_bottom_item"></include>  
  13.     <include  
  14.         android:id="@+id/history_order"  
  15.         android:layout_width="0dp"  
  16.         android:layout_weight="1"  
  17.         android:layout_height="match_parent"  
  18.         layout="@layout/layout_bottom_item"></include>  
  19.     <include  
  20.         android:id="@+id/wode_info"  
  21.         android:layout_width="0dp"  
  22.         android:layout_weight="1"  
  23.         android:layout_height="match_parent"  
  24.         layout="@layout/layout_bottom_item"></include>  
  25. </LinearLayout>  
三、布局文件创建好之后,第一,我们就创建MyBottomLayout类,并且继承自LinerLayout。实现LinerLayout的构造方法,否则会报错。第二,在构造方法中我们使用inflater把我们刚才创建的layout_bottom.xml压进我们当前的布局。第三,初始化底部布局中的控件。第四,根据初始化的底部布局控件找到底部各个控件,并未他们设置初始值。第五,为初始化的底部布局设置点击事件。根据点击的位置,设置底部控件不同的颜色和状态。第六,自定义一个接口,并且定义一个回调方法,在点击实践中传入点击的ID,以便我们在使用这个控件的时候,拿到点击的id,在activity中回调接口,处理点击事件。

 

  

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.meijianfang.customView;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.widget.ImageView;  
  8. import android.widget.LinearLayout;  
  9. import android.widget.RelativeLayout;  
  10. import android.widget.TextView;  
  11.   
  12. import com.meijianfang.R;  
  13.   
  14.   
  15. /** 
  16.  * Created by cdy on 2016/3/9. 
  17.  * 自定义底部tab布局 
  18.  */  
  19. public class MyBottomLayout extends LinearLayout{  
  20.     RelativeLayout wode_Relativelayout;  
  21.     RelativeLayout history_Relativelayout;  
  22.     RelativeLayout info_Relativelayout;  
  23.     LayoutInflater inflater;  
  24.     public MyBottomLayout(Context context, AttributeSet attrs) {  
  25.         super(context, attrs);  
  26.         inflater = LayoutInflater.from(getContext());  
  27.         View view = inflater.inflate(R.layout.layout_bottom,this);  
  28.         findView(view);  
  29.         initData();  
  30.         setOnclick();  
  31.     }  
  32.   
  33.     /** 
  34.      * 根据初始化的布局控件,找到我们的底部各个控件,并未他们设置初始值。 
  35.      */  
  36.     private void initData() {  
  37.         ImageView imageView = (ImageView)wode_Relativelayout.findViewById(R.id.tab_woicon);  
  38.         imageView.setBackgroundResource(R.mipmap.tab_icon_pressed);  
  39.         TextView textView = (TextView)wode_Relativelayout.findViewById(R.id.tab_wotext);  
  40.   
  41.         textView.setTextColor(getResources().getColor(R.color.button_green));  
  42.         textView.setText("我的任务");  
  43.   
  44.         ImageView imageView1 = (ImageView)history_Relativelayout.findViewById(R.id.tab_woicon);  
  45.         imageView1.setBackgroundResource(R.mipmap.tabl_icon_normal);  
  46.         TextView textView1 = (TextView)history_Relativelayout.findViewById(R.id.tab_wotext);  
  47.         textView1.setTextColor(getResources().getColor(R.color.black));  
  48.         textView1.setText("历史任务");  
  49.   
  50.         ImageView imageView2 = (ImageView)info_Relativelayout.findViewById(R.id.tab_woicon);  
  51.         imageView2.setBackgroundResource(R.mipmap.tabp_icon_normal);  
  52.         TextView textView2 = (TextView)info_Relativelayout.findViewById(R.id.tab_wotext);  
  53.         textView2.setTextColor(getResources().getColor(R.color.black));  
  54.         textView2.setText("个人信息");  
  55.     }  
  56.   
  57.     /** 
  58.      * 初始化后的底部布局设置点击事件 
  59.      */  
  60.     private void setOnclick() {  
  61.         wode_Relativelayout.setOnClickListener(new lister());  
  62.         history_Relativelayout.setOnClickListener(new lister());  
  63.         info_Relativelayout.setOnClickListener(new lister());  
  64.     }  
  65.   
  66.     //自定义接口  
  67.    public interface  ICallbackLister{  
  68.         public void click(int id);  
  69.     }  
  70.   
  71.     ICallbackLister callbackLister = null;  
  72.   
  73.     /** 
  74.      * @param callbackLister 
  75.      * 接口的回调方法 
  76.      */  
  77.     public void setOnCallbackLister(ICallbackLister callbackLister){  
  78.         this.callbackLister = callbackLister;  
  79.     }  
  80.     //点击事件的处理  
  81.     private class lister implements OnClickListener {  
  82.   
  83.         @Override  
  84.         public void onClick(View view) {  
  85.             switch (view.getId()){  
  86.                 case R.id.wode_order:  
  87.                     //更改图标颜色  
  88.                     //实现页面切换  
  89.                     initViewPic(0);  
  90.                     break;  
  91.                 case R.id.history_order:  
  92.                     initViewPic(1);  
  93.                     break;  
  94.                 case R.id.wode_info:  
  95.                     initViewPic(2);  
  96.                     break;  
  97.                 default:  
  98.                     break;  
  99.   
  100.             }  
  101.             //点击id,传入接口的方法  
  102.             callbackLister.click(view.getId());  
  103.         }  
  104.     }  
  105.   
  106.     /** 
  107.      * @param i 
  108.      * 根据点击切换卡的不同位置,给底部控件设置切换后的颜色和状态。 
  109.      */  
  110.   
  111.     private void initViewPic(int i) {  
  112.         switch (i){  
  113.             case 0:  
  114.                 //把第一个切换卡设置成点击的状态  
  115.                 TextView textView = (TextView)wode_Relativelayout.findViewById(R.id.tab_wotext);  
  116.                 textView.setTextColor(getResources().getColor(R.color.button_green));  
  117.   
  118.                 TextView textView1 = (TextView)history_Relativelayout.findViewById(R.id.tab_wotext);  
  119.                 textView1.setTextColor(getResources().getColor(R.color.black));  
  120.   
  121.                 TextView textView2 = (TextView)info_Relativelayout.findViewById(R.id.tab_wotext);  
  122.                 textView2.setTextColor(getResources().getColor(R.color.black));  
  123.   
  124.                 ImageView imageView = (ImageView)wode_Relativelayout.findViewById(R.id.tab_woicon);  
  125.                 imageView.setBackgroundResource(R.mipmap.tab_icon_pressed);  
  126.   
  127.                 ImageView imageView1 = (ImageView)history_Relativelayout.findViewById(R.id.tab_woicon);  
  128.                 imageView1.setBackgroundResource(R.mipmap.tabl_icon_normal);  
  129.   
  130.                 ImageView imageView2 = (ImageView)info_Relativelayout.findViewById(R.id.tab_woicon);  
  131.                 imageView2.setBackgroundResource(R.mipmap.tabp_icon_normal);  
  132.                 //把其他的切换卡设置成未点击的状态  
  133.                 break;  
  134.             case 1:  
  135.                 TextView textViewa = (TextView)wode_Relativelayout.findViewById(R.id.tab_wotext);  
  136.                 textViewa.setTextColor(getResources().getColor(R.color.black));  
  137.   
  138.                 TextView textViewb = (TextView)history_Relativelayout.findViewById(R.id.tab_wotext);  
  139.                 textViewb.setTextColor(getResources().getColor(R.color.button_green));  
  140.   
  141.                 TextView textViewc = (TextView)info_Relativelayout.findViewById(R.id.tab_wotext);  
  142.                 textViewc.setTextColor(getResources().getColor(R.color.black));  
  143.   
  144.                 ImageView imageViewa = (ImageView)wode_Relativelayout.findViewById(R.id.tab_woicon);  
  145.                 imageViewa.setBackgroundResource(R.mipmap.tab_icon_normal);  
  146.   
  147.                 ImageView imageViewb = (ImageView)history_Relativelayout.findViewById(R.id.tab_woicon);  
  148.                 imageViewb.setBackgroundResource(R.mipmap.tabl_icon_pressed);  
  149.   
  150.                 ImageView imageViewc = (ImageView)info_Relativelayout.findViewById(R.id.tab_woicon);  
  151.                 imageViewc.setBackgroundResource(R.mipmap.tabp_icon_normal);  
  152.                 break;  
  153.             case 2:  
  154.                 TextView textViewa1 = (TextView)wode_Relativelayout.findViewById(R.id.tab_wotext);  
  155.                 textViewa1.setTextColor(getResources().getColor(R.color.black));  
  156.   
  157.                 TextView textViewb1 = (TextView)history_Relativelayout.findViewById(R.id.tab_wotext);  
  158.                 textViewb1.setTextColor(getResources().getColor(R.color.black));  
  159.   
  160.                 TextView textViewc1 = (TextView)info_Relativelayout.findViewById(R.id.tab_wotext);  
  161.                 textViewc1.setTextColor(getResources().getColor(R.color.button_green));  
  162.   
  163.                 ImageView imageViewa1 = (ImageView)wode_Relativelayout.findViewById(R.id.tab_woicon);  
  164.                 imageViewa1.setBackgroundResource(R.mipmap.tab_icon_normal);  
  165.   
  166.                 ImageView imageViewb1 = (ImageView)history_Relativelayout.findViewById(R.id.tab_woicon);  
  167.                 imageViewb1.setBackgroundResource(R.mipmap.tabl_icon_normal);  
  168.   
  169.                 ImageView imageViewc1 = (ImageView)info_Relativelayout.findViewById(R.id.tab_woicon);  
  170.                 imageViewc1.setBackgroundResource(R.mipmap.tabp_icon_pressed);  
  171.                 break;  
  172.             default:  
  173.                 break;  
  174.   
  175.         }  
  176.     }  
  177.   
  178.     /** 
  179.      * @param view 
  180.      * 初始化底部布局中的控件 
  181.      */  
  182.     private void findView(View view) {  
  183.         wode_Relativelayout = (RelativeLayout)view.findViewById(R.id.wode_order);  
  184.         history_Relativelayout = (RelativeLayout)view.findViewById(R.id.history_order);  
  185.         info_Relativelayout = (RelativeLayout)view.findViewById(R.id.wode_info);  
  186.   
  187.     }  
  188. }  

四、在activity中使用我们自定义的底部控件。

 

 1、在activity中声明我们的控件

 

//自定义的底部切换控件
MyBottomLayout bottom;

 

 2、在onCreate()方法中初始化我们的控件,并为我们的控件设置我们定义好的回调方法。

 

bottom = (MyBottomLayout)findViewById(R.id.bottom);
bottom.setOnCallbackLister(new MycallbackLister());
3、在回调方法中,处理我们需要切换的页面,我这里是定义了三个不同的fragement,切换。

 

 

/**
 * 实现接口切换不同的页面
*/
private class MycallbackLister implements MyBottomLayout.ICallbackLister {

    @Override
    public void click(int id) {
        switch (id){
           case  R.id.wode_order:
               
               initPageFragement(new WodeTaskFragement());
                break;
            case R.id.history_order:
                
                initPageFragement(new HistoryTaskFragement());
                break;
            case R.id.wode_info:
              
                initPageFragement(new WodeInfoFragement());
                break;

            default:
                break;
        }
    }
}

 

五、我切换fragement的方法。有不懂fragement的可以查看写资料,过些天我也会写一些fragement的文章。

   

/*
* 动态的切换页面
* */
private void initPageFragement(Fragment fragment) {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    //替换传进来的fragement
    transaction.replace(R.id.my_content,fragment);
    transaction.commit();
}

 

 
 
posted @ 2017-03-12 19:53  天涯海角路  阅读(425)  评论(0)    收藏  举报