Android 自定义Tabbar

由于需要自定义了一个组件,代码如下

自定义tabbar

public class TopTabbar extends LinearLayout implements OnClickListener {
 //namespace
 private final String nameSpace = "http://meiyitianabc.blog.163.com";
 //default for tab height .
 private int topContainerHeight = 40;
 //default for  content Layout .
 private int contentContainerHeight = 100;
 //tag .
 String TAG = TopTabbar.class.getName();
 //each tab item
 LinearLayout fistItem = null;
 LinearLayout secondItem = null;
 LinearLayout thirdItem = null;
 //tab title for each tab item
 TextView tv_title_1 = null;
 TextView tv_title_2 = null;
 TextView tv_title_3 = null;
 //the value for each tab text .
 private String title1;
 private String title2;
 private String tilte3;
 //
 LinearLayout contentContainer = null;
 //content container for each item clicked .
 View contentContainer1 = null;
 View contentContainer2 = null;
 View contentContainer3 = null;
 //click callback for  onClick .
 TabbarCallback tabOnClick = null;
 //
 LinearLayout topContainer = null;
 //bitmap 
 Bitmap bm_tabBg = null;
 //getter and setter methods .
 public TextView getTv_title_1() {
  return tv_title_1;
 }
 public TextView getTv_title_2() {
  return tv_title_2;
 }
 public TextView getTv_title_3() {
  return tv_title_3;
 }
 public LinearLayout getContentContainer() {
  return contentContainer;
 }
 public LinearLayout getTopContainer() {
  return topContainer;
 }
 public void setContentContainer1(View contentContainer1) {
  this.contentContainer1 = contentContainer1;
  this.contentContainer.addView(contentContainer1);
 }
 public void setContentContainer2(View contentContainer2) {
  this.contentContainer2 = contentContainer2;
  this.contentContainer.addView(contentContainer2);
 }
 public void setContentContainer3(View contentContainer3) {
  this.contentContainer3 = contentContainer3;
  this.contentContainer.addView(contentContainer3);
 }
 public void setTabOnClick(TabbarCallback tabOnClick) {
  this.tabOnClick = tabOnClick;
 }
 
 @Override
 public void addView(View child) {
  super.addView(child);
 }
 Context context = null;
 public TopTabbar(Context context,AttributeSet attr) {
  super(context);
  this.context = context;
  //
  this.setOrientation(LinearLayout.HORIZONTAL); 
  LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View view = inflater.inflate(R.layout.tabbar, this);
  int topContainerHeightRes = attr.getAttributeIntValue(nameSpace, "topContainerHeight", topContainerHeight);
  int contentContainerHeightRes = attr.getAttributeIntValue(nameSpace, "ContentContainerHeight", contentContainerHeight);
  //find top tab container ,then  set height .
  topContainer = (LinearLayout)view.findViewById(R.id.tabbar_layout_topContainer);
  topContainer.setMinimumHeight(topContainerHeightRes);
  topContainer.setId(R.string.tabContainer);
  //Find each item ,set id ,and use them  in activity code .
  fistItem = (LinearLayout) topContainer.findViewById(R.id.tabbar_linearlayout_fistItem);
  secondItem = (LinearLayout)topContainer.findViewById(R.id.tabbar_linearlayout_secondItem);
  thirdItem = (LinearLayout)topContainer.findViewById(R.id.tabbar_linearlayout_thirdItem);
  //set click event .
  fistItem.setOnClickListener(this);
  secondItem.setOnClickListener(this);
  thirdItem.setOnClickListener(this);
  //find each textView .
  tv_title_1 = (TextView)this.findViewById(R.id.tabbar_tv_title_1);
  tv_title_2 = (TextView)this.findViewById(R.id.tabbar_tv_title_2);
  tv_title_3 = (TextView)this.findViewById(R.id.tabbar_tv_title_3);
  //find content  container
  contentContainer = (LinearLayout)view.findViewById(R.id.tabbar_layout_ContentContainer);
  //set height .
  contentContainer.setMinimumHeight(contentContainerHeightRes);
  //find each content container .
  contentContainer1 = (LinearLayout)contentContainer.findViewById(R.id.tabbar_layout_ContentContainer_1);
  contentContainer2 = (LinearLayout)contentContainer.findViewById(R.id.tabbar_layout_ContentContainer_2);
  contentContainer3 = (LinearLayout)contentContainer.findViewById(R.id.tabbar_layout_ContentContainer_3);
  //set default view 
  //set whitch tab view should light .
  fistItem.setBackgroundResource(R.drawable.top_type_left_1);
  secondItem.setBackgroundResource(R.drawable.top_type_bg);
  thirdItem.setBackgroundResource(R.drawable.top_type_bg);
  //set whitch content view should visibile .
  contentContainer1.setVisibility(View.VISIBLE);
  contentContainer2.setVisibility(View.GONE);
  contentContainer3.setVisibility(View.GONE);
  //set id 
  int id = attr.getAttributeIntValue(nameSpace, "id", 0x12345);
  if(id==0x12345){
   Log.i("TopTabbar", "sorry ,TopTabbar id is not set , you can access it by default id 0x12345");
   this.setId(0x12345);
  }else{
   this.setId(id);
  }
 }
 @Override
 public void onClick(View v) {
  switch(v.getId()){
  case R.id.tabbar_linearlayout_fistItem:
   //set whitch tab view should light .
   fistItem.setBackgroundResource(R.drawable.top_type_left_1);
   secondItem.setBackgroundResource(R.drawable.top_type_bg);
   thirdItem.setBackgroundResource(R.drawable.top_type_bg);
   //set whitch content view should visibile .
   contentContainer1.setVisibility(View.VISIBLE);
   contentContainer2.setVisibility(View.GONE);
   contentContainer3.setVisibility(View.GONE);
   //set callback .
   tabOnClick.fistItemClickCallback();
   break;
  case R.id.tabbar_linearlayout_secondItem:
   //set whitch tab view should light .
   fistItem.setBackgroundResource(R.drawable.top_type_bg);
   secondItem.setBackgroundResource(R.drawable.top_type_middle_1);
   thirdItem.setBackgroundResource(R.drawable.top_type_bg);
   //set whitch content view should visibile .
   contentContainer1.setVisibility(View.GONE);
   contentContainer2.setVisibility(View.VISIBLE);
   contentContainer3.setVisibility(View.GONE);
   //set callback .
   tabOnClick.secondItemClickCallback();
   break;
  case R.id.tabbar_linearlayout_thirdItem:
   //set whitch tab view should light .
   fistItem.setBackgroundResource(R.drawable.top_type_bg);
   secondItem.setBackgroundResource(R.drawable.top_type_bg);
   thirdItem.setBackgroundResource(R.drawable.top_type_right_1);
   //set whitch content view should visibile .
   contentContainer1.setVisibility(View.GONE);
   contentContainer2.setVisibility(View.GONE);
   contentContainer3.setVisibility(View.VISIBLE);
   //set callback .
   tabOnClick.thirdItemClickCallback();
   break;
  }
 }
}

 

