模拟WeChat

来源:互联网 发布:sql删除字段不满足 编辑:程序博客网 时间:2024/05/25 12:21
第一步:画出UI

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

packagecom.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 classWxFragmentextendsFragment {

   
@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(newView.OnClickListener() {
           
@Override
           
public void onClick(View v) {

                System.
out.println("hahahaahhahahh");
           
}
        })
;

        return
view ;
   
}
}

(2)ContactFragment

packagecom.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 classContactFragmentextendsFragment {

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



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

        return
view ;
   
}
}


(3)  DiscoverFragment

packagecom.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 classDiscoverFragmentextendsFragment {

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



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

        return
view ;
   
}
}

(4)  MeFragment

packagecom.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 classMeFragmentextendsFragment {

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



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

        return
view ;
   
}
}

第三步:在MainActivity实现功能

packagecom.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,newWxFragment());
            break;

            case
R.id. btn_contact :
                beginTransaction.replace(R.id.
ll_layout,newContactFragment());
            break;

            case
R.id. btn_discover:
                beginTransaction.replace(R.id.
ll_layout,newDiscoverFragment());
            break;

            case
R.id. btn_me:
                beginTransaction.replace(R.id.
ll_layout,newMeFragment());
            break;

       
}
        beginTransaction.commit()
;

   
}
}

1 0
原创粉丝点击