您的位置:首页 > 其它

模拟WeChat

2016-04-26 22:59 351 查看
第一步:画出UI

(1)主要模块
<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"

   
tools
:context=".MainActivity"
>

    <LinearLayout

       
android
:id="@+id/ll_layout"

       
android
:layout_width="match_parent"

       
android
:layout_height="match_parent"

       
android
:orientation="vertical"
>

    </LinearLayout>

    <LinearLayout

       
android
:layout_width="match_parent"

       
android
:layout_height="wrap_content"

       
android
:layout_alignParentBottom=
"true"

       
android
:orientation="horizontal"
>

        <Button

           
android
:id="@+id/btn_wx"

           
android
:layout_width="0dp"

           
android
:layout_height="wrap_content"

           
android
:layout_weight="1"

           
android
:text="微信"
/>

        <Button

           
android
:id="@+id/btn_contact"

           
android
:layout_width="0dp"

           
android
:layout_height="wrap_content"

           
android
:layout_weight="1"

           
android
:text="通讯录"
/>

        <Button

           
android
:id="@+id/btn_discover"

           
android
:layout_width="0dp"

           
android
:layout_height="wrap_content"

           
android
:layout_weight="1"

           
android
:text="发现"
/>

        <Button

           
android
:id="@+id/btn_me"

           
android
:layout_width="0dp"

           
android
:layout_height="wrap_content"

           
android
:layout_weight="1"

           
android
:text="我"
/>

    </LinearLayout>

</RelativeLayout>

(2)fragment_wx.xml

<?
xml version="1.0"
encoding=
"utf-8"?>

<LinearLayout
xmlns:
android="http://schemas.android.com/apk/res/android"

   
android
:layout_width="match_parent"

   
android
:layout_height="match_parent">

    <TextView

       
android
:layout_marginTop="10dp"

       
android
:layout_width="wrap_content"

       
android
:layout_height="wrap_content"

       
android
:text="我是微信模块"

       
/>

    <Button

       
android
:layout_marginTop="10dp"

       
android
:id="@+id/test"

       
android
:layout_width="wrap_content"

       
android
:layout_height="wrap_content"

       
android
:text="测试"

       
/>

</LinearLayout>

(3)fragment_contact.xml

<?
xml version="1.0"
encoding=
"utf-8"?>

<LinearLayout
xmlns:
android="http://schemas.android.com/apk/res/android"

   
android
:layout_width="match_parent"

   
android
:layout_height="match_parent">

    <TextView

       
android
:layout_width="wrap_content"

       
android
:layout_height="wrap_content"

       
android
:text="我是微信通讯录模块"

       
android
:textSize="20sp"

       
/>

   
</LinearLayout>

(4)fragment_discover.xml

<?
xml version="1.0"
encoding=
"utf-8"?>

<LinearLayout
xmlns:
android="http://schemas.android.com/apk/res/android"

   
android
:layout_width="match_parent"

   
android
:layout_height="match_parent">

    <TextView

       
android
:layout_width="wrap_content"

       
android
:layout_height="wrap_content"

       
android
:text="我是微信发现模块"

       
android
:textSize="20sp"

       
/>

   
</LinearLayout>

(5)fragment_me.xml

<?
xml version="1.0"
encoding=
"utf-8"?>

<LinearLayout
xmlns:
android="http://schemas.android.com/apk/res/android"

   
android
:layout_width="match_parent"

   
android
:layout_height="match_parent">

    <TextView

       
android
:layout_width="wrap_content"

       
android
:layout_height="wrap_content"

       
android
:text="我是微信我模块"

       
android
:textSize="20sp"

       
/>

   
</LinearLayout>

第二步:继承Fragment

(1)  WxFragment

package
com.eson.wechat;

import
android.app.Fragment
;

import
android.os.Bundle
;

import
android.support.annotation.Nullable
;

import
android.view.LayoutInflater
;

import
android.view.View
;

import
android.view.ViewGroup
;

