<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/headLayout"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="vertical">
<!-- 标题文字 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="horizontal" >
<!-- 聊天 -->
<TextView
android:id="@+id/chatTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@string/chatText"
android:textSize="16sp"
android:textColor="@color/text_dark"/>
<!-- 发现 -->
<TextView
android:id="@+id/foundTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@string/foundText"
android:textSize="16sp"
android:textColor="@color/text_dark"/>
<!-- 通讯录 -->
<TextView
android:id="@+id/contactsTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="@string/contactsText"
android:textSize="16sp"
android:textColor="@color/text_dark" />
</LinearLayout>
<!-- 选中标识 -->
<ImageView
android:id="@+id/tabBottomLine"
android:layout_width="wrap_content"
android:layout_height="3dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@color/text_green"
android:contentDescription="@string/app_name" />
</RelativeLayout>
<!-- 分隔线 -->
<View
android:id="@+id/dividerLine"
android:layout_below="@+id/headLayout"
android:background="@color/gray_light"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<!-- 页面容器 -->
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_below="@+id/dividerLine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:flipInterval="300"
android:persistentDrawingCache="animation" />
</RelativeLayout>下面为主工程代码:package com.example.wechatsample;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewConfiguration;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* 高仿微信主界面
*/
public class MainActivity extends FragmentActivity implements OnClickListener, OnPageChangeListener
{
private TextView[] tabTextView = null;
private ImageView tabBottomLine = null;
private ViewPager viewPager = null;
private List<Fragment> listFragments = null;
private FragmentPagerAdapter fmPagerAdapter = null;
private int tabWidth = 0;
private int curTabIndex = VIEW_ID_CHAT;
private static final int VIEW_ID_CHAT = 0;
private static final int VIEW_ID_FOUND = 1;
private static final int VIEW_ID_CONTACTS = 2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setOverflowShowingAlways();
// 初始化tab文字
tabTextView = new TextView[3];
tabTextView[VIEW_ID_CHAT] = (TextView)findViewById(R.id.chatTextView);
tabTextView[VIEW_ID_FOUND] = (TextView)findViewById(R.id.foundTextView);
tabTextView[VIEW_ID_CONTACTS] = (TextView)findViewById(R.id.contactsTextView);
tabTextView[VIEW_ID_CHAT].setOnClickListener(this);
tabTextView[VIEW_ID_FOUND].setOnClickListener(this);
tabTextView[VIEW_ID_CONTACTS].setOnClickListener(this);
// 初始化tab标识线
tabBottomLine = (ImageView)findViewById(R.id.tabBottomLine);
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams)tabBottomLine.getLayoutParams();
DisplayMetrics dm = getResources().getDisplayMetrics();
lParams.width = dm.widthPixels / 3;
tabBottomLine.setLayoutParams(lParams);
tabWidth = lParams.width;
// 初始化页面
listFragments = new ArrayList<Fragment>();
listFragments.add(new ChatFragment());
listFragments.add(new FoundFragment());
listFragments.add(new ContactsFragment());
viewPager = (ViewPager)findViewById(R.id.viewPager);
fmPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager())
{
@Override
public int getCount()
{
return listFragments.size();
}
@Override
public Fragment getItem(int index)
{
return listFragments.get(index);
}
};
viewPager.setAdapter(fmPagerAdapter);
viewPager.setOnPageChangeListener(this);
viewPager.setCurrentItem(VIEW_ID_CHAT);
updateTabTextStatus(VIEW_ID_CHAT);
}
// 更新tab文字选中状态
private void updateTabTextStatus(int index)
{
for (TextView tv : tabTextView)
{
tv.setTextColor(Color.parseColor("#2c2c2c"));
}
tabTextView[index].setTextColor(Color.parseColor("#45c01a"));
}
// 切换tab选项
private void changeTabItem(int index)
{
Animation animation = new TranslateAnimation(curTabIndex*tabWidth , index*tabWidth, 0, 0);
curTabIndex = index;
animation.setFillAfter(true);
animation.setDuration(300);
tabBottomLine.startAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener()
{
@Override
public void onAnimationStart(Animation animation)
{
}
@Override
public void onAnimationRepeat(Animation animation)
{
}
@Override
public void onAnimationEnd(Animation animation)
{
// 动画结束,更新文字选中状态
updateTabTextStatus(curTabIndex);
}
});
}
@Override
public void onPageSelected(int position)
{
// 左右滑动切换tab页
changeTabItem(position);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
}
@Override
public void onPageScrollStateChanged(int state)
{
}
@Override
public void onClick(View v)
{
// 点击切换tab页
switch (v.getId())
{
case R.id.chatTextView:
tabTextView[VIEW_ID_CHAT].setTextColor(Color.parseColor("#8a8a8a"));
viewPager.setCurrentItem(VIEW_ID_CHAT);
break;
case R.id.foundTextView:
tabTextView[VIEW_ID_FOUND].setTextColor(Color.parseColor("#8a8a8a"));
viewPager.setCurrentItem(VIEW_ID_FOUND);
break;
case R.id.contactsTextView:
tabTextView[VIEW_ID_CONTACTS].setTextColor(Color.parseColor("#8a8a8a"));
viewPager.setCurrentItem(VIEW_ID_CONTACTS);
break;
default:
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onMenuOpened(int featureId, Menu menu)
{
if (featureId == Window.FEATURE_ACTION_BAR && menu != null)
{
if (menu.getClass().getSimpleName().equals("MenuBuilder"))
{
try
{
Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
}
catch (Exception e)
{
}
}
}
return super.onMenuOpened(featureId, menu);
}
private void setOverflowShowingAlways()
{
try
{
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
原文地址:http://blog.csdn.net/grafx/article/details/38944387