android 仿微信demo——微信主界面实现

以往文章中实现微信启动页,登录注册功能,此基础上继续完善仿微信功能。

主界面实现

(1)整体采用RelativeLayout相对布局

(2)最上面是toolbar操作栏,搜索框SearchView,Overflow(含有4个单选菜单项)

(3)中间使用Fragment组件(不使用ViewPager,有兴趣可以自己添加实现下)。

(4)最下面是水平的LinearLayout线性布局:含有4个自定义的控件

这一篇主要是实现主界面,其他像顶部(toolbar,SearchView,Overflow),中间的fragment,后续文章在更新。

创建主界面activity MainWeixin.java

MainWeixin.java

package com.example.wxchatdemo;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

public class MainWeixin extends FragmentActivity implements View.OnClickListener {
    private WeixinFragment firstFragment = null;// 用于显示微信界面
    private ContactListFragment secondFragment = null;// 用于显示通讯录界面
    private FindFragment thirdFragment = null;// 用于显示发现界面
    private SelfFragment fourthFragment = null;// 用于显示我界面
    private View firstLayout = null;// 微信显示布局
    private View secondLayout = null;// 通讯录显示布局
    private View thirdLayout = null;// 发现显示布局
    private View fourthLayout = null;// 我显示布局
    /*声明组件变量*/
    private ImageView weixinImg = null;
    private ImageView contactImg = null;
    private ImageView findImg = null;
    private ImageView selfImg = null;
    private TextView weixinText = null;
    private TextView contactText = null;
    private TextView  findText = null;
    private TextView selfText = null;
    private FragmentManager fragmentManager = null;// 用于对Fragment进行管理
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//要求窗口没有title
        super.setContentView(R.layout.main_weixin);
        // 初始化布局元素
        initViews();
        fragmentManager = getFragmentManager();//用于对Fragment进行管理
        // 设置默认的显示界面
        setTabSelection(0);
    }
    /**
     * 在这里面获取到每个需要用到的控件的实例,并给它们设置好必要的点击事件
     */
    @SuppressLint("NewApi")
    public void initViews() {
        fragmentManager = getFragmentManager();
        firstLayout = findViewById(R.id.weixin_layout);
        secondLayout = findViewById(R.id.contacts_layout);
        thirdLayout = findViewById(R.id.find_layout);
        fourthLayout = findViewById(R.id.self_layout);
        weixinImg = (ImageView) findViewById(R.id.weixin_img);
        contactImg = (ImageView) findViewById(R.id.contact_img);
        findImg = (ImageView) findViewById(R.id.find_img);
        selfImg = (ImageView) findViewById(R.id.self_img);
        weixinText = (TextView) findViewById(R.id.weixin_text);
        contactText = (TextView) findViewById(R.id.contact_text);
        findText = (TextView) findViewById(R.id.find_text);
        selfText = (TextView) findViewById(R.id.self_text);
        //处理点击事件
        firstLayout.setOnClickListener(this);
        secondLayout.setOnClickListener(this);
        thirdLayout.setOnClickListener(this);
        fourthLayout.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.weixin_layout:
                setTabSelection(0);// 当点击了微信时,选中第1个tab
                break;
            case R.id.contacts_layout:
                setTabSelection(1);// 当点击了通讯录时,选中第2个tab
                break;
            case R.id.find_layout:
                setTabSelection(2);// 当点击了发现时,选中第3个tab
                break;
            case R.id.self_layout:
                setTabSelection(3);// 当点击了我时,选中第4个tab
                break;
            default:
                break;
        }
    }
    /**
     * 根据传入的index参数来设置选中的tab页 每个tab页对应的下标。0表示微信,1表示通讯录,2表示发现,3表示我
     */
    @SuppressLint("NewApi")
    private void setTabSelection(int index) {
        clearSelection();// 每次选中之前先清除掉上次的选中状态
        FragmentTransaction transaction = fragmentManager.beginTransaction();// 开启一个Fragment事务
        hideFragments(transaction);// 先隐藏掉所有的Fragment,以防止有多个Fragment显示在界面上的情况
        switch (index) {
            case 0:
                // 当点击了我的微信tab时改变控件的图片和文字颜色
                weixinImg.setImageResource(R.drawable.tab_weixin_pressed);//修改布局中的图片
                weixinText.setTextColor(Color.parseColor("#0090ff"));//修改字体颜色
                if (firstFragment == null) {
                    /*获取登录activity传过来的微信号*/
                    Intent intent = getIntent();
                    String number = intent.getStringExtra("weixin_number");
                    // 如果FirstFragment为空,则创建一个并添加到界面上
                    firstFragment = new WeixinFragment(number);
                    transaction.add(R.id.fragment, firstFragment);
                } else {
                    // 如果FirstFragment不为空,则直接将它显示出来
                    transaction.show(firstFragment);//显示的动作
                }
                break;
            // 以下和firstFragment类同
            case 1:
                contactImg.setImageResource(R.drawable.tab_address_pressed);
                contactText.setTextColor(Color.parseColor("#0090ff"));
                if (secondFragment == null) {
                    /*获取登录activity传过来的微信号*/
                    Intent intent = getIntent();
                    String number = intent.getStringExtra("weixin_number");
                    secondFragment = new ContactListFragment(number);
                    transaction.add(R.id.fragment, secondFragment);
                } else {
                    transaction.show(secondFragment);
                }
                break;
            case 2:
                findImg.setImageResource(R.drawable.tab_find_frd_pressed);
                findText.setTextColor(Color.parseColor("#0090ff"));
                if (thirdFragment == null) {
                    thirdFragment = new FindFragment();
                    transaction.add(R.id.fragment, thirdFragment);
                } else {
                    transaction.show(thirdFragment);
                }
                break;
            case 3:
                selfImg.setImageResource(R.drawable.tab_settings_pressed);
                selfText.setTextColor(Color.parseColor("#0090ff"));
                if (fourthFragment == null) {
                    fourthFragment = new SelfFragment();
                    transaction.add(R.id.fragment, fourthFragment);
                } else {
                    transaction.show(fourthFragment);
                }
                break;
        }
        transaction.commit();
    }
    /**
     * 清除掉所有的选中状态
     */
    private void clearSelection() {
        weixinImg.setImageResource(R.drawable.tab_weixin_normal);
        weixinText.setTextColor(Color.parseColor("#82858b"));
        contactImg.setImageResource(R.drawable.tab_address_normal);
        contactText.setTextColor(Color.parseColor("#82858b"));
        findImg.setImageResource(R.drawable.tab_find_frd_normal);
        findText.setTextColor(Color.parseColor("#82858b"));
        selfImg.setImageResource(R.drawable.tab_settings_normal);
        selfText.setTextColor(Color.parseColor("#82858b"));
    }
    /**
     * 将所有的Fragment都设置为隐藏状态 用于对Fragment执行操作的事务
     */
    @SuppressLint("NewApi")
    private void hideFragments(FragmentTransaction transaction) {
        if (firstFragment != null) {
            transaction.hide(firstFragment);
        }
        if (secondFragment != null) {
            transaction.hide(secondFragment);
        }
        if (thirdFragment != null) {
            transaction.hide(thirdFragment);
        }
        if (fourthFragment != null) {
            transaction.hide(fourthFragment);
        }
    }
    //封装一个AlertDialog
    private void exitDialog() {
        Dialog dialog = new AlertDialog.Builder(this)
                .setTitle("温馨提示")
                .setMessage("您确定要退出程序吗?")
                .setPositiveButton("关闭微信", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        finish();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                    }
                }).create();
        dialog.show();//显示对话框
    }
    /**
     * 返回菜单键监听事件
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {//如果是返回按钮
            exitDialog();
        }
        return super.onKeyDown(keyCode, event);
    }
}

创建主界面activity MainWeixin.java对应的主布局文件main_weixin.xml

main_weixin.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">
    </FrameLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:background="#f7f7f7"
        android:gravity="center"
        android:orientation="horizontal">
        <LinearLayout
            android:id="@+id/weixin_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_weight="1"
            android:orientation="vertical"
            android:padding="3dp">
            <ImageView
                android:id="@+id/weixin_img"
                android:layout_width="wrap_content"
                android:layout_height="24dp"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/tab_weixin_pressed" />
            <TextView
                android:id="@+id/weixin_text"
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:layout_gravity="center_horizontal"
                android:gravity="top"
                android:text="微信"
                android:textColor="#82858b"
                android:textSize="13dp" />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/contacts_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_weight="1"
            android:orientation="vertical"
            android:padding="3dp">
            <ImageView
                android:id="@+id/contact_img"
                android:layout_width="wrap_content"
                android:layout_height="24dp"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/tab_address_normal" />
            <TextView
                android:id="@+id/contact_text"
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:layout_gravity="center_horizontal"
                android:gravity="top"
                android:text="通讯录"
                android:textColor="#82858b"
                android:textSize="13dp" />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/find_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_weight="1"
            android:orientation="vertical"
            android:padding="3dp">
            <ImageView
                android:id="@+id/find_img"
                android:layout_width="wrap_content"
                android:layout_height="24dp"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/tab_find_frd_normal" />
            <TextView
                android:id="@+id/find_text"
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:layout_gravity="center_horizontal"
                android:gravity="top"
                android:text="发现"
                android:textColor="#82858b"
                android:textSize="13dp" />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/self_layout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_weight="1"
            android:orientation="vertical"
            android:padding="3dp">
            <ImageView
                android:id="@+id/self_img"
                android:layout_width="wrap_content"
                android:layout_height="24dp"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/tab_settings_normal" />
            <TextView
                android:id="@+id/self_text"
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:layout_gravity="center_horizontal"
                android:gravity="top"
                android:text="我"
                android:textColor="#82858b"
                android:textSize="13dp" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

创建4个Fragment.class和4个Fragment.xml布局,对应微信,通讯录,发现,我

WeixinFragment.java

package com.example.wxchatdemo;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("ValidFragment")
public class WeixinFragment extends Fragment {
    private String number;
    @SuppressLint("ValidFragment")
    WeixinFragment(String number) {
        this.number = number;
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.weixin_fragment, container, false);
        return view;
    }
}

ContactListFragment.java

package com.example.wxchatdemo;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("ValidFragment")
public class ContactListFragment extends Fragment {
    private String number;
    @SuppressLint("ValidFragment")
    ContactListFragment(String number) {
        this.number = number;
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.weixin_fragment, container, false);
        return view;
    }
}

FindFragment.java

package com.example.wxchatdemo;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FindFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.find_fragment, container, false);
        return view;
    }
}

SelfFragment.java

package com.example.wxchatdemo;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SelfFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.self_fragment, container, false);
        return view;
    }
}

创建四个fragmen布局,代码都一样,只要有就行,后面会完善的,四个fragment布局文件为,weixin_fragment.xml,contactlist_fragment.xml,find_fragment.xml,self_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</LinearLayout>

在AndroidMainfest.xml中声明创建的activity,由于创建fragment不是activity,所有不用声明,声明主界面的activity即可(fragment是内嵌在activity中的)

测试

把之前两个登录activity请求成功跳转的activity代码段注释取消掉,启动项目测试

总结

这篇关于微信demo的文章就到这里了,希望大家可以多多关注我们的更多精彩内容!

(0)

相关推荐

  • android 仿微信demo——登录功能实现(移动端)

    移动端登录功能实现 登录功能基本和注册一样,唯一不同的是登录可以实现两种登录方式(微信号和手机号),也就是布局不一样.所以需要两个布局,两个activity(这个方法比较简单粗暴,我懒.也可以通过activity动态切换布局,这样只需要一个activity就可以了) 创建两个activity,实现两种登录方式 微信号登录activity LoginUser.java package com.example.wxchatdemo; import android.annotation.Suppres

  • android 仿微信demo——微信启动界面实现

    微信启动界面 创建项目 android studio创建移动端项目 微信启动界面实现 当第一次点击微信时会看到微信出现启动界面(不包括两个按钮)停留大概一秒的时间,然后才进入包括两个按钮的启动界面.按钮在没有获取和获取焦点时都有不同的图片显示,所以下面要实现这些功能 创建两个activity其对应的布局,一个activity显示停留的界面(布局就是一张图片),另一个activity显示真正的启动界面(布局包括图片及两个按钮),创建两个selector文件实现按钮在没有获取和获取焦点时显示不同图片

  • android 仿微信demo——注册功能实现(移动端)

    移动端注册功能实现 微信的注册界面每一个文本段都有下划线且默认颜色都是灰色,当其中一个文本段获取焦点会将下划线的颜色变为绿色,而且文本输入框的光标也是绿色的,还有在文本输入框没有全部输入的情况下,按钮是不能点击的,只有当文本输入框全部输入的情况下才能点击且此时按钮会变成绿色.除了这些UI功能外,当点击注册按钮是还会把表单数据发送给服务器 创建activity Reigister.java activity Reigister.java package com.example.wxchatdemo

  • android 仿微信demo——微信消息界面实现(移动端)

    目录 移动端微信消息页实现 总结 移动端微信消息页实现 在上一篇中主界面实现说过微信四个页面中间都是是fragment的,并且四个fragment的布局都还没实现,所以这一篇主要实现微信消息界面的实现(第一个fragment) 微信消息页是可以上下滑动,每一个列表最多都有可显示五个数据,还可以点击列表 要实现上诉功能只需要在fragment布局中使用ListView,然后给ListView指定一个Item布局即可 修改微信消息界面fragment布局 weixin_fragment.xml <L

  • android 仿微信demo——登录功能实现(服务端)

    上一篇文章实现了微信登录的移动端功能,下面继续完善功能,实现微信登录服务端功能 服务端登录功能实现 在以往文章里已经实现了服务端mvc框架,而登录和注册是类似,所以只需要在web层创建一个Servlet用于和客户端完成数据交互且在service层和dao层中在相应的接口添加相应的抽象方法,然后再实现类中重写就好了. 创建Servlet Login.java,实现服务端和客户端的数据交互 Login.java package com.example.controller; import com.a

  • android 仿微信demo——注册功能实现(服务端)

    服务端注册功能实现 通过web层完成客户端和服务端的数据交互(接受数据,发送数据),service层完成业务逻辑(注册,登录),dao层操作数据库(要借助工具类) 创建项目 idea创建服务端项目 配置tomcat服务器 启动项目测试服务器 创建web层和客户端完成数据交互 创建Servlet Reigister.java Reigister.java package com.example.controller; import com.alibaba.fastjson.JSON; import

  • android 仿微信demo——微信主界面实现

    以往文章中实现微信启动页,登录注册功能,此基础上继续完善仿微信功能. 主界面实现 (1)整体采用RelativeLayout相对布局 (2)最上面是toolbar操作栏,搜索框SearchView,Overflow(含有4个单选菜单项) (3)中间使用Fragment组件(不使用ViewPager,有兴趣可以自己添加实现下). (4)最下面是水平的LinearLayout线性布局:含有4个自定义的控件 这一篇主要是实现主界面,其他像顶部(toolbar,SearchView,Overflow),

  • Android仿硬币转动微信红包动画效果

    项目需要研究了一下微信红包动画,即硬币转动的效果,原理其实就是三张不同角度的图片利用AnimationDrawable帧动画进行播放,在参考了案例之后,给自己记录一下完成的过程. 1,在XML文件中定义动画: 步骤如下: ①新建 Android 项目 ②在drawable目录中新建一个anim.xml(注意文件名小写) <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:andr

  • Android编程实现下载时主界面与详细界面一致更新的方法

    本文实例讲述了Android编程实现下载时主界面与详细界面一致更新的方法.分享给大家供大家参考,具体如下: 1.创建监听管理类 public class ObserverManager { private List<Observer> observers = new ArrayList<ObserverManager.Observer>(); public interface Observer { public void update(); public void updateSt

  • android 仿ios数字密码解锁界面的实例

    如下所示: 每个Android开发人员都知道,现在android的解锁最常用的就是九宫格解锁,ios的解锁常用的是数字密码解锁.而我们在开发工程中,很多时候,都需要android和ios进行结合.有的时候我们就需要把我们的解锁界面弄成像ios一样的数字键盘. 这里我就实现了一个仿照ios的数字密码解锁界面.在这里我采用了两种方式来实现,第一种就是使用自定义控件的形式,第二种就是使用我们的布局来实现的.这里我就着重讲一下使用自定义控件形式实现的思路.至于使用布局文件实现的方式,我就不进行具体的讲解

  • android 仿微信demo——微信通讯录界面功能实现(移动端,服务端)

    目录 移动端微信通讯录界面功能实现 服务端微信通讯录界面功能实现 测试 总结 前面我们实现了微信消息界面的实现,这篇继续完善微信功能,实现微信通讯录界面 移动端微信通讯录界面功能实现 微信通讯录,头部是四个标签(不进行分组),下面是好友信息且根据呢称首字母进行排序分组,底部还统计了好友个数,右边是一组英文字母导航,可滑动并且还可以点击跳转到相应的分组 微信好友和顶部的四个标签,可以用ListViw实现并指定一个item布局,分组效果只需要在代码段进行判断即可 右边的字母操作行可以自定义一个组件继

  • android 仿微信demo——微信消息界面实现(服务端)

    目录 服务端微信消息页实现 测试 总结 上一篇实现了移动端微信消息界面功能,以此为基础继续完善服务端功能 服务端微信消息页实现 微信消息界面的实现,和登录,注册是类似的,无非就是接受客户端数据,然后通过这个数据去数据库查找,如果查得到话,返回相应值给客户端. 在移动端中,当用户输入表单后点击登陆,如果登陆成功,则会把微信号通过Itent传给主界面activity,而在微信主界面点击微信消息界面时,会把微信号作为fragment的参数传给微信消息界面,然后通过把微信号数据发送给服务器,服务器接受到

  • Android仿新浪微博oauth2.0授权界面实现代码(2)

    oauth2.0授权界面,大致流程图: 前提准备: 在新浪开放平台申请appkey和appsecret:http://open.weibo.com/. 熟悉oauth2.0协议,相关知识:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html OAuth2的access_token接口:http://open.weibo.com/wiki/OAuth2/access_token 代码详解 大致思路如下:建立一个webview加载授权界面,授权回

  • Android仿iOS侧滑退出当前界面功能

    我们都知道在ios手机上面,有一个侧滑退出当前界面的功能,但是在安卓手机上系统没有给我们提供这样的功能,但是这依然阻挡不了强大的安卓的定制功能,我们完全可以自己定制一套这样的功能. 首先看下效果图: 分析: (1)要想模仿ios的这种效果,因为我们通过手指的滑动,所以这里肯定跟我们的滑动事件有关系(onInterceptTouchEvent,onTouchEvent这两个方法的关系,如果不清楚,请直接查阅事件传递机制原理) (2)我们要想直接拦截我们的所有触摸事件,我们可以在上层父级布局中进行拦

随机推荐