/**

* Created by Eson on 2016/4/11.

*/
public class
WxFragment
extends
Fragment {

   
@Nullable

    @Override

   
public
View
onCreateView(LayoutInflater inflater
,
ViewGroup container,
Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_wx,
null
);

       
//测试按钮如何点击

       
view.findViewById(R.id.
test).setOnClickListener(new
View.OnClickListener() {

           
@Override

           
public void
onClick(View v) {

                System.out.println(
"hahahaahhahahh"
);

           
}

        });

        return
view
;

   
}
}

(2)ContactFragment

package
com.eson.wechat;

import
android.app.Fragment
;

import
android.os.Bundle
;

import
android.support.annotation.Nullable
;

import
android.view.LayoutInflater
;

import
android.view.View
;

import
android.view.ViewGroup
;

/**

* Created by Eson on 2016/4/11.

*/
public class
ContactFragment
extends
Fragment {

   
@Nullable

    @Override

   
public
View
onCreateView(LayoutInflater inflater
,
ViewGroup container,
Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_contact,
null
);

        return
view
;

   
}
}

(3)  DiscoverFragment

package
com.eson.wechat;

import
android.app.Fragment
;

import
android.os.Bundle
;

import
android.support.annotation.Nullable
;

import
android.view.LayoutInflater
;

import
android.view.View
;

import
android.view.ViewGroup
;

/**

* Created by Eson on 2016/4/11.

*/
public class
DiscoverFragment
extends
Fragment {

   
@Nullable

    @Override

   
public
View
onCreateView(LayoutInflater inflater
,
ViewGroup container,
Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_discover,
null)
;

        return
view
;

   
}
}

(4)  MeFragment

package
com.eson.wechat;

import
android.app.Fragment
;

import
android.os.Bundle
;

import
android.support.annotation.Nullable
;

import
android.view.LayoutInflater
;

import
android.view.View
;

import
android.view.ViewGroup
;

/**

* Created by Eson on 2016/4/11.

*/
public class
MeFragment
extends
Fragment {

   
@Nullable

    @Override

   
public
View
onCreateView(LayoutInflater inflater
,
ViewGroup container,
Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_me,
null
);

        return
view
;

   
}
}

第三步:在MainActivity实现功能

package
com.eson.wechat;

import
android.app.FragmentManager
;

import
android.app.FragmentTransaction
;

import
android.os.Bundle
;

import
android.support.v7.app.AppCompatActivity
;

import
android.view.View
;

import
android.widget.Button
;

public class
MainActivity
extends
AppCompatActivity
implements
View.OnClickListener {

   
@Override

   
protected void
onCreate(Bundle savedInstanceState) {

       
super
.onCreate(savedInstanceState)
;

       
setContentView(R.layout.
activity_main);

       
Button btn_wx  = (Button) findViewById(R.id.
btn_wx);

       
Button btn_contact  = (Button) findViewById(R.id.
btn_contact);

       
Button btn_discover  = (Button) findViewById(R.id.
btn_discover);

       
Button btn_me  = (Button) findViewById(R.id.
btn_me);

       
btn_wx.setOnClickListener(
this);

       
btn_contact.setOnClickListener(
this);

       
btn_discover.setOnClickListener(
this);

       
btn_me.setOnClickListener(
this);

   
}

   
@Override

   
public void
onClick(View v) {

        FragmentManager fragmentManager=getFragmentManager();

       
FragmentTransaction beginTransaction = fragmentManager.beginTransaction()
;

        switch
(v.getId()){

           
case
R.id.btn_wx:

                beginTransaction.replace(R.id.
ll_layout,new
WxFragment())
;

            break;

            case
R.id.
btn_contact
:

                beginTransaction.replace(R.id.
ll_layout,new
ContactFragment())
;

            break;

            case
R.id.
btn_discover:

                beginTransaction.replace(R.id.
ll_layout,new
DiscoverFragment())
;

            break;

            case
R.id.
btn_me:

                beginTransaction.replace(R.id.
ll_layout,new
MeFragment())
;

            break;

       
}

        beginTransaction.commit();

   
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: