android-仿QQtab
生活随笔
收集整理的這篇文章主要介紹了
android-仿QQtab
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.TranslateAnimation; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.RelativeLayout.LayoutParams; import android.widget.TextView; public class QQTabActivity extends Activity { /** Called when the activity is first created. */ private RelativeLayout layout; private RelativeLayout layout1; private RelativeLayout layout2; private RelativeLayout layout3; private TextView text; private ImageView tab1; private ImageView tab2; private ImageView tab3; private ImageView first; private int current = 1; // 默認選中第一個,可以動態(tài)的改變此參數(shù)值 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initUI(); } private void initUI(){ layout = (RelativeLayout) findViewById(R.id.root); layout1 = (RelativeLayout) findViewById(R.id.layout1); layout2 = (RelativeLayout) findViewById(R.id.layout2); layout3 = (RelativeLayout) findViewById(R.id.layout3); tab1 = (ImageView) findViewById(R.id.tab1); tab2 = (ImageView) findViewById(R.id.tab2); tab3 = (ImageView) findViewById(R.id.tab3); text = (TextView) findViewById(R.id.text); tab1.setOnClickListener(listener); tab2.setOnClickListener(listener); tab3.setOnClickListener(listener); RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); rl.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE); first = new ImageView(this); first.setTag("first"); first.setImageResource(R.drawable.topbar_select); switch (current) { case 1: layout1.addView(first,rl); current = R.id.tab1; text.setText("最近聯(lián)系人"); break; case 2: layout2.addView(first,rl); current = R.id.tab2; text.setText("我的聯(lián)系人"); break; case 3: layout3.addView(first,rl); current = R.id.tab3; text.setText("我的QQ群聯(lián)系人"); break; default: break; } } private boolean isAdd = false;// 是否添加過 top_select private int select_width; // top_select_width private int select_height; // top_select_height private int firstLeft; // 第一次添加后的左邊距***** private int startLeft; // 起始左邊距 // 添加一個view,移除一個view private void replace(){ switch (current) { case R.id.tab1: changeTop(layout1); break; case R.id.tab2: changeTop(layout2); break; case R.id.tab3: changeTop(layout3); break; default: break; } } private void changeTop(RelativeLayout relativeLayout){ ImageView old = (ImageView) relativeLayout.findViewWithTag("first"); select_width =old.getWidth(); select_height = old.getHeight(); RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(select_width, select_height); rl.leftMargin = old.getLeft()+((RelativeLayout)old.getParent()).getLeft(); rl.topMargin = old.getTop()+((RelativeLayout)old.getParent()).getTop(); // 獲取起始位置 firstLeft = old.getLeft()+((RelativeLayout)old.getParent()).getLeft(); ImageView iv = new ImageView(this); iv.setTag("move"); iv.setImageResource(R.drawable.topbar_select); layout.addView(iv,rl); relativeLayout.removeView(old); } private OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(!isAdd){ replace();// // 初次使用移除old 添加新的top_select為RelativeLayout所使用 isAdd = true; } ImageView top_select = (ImageView) layout.findViewWithTag("move"); int tabLeft = 0; int endLeft=0; boolean run = false; switch (v.getId()) { case R.id.tab1: if(current!=R.id.tab1) {// 中心位置 tabLeft = ((RelativeLayout)tab1.getParent()).getLeft()+tab1.getLeft()+tab1.getWidth()/2; // 最終位置 endLeft = tabLeft-select_width/2; current = R.id.tab1; run = true; text.setText("最近聯(lián)系人"); } break; case R.id.tab2: if(current!=R.id.tab2){ // 中心位置 tabLeft = ((RelativeLayout)tab2.getParent()).getLeft()+tab2.getLeft()+tab2.getWidth()/2; // 最終位置 endLeft = tabLeft-select_width/2; current = R.id.tab2; run = true; text.setText("我的聯(lián)系人"); } break; case R.id.tab3: if(current!=R.id.tab3){ // 中心位置 tabLeft = ((RelativeLayout)tab3.getParent()).getLeft()+tab3.getLeft()+tab3.getWidth()/2; // 最終位置 endLeft = tabLeft-select_width/2; current = R.id.tab3; run = true; text.setText("我的QQ群聯(lián)系人"); } break; default: break; } if(run){ TranslateAnimation animation = new TranslateAnimation(startLeft, endLeft-firstLeft, 0, 0);//水平移動 startLeft = endLeft -firstLeft; 重新設(shè)定起始位置 animation.setDuration(400); animation.setFillAfter(true); top_select.bringToFront(); top_select.startAnimation(animation); } } }; }
總結(jié)
以上是生活随笔為你收集整理的android-仿QQtab的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑常见问题交流
- 下一篇: 鼠标里面失灵怎么修复 鼠标故障如何修复