第一步:先设计好布局,我弄了四个小RelativeLayout,每个就是一个Tab,里面包含一个imageview和一个button,其button可以用textview代替。
代码:
第二步:初始化。
代码:
private void initViews(){
View searchLayout=findViewById(R.id.relative_title1);
View linkLayout=findViewById(R.id.relative_title2);
View addLayout=findViewById(R.id.relative_title3);
View editLayout=findViewById(R.id.relative_title4);
searchLayout.setOnClickListener(this);
linkLayout.setOnClickListener(this);
addLayout.setOnClickListener(this);
editLayout.setOnClickListener(this);
}
第三步:点击事件。
代码:
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
switch (v.getId()) {
case R.id.relative_title1:
setTabSelection(0);
break;
case R.id.relative_title2:
setTabSelection(1);
break;
case R.id.relative_title3:
setTabSelection(2);
break;
case R.id.relative_title4:
setTabSelection(3);
break;
default:
break;
}
}
private void setTabSelection(int index) {
switch (index){
case 0:
//点击第一个Tab时要做的事
break;
case 1:
//点击第二个Tab时要做的事
break;
case 2:
//点击第三个Tab时要做的事
break;
case 3:
//点击第四个Tab时要做的事
break;
default:
break;
}
}
效果图: