模拟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();
}
}
(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();
}
}
相关文章推荐
- 编程珠玑ch01
- UVa--11572 Unique Snowflakes(尺取法)
- DP 动态规划 Problem A 1001 最长子序列
- 读芥川龙之介小说
- Android带图片的Toast(自定义Toast)
- [IT业界] 网盘纷纷停止服务,下一个是谁?
- CMakeLists.txt杂记
- mysql修改表或者数据库的总结
- 作为后台人员你需要知道的CSS基本知识
- 千方百计获取百度网盘下载链接
- group by的使用
- 卿学姐与诡异村庄(并查集)
- Android Studio快捷键
- 子串和
- 剑指offer:最小的K个数
- JVM内存模型以及HotSpot的GC策略
- eclipse启动Android调试出错:The connection to adb is down, and a severe error has occured.
- android平台添加新语种
- 如何在ubuntu中创建eclipse的快捷方式
- Cocos2D 初步了解三:CCAction