Android UI development Part 6-sliding tab like QQ

Source: Internet
Author: User

Public class mainactivity extends activitygroup {private relativelayout layout; private inclulayout1; private relativelayout layout2; private relativelayout layout3; private inclubodylayout; private imageview tab1; private imageview tab2; private imageview tab3; private imageview first; private int current = 1; // the first one is selected by default. This parameter value can be dynamically changed: 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); bodylayout = (relativelayout) findviewbyid (R. id. bodylayout); tab1 = (imageview) findviewbyid (R. ID. Tab1); tab1.setonclicklistener (onclicklistener); tab2 = (imageview) findviewbyid (R. id. tab2); tab2.setonclicklistener (onclicklistener); tab3 = (imageview) findviewbyid (R. id. tab3); tab3.setonclicklistener (onclicklistener); 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; break; Case 2: layout2.addview (first, rl); current = R. id. tab2; break; Case 3: layout3.addview (first, rl); current = R. id. tab3; break; default: break;} view = getlocalactivitymanager (). startactivity ("Index", new intent (mainactiv Ity. this, tab1.class )). getdecorview (); bodylayout. addview (View);} private Boolean isadd = false; // whether top_selectprivate int select_width has been added; // top_select_widthprivate int select_height; // top_select_heightprivate int firstleft; // The left margin after the first addition *** private int startleft; // The left margin of the Start *** // Add a view and remove a viewprivate void Replace () {Switch (current) {case R. id. tab1: changetop (layout1); break; case R. id. tab2: Chang Etop (layout2); break; case R. id. tab3: changetop (layout3); break; default: break;} private void changetop (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 () + (relativelay Out) Old. getparent ()). getleft (); RL. topmargin = old. gettop () + (relativelayout) Old. getparent ()). gettop (); // get the starting position 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 = new onclicklistene R () {public void onclick (view v) {If (! Isadd) {Replace (); // for the first time, use remove old to add a new top_select as isadd = true;} imageview top_select = (imageview) layout. findviewwithtag ("move"); int tableft; int endleft = 0; Boolean run = false; Switch (v. GETID () {case R. id. tab1: If (current! = R. id. tab1) {// The central location tableft = (relativelayout) tab1.getparent ()). getleft () + tab1.getleft () + tab1.getwidth ()/2; // The final position endleft = tableft-select_width/2; current = R. id. tab1; run = true; bodylayout. removeallviews (); view = getlocalactivitymanager (). startactivity ("Index", new intent (mainactivity. this, tab1.class )). getdecorview (); bodylayout. addview (View);} 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; bodylayout. removeallviews (); view = getlocalactivitymanager (). startactivity ("Index", new intent (mainactivity. this, tab2.class )). getdecorview (); bodylayout. addview (View);} 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; bodylayout. removeallviews (); view = getlocalactivitymanager (). startactivity ("Index", new intent (mainactivity. this, tab3.class )). getdecorview (); bodylayout. addview (View);} break; default: break;} If (run) {translateanimation animation = new translateanimation (startleft, endleft-firstleft, 0, 0 ); startleft = endleft-firstleft; // reset the starting position of animation. setduration (400); animation. setfillafter (true); top_select.bringtofront (); top_select.startanimation (animation );}}};}

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/root" android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayout android:orientation="vertical"android:layout_width="fill_parent" android:layout_height="fill_parent"android:background="@drawable/default_bg"><LinearLayout android:orientation="horizontal"android:layout_width="fill_parent" android:layout_height="fill_parent"android:layout_weight="6.0"><RelativeLayout android:id="@+id/layout1"android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_weight="1.0" android:layout_gravity="center_vertical"><ImageView android:id="@+id/tab1" android:layout_width="wrap_content"android:layout_height="wrap_content" android:src="@drawable/tab1"android:layout_centerInParent="true" /></RelativeLayout><RelativeLayout android:id="@+id/layout2"android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_weight="1.0" android:layout_gravity="center_vertical"><ImageView android:id="@+id/tab2" android:layout_width="wrap_content"android:layout_height="wrap_content" android:src="@drawable/tab2"android:layout_centerInParent="true" /></RelativeLayout><RelativeLayout android:id="@+id/layout3"android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_weight="1.0" android:layout_gravity="center_vertical"><ImageView android:id="@+id/tab3" android:layout_width="wrap_content"android:layout_height="wrap_content" android:src="@drawable/tab3"android:layout_centerInParent="true" /></RelativeLayout></LinearLayout><RelativeLayout android:orientation="horizontal"android:layout_width="fill_parent" android:layout_height="fill_parent"android:layout_weight="1.0" android:background="#ffffff"android:gravity="center" android:id="@+id/bodylayout"></RelativeLayout></LinearLayout></RelativeLayout> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.