这篇文章主要为大家详细介绍了 Android 学习教程之高仿安卓微信 6.0 的相关资料, 具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Android 是一种基于 Linux 的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由 Google 公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用 "安卓" 或 "安致"。
wechat6Activity.java 的代码:
- package siso.geekworld;
- import android.app.Activity;
- 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.view.Menu;
- import android.view.View;
- import android.view.ViewConfiguration;
- import android.view.Window;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- import java.util.ArrayList;
- import java.util.List;
- import viewhelper.ChangeColorIconWithText;
- import viewhelper.TabFragment;
- public class wechat6Activity extends FragmentActivity implements View.OnClickListener,ViewPager.OnPageChangeListener{
- private ViewPager viewPager;
- private List<Fragment> mTabs = new ArrayList<>();
- private String[] mTitles = new String[]{"First Fragment","Second Fragment","Third Fragment","Fourth Fragment"};
- private FragmentPagerAdapter adapter;
- private List<ChangeColorIconWithText> mTabIndicators = new ArrayList<>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_wechat6);
- setOverflowButtonAlways();
- getActionBar().setDisplayShowHomeEnabled(false);
- initView();
- initDatas();
- initEvents();
- viewPager.setAdapter(adapter);
- }
- //初始化所有事件
- private void initEvents() {
- viewPager.addOnPageChangeListener(this);
- }
- //初始化所有数据
- private void initDatas() {
- for(String mtitle:mTitles){
- TabFragment tabFragment = new TabFragment();
- Bundle bundle = new Bundle();
- bundle.putString(TabFragment.TITLE,mtitle);
- tabFragment.setArguments(bundle);
- mTabs.add(tabFragment);
- }
- adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
- @Override
- public android.support.v4.app.Fragment getItem(int position) {
- return mTabs.get(position);
- }
- @Override
- public int getCount() {
- return mTabs.size();
- }
- };
- }
- //初始化所有view
- private void initView() {
- viewPager = (ViewPager)findViewById(R.id.id_viewpager);
- ChangeColorIconWithText one = (ChangeColorIconWithText)findViewById(R.id.id_indicator_one);
- ChangeColorIconWithText two = (ChangeColorIconWithText)findViewById(R.id.id_indicator_two);
- ChangeColorIconWithText three = (ChangeColorIconWithText)findViewById(R.id.id_indicator_three);
- ChangeColorIconWithText four = (ChangeColorIconWithText)findViewById(R.id.id_indicator_four);
- mTabIndicators.add(one);
- mTabIndicators.add(two);
- mTabIndicators.add(three);
- mTabIndicators.add(four);
- one.setOnClickListener(this);
- two.setOnClickListener(this);
- three.setOnClickListener(this);
- four.setOnClickListener(this);
- one.setIconAlpha(1.0f);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- //应用反射改变OverflowButton的图标
- private void setOverflowButtonAlways(){
- try {
- ViewConfiguration config = ViewConfiguration.get(this);
- Field menuKey = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
- menuKey.setAccessible(true);
- menuKey.setBoolean(config, false);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- //设置menu显示icon
- @Override
- public boolean onMenuOpened(int featureId, Menu menu) {
- if(featureId== Window.FEATURE_ACTION_BAR&&menu!=null){
- if(menu.getClass().getSimpleName().equals("MenuBuilder")){
- try {
- Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
- method.setAccessible(true);
- method.invoke(menu,true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- return super.onMenuOpened(featureId, menu);
- }
- @Override
- public void onClick(View v) {
- resetOtherTabs();
- switch (v.getId()){
- case R.id.id_indicator_one:
- mTabIndicators.get(0).setIconAlpha(1.0f);
- viewPager.setCurrentItem(0,false);
- break;
- case R.id.id_indicator_two:
- mTabIndicators.get(1).setIconAlpha(1.0f);
- viewPager.setCurrentItem(1, false);
- break;
- case R .id.id_indicator_three:
- mTabIndicators.get(2).setIconAlpha(1.0f);
- viewPager.setCurrentItem(2, false);
- break;
- case R.id.id_indicator_four:
- mTabIndicators.get(3).setIconAlpha(1.0f);
- viewPager.setCurrentItem(3, false);
- break;
- }
- }
- private void resetOtherTabs() {
- for(int i=0;i<mTabIndicators.size();i++){
- mTabIndicators.get(i).setIconAlpha(0);
- }
- }
- @Override
- public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
- if(positionOffset>0){
- ChangeColorIconWithText left = mTabIndicators.get(position);
- ChangeColorIconWithText right = mTabIndicators.get(position+1);
- left.setIconAlpha(1-positionOffset);
- right.setIconAlpha(positionOffset);
- }
- }
- @Override
- public void onPageSelected(int position) {
- }
- @Override
- public void onPageScrollStateChanged(int state) {
- }
- }
ChangeColorIconWithText.java 代码:
TabFragment.java 代码:
- package viewhelper;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- public class TabFragment extends android.support.v4.app.Fragment {
- private String mTitle = "DEFAULT";
- public static final String TITLE = "title";
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- if(getArguments()!=null){
- mTitle = getArguments().getString(TITLE);
- }
- TextView tv = new TextView(getActivity());
- tv.setText(mTitle);
- tv.setTextSize(20);
- tv.setGravity(Gravity.CENTER);
- tv.setBackgroundColor(Color.parseColor("#ffffffff"));
- return tv;
- }
- }
activity_wechat6.xml 内容:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:hymen="http://schemas.android.com/apk/res-auto"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
- <android.support.v4.view.ViewPager
- android:id="@+id/id_viewpager"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1">
- </android.support.v4.view.ViewPager>
- <LinearLayout
- android:orientation="horizontal"
- android:background="@drawable/tab_bg"
- android:layout_width="match_parent"
- android:layout_height="60dp">
- <viewhelper.ChangeColorIconWithText
- android:id="@+id/id_indicator_one"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:padding="5dp"
- hymen:micon="@drawable/ic_menu_start_conversation"
- hymen:mcolor="#ff45c01a"
- hymen:text_size="12sp"
- hymen:text="@string/app_name"
- />
- <viewhelper.ChangeColorIconWithText
- android:id="@+id/id_indicator_two"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:padding="5dp"
- hymen:micon="@drawable/ic_menu_friendslist"
- hymen:mcolor="#ff45c01a"
- hymen:text_size="12sp"
- hymen:text="@string/tab_contact"
- />
- <viewhelper.ChangeColorIconWithText
- android:id="@+id/id_indicator_three"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:padding="5dp"
- hymen:mcolor="#ff45c01a"
- hymen:micon="@drawable/ic_menu_emoticons"
- hymen:text="@string/tab_found"
- hymen:text_size="12sp" />
- <viewhelper.ChangeColorIconWithText
- android:id="@+id/id_indicator_four"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:padding="5dp"
- hymen:mcolor="#ff45c01a"
- hymen:micon="@drawable/ic_menu_allfriends"
- hymen:text="@string/tab_me"
- hymen:text_size="12sp" />
- </LinearLayout>
- </LinearLayout>
strings.xml 内容:
- <resources>
- <string name="app_name">
- 微信
- </string>
- <string name="action_search">
- 查找
- </string>
- <string name="action_add">
- 添加
- </string>
- <string name="menu_group_chat">
- 发起群聊
- </string>
- <string name="menu_feedback">
- 意见反馈
- </string>
- <string name="menu_addfriend">
- 添加朋友
- </string>
- <string name="menu_scan">
- 扫一扫
- </string>
- <string name="tab_contact">
- 通讯录
- </string>
- <string name="tab_found">
- 发现
- </string>
- <string name="tab_me">
- 我
- </string>
- </resources>
main.xml 内容 :
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android"
- >
- <item
- android:id="@+id/action_search"
- android:icon="@drawable/actionbar_search_icon"
- android:actionViewClass="android.widget.SearchView"
- android:showAsAction="ifRoom|collapseActionView"
- android:title="@string/action_search"/>
- <item
- android:id="@+id/action_group_chat"
- android:icon="@drawable/menu_group_chat_icon"
- android:title="@string/menu_group_chat"/>
- <item
- android:id="@+id/action_add_friend"
- android:icon="@drawable/menu_add_icon"
- android:title="@string/menu_addfriend"/>
- <item
- android:id="@+id/action_scan"
- android:icon="@drawable/men_scan_icon"
- android:title="@string/menu_scan"/>
- <item
- android:id="@+id/action_feedback"
- android:icon="@drawable/menu_feedback_icon"
- android:title="@string/menu_feedback"/>
- </menu>
styles.xml 内容:
- <resources>
- <!-- Base application theme. -->
- <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
- <!-- Customize your theme here. -->
- <item name="colorPrimary">
- @color/colorPrimary
- </item>
- <item name="colorPrimaryDark">
- @color/colorPrimaryDark
- </item>
- <item name="colorAccent">
- @color/colorAccent
- </item>
- <item name="android:actionOverflowButtonStyle">
- @style/WeiXinOverflowButtonStyle
- </item>
- </style>
- <style name="WeiXinOverflowButtonStyle">
- <item name="android:src">
- @drawable/actionbar_add_icon
- </item>
- </style>
- <!-- Application theme. -->
- <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
- <!-- <style name="AppTheme" parent="AppBaseTheme">-->
- <!-- All customizations that are NOT specific to a particular API-level
- can go here. -->
- </style>
- </resources>
drawable 资源:
运行结果如图:
来源: http://www.phperz.com/article/17/0316/307148.html