Android 网易顶部Bar

Android 网易顶部Bar_第1张图片


MainActivity.java

package com.xzq.wl;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;
    private TextView tv5;
    private TextView tv6;
    private ImageView iv;
    private int fromX;
    private int toX;
    private TranslateAnimation am;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        findViewById();
        processBiz();
        addListener();

    }

    private void addListener() {
        tv1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                move(v);
            }
        });

        tv2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                move(v);
            }
        });

        tv3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                move(v);
            }
        });

        tv4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                move(v);
            }
        });
        tv5.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                move(v);
            }
        });
        tv6.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                move(v);
            }
        });
    }

    private void findViewById() {
        tv1 = (TextView) findViewById(R.id.textView1);
        tv2 = (TextView) findViewById(R.id.textView2);
        tv3 = (TextView) findViewById(R.id.textView3);
        tv4 = (TextView) findViewById(R.id.textView4);
        tv5 = (TextView) findViewById(R.id.textView5);
        tv6 = (TextView) findViewById(R.id.textView6);
        iv = (ImageView) findViewById(R.id.imageView1);
    }

    private void processBiz() {
        ViewTreeObserver viewTreeObserver = tv1.getViewTreeObserver();
        if (viewTreeObserver.isAlive()) {
            viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                public void onGlobalLayout() {
                    tv1.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    int initX = tv1.getLeft() - (iv.getWidth() - tv1.getWidth()) / 2;
                    iv.setPadding(initX, 0, 0, 0);
                }
            });
        }
    }

    private void move(View v) {
        toX = ((RelativeLayout) v.getParent()).getLeft();
        am = new TranslateAnimation(fromX, toX, 0, 0);
        am.setDuration(200);
        am.setFillAfter(true);
        iv.clearAnimation();
        iv.startAnimation(am);
        fromX = toX;
    }

}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/big_button_up" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:contentDescription="@string/app_name"
            android:src="@drawable/slidebar" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:baselineAligned="false"
            android:orientation="horizontal" >

            <RelativeLayout
                android:id="@+id/layout1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1.0" >

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:padding="10dip"
                    android:text="@string/news" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/layout2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1.0" >

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:padding="10dip"
                    android:text="@string/sport" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/layout3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1.0" >

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:padding="10dip"
                    android:text="@string/fun" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/layout4"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1.0" >

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:padding="10dip"
                    android:text="@string/money" />
            </RelativeLayout>


            <RelativeLayout
                android:id="@+id/layout5"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1.0" >

                <TextView
                    android:id="@+id/textView5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:padding="10dip"
                    android:text="@string/tech" />
            </RelativeLayout>


            <RelativeLayout
                android:id="@+id/layout6"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1.0" >

                <TextView
                    android:id="@+id/textView6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:padding="10dip"
                    android:text="@string/more" />
            </RelativeLayout>
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">WangYi</string>
    <string name="news">头条</string>
    <string name="sport">体育</string>
    <string name="fun">娱乐</string>
    <string name="money">财经</string>
    <string name="tech">科技</string>
    <string name="more">更多</string>

</resources>


你可能感兴趣的:(Android 网易顶部Bar)