使用

 package com.appstore.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.appstore.ui.commctrls.BaseActivity;
import com.appstore.ui.commctrls.TabbarCallback;
import com.appstore.ui.commctrls.TopTabbar;
/**
 * test tabbar control .
 * @author zxy
 *
 */
public class TopTabbabTest extends BaseActivity implements TabbarCallback {

    /** Called when the activity is first created. */
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabviewtest);
        //id parameter is the same as id  you set in xml layout ,if not set you can  access it by default id 0x1234
        TopTabbar tb = (TopTabbar)findViewById(0x1234);
        //TopTabbar tb = new TopTabbar(this);
        if(tb==null){
         Log.i("toptabbar", "toptabbar is  null");
        }else{
         //set callback .
         tb.setTabOnClick(this);
         //set tab text
         tb.getTv_title_1().setText("tab1");
         tb.getTv_title_2().setText("tab2");
         tb.getTv_title_3().setText("tab3");
         //construct content view 
         LinearLayout contentView1 = new LinearLayout(this);
         TextView tv1 = new TextView(this);
         tv1.setText("fist content view .");
         contentView1.addView(tv1);
         contentView1.setVisibility(View.VISIBLE);
         
         LinearLayout contentView2 = new LinearLayout(this);
         TextView tv2 = new TextView(this);
         tv2.setText("second content view .");
         contentView2.addView(tv2);
         contentView2.setVisibility(View.GONE);
         
//         LinearLayout contentView3 = new LinearLayout(this);
//         TextView tv3 = new TextView(this);
//         tv3.setText("third  content view .");
//         contentView3.addView(tv3);
//         contentView3.setVisibility(View.GONE);
         
         //set content 
         tb.setContentContainer1(contentView1);
         tb.setContentContainer2(contentView2);
         tb.setContentContainer3(getLocalActivityManager().startActivity(
                    "firstpage",
                    new Intent(this, GalleryTest.class)
                            .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP))
                    .getDecorView());
        }
    }

    //event click on fistitem
 @Override
 public void fistItemClickCallback() {
  Toast.makeText(this,"hello ,you clicked fist item .", Toast.LENGTH_SHORT).show();
 }
 //event click on secondItem
 @Override
 public void secondItemClickCallback() {
  Toast.makeText(this,"hello ,you clicked second  item .", Toast.LENGTH_SHORT).show();
 }
 //event click on thirdItem
 @Override
 public void thirdItemClickCallback() {
  Toast.makeText(this,"hello ,you clicked third item .", Toast.LENGTH_SHORT).show();
 }
}

效果

Android 自定义Tabbar - meiyitianabc - 飞得更高!
posted @ 2012-08-27 13:56  GreyWolf  阅读(604)  评论(0编辑  收藏  举报