Android仿微信界面--使用Fragment实现(慕课网笔记)
1 效果图
这里我们没有实现滑动切换view的功能
2 具体实现:
2.1 布局文件:top.xml, bottom.xml,tab01.xml,tab02.xml,tab03.xml,tab04.xml
具体请参考上述博客
2.2 新建4个Fragment,WeixinFragment,FrdFragment,AddressFragment,SettingFragment,分别对应tab01.xml,tab02.xml,tab03.xml,tab04.xml,其中这个Fragment是android.support.v4.app下面的(为了更好的兼容低版本的安卓设备),此时注意,以下凡是用到的Fragment都要是android.support.v4.app下的,这样不易出问题。
WeixinFragment
package com.example.imooc_weixinfragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class WeixinFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//引入我们的布局
return inflater.inflate(R.layout.tab01, container, false);
} }
FrdFragment
package com.example.imooc_weixinfragment; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class FrdFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//引入我们的布局
return inflater.inflate(R.layout.tab02, container, false);
} }
AddressFragment
package com.example.imooc_weixinfragment; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class AddressFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//引入我们的布局
return inflater.inflate(R.layout.tab03, container, false);
} }
SettingFragment
package com.example.imooc_weixinfragment; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class SettingFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//引入我们的布局
return inflater.inflate(R.layout.tab04, container, false);
} }
activity_main.xml,这里我们使用FrameLayout代替viewPager
<LinearLayout 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"
android:orientation="vertical"
> <include layout="@layout/top"/>
<FrameLayout
android:id="@+id/id_content"
android:layout_weight=""
android:layout_width="match_parent"
android:layout_height="0dp"> </FrameLayout>
<include layout="@layout/bottom"/> </LinearLayout>
2.3 MainActivity
package com.example.imooc_weixinfragment; 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.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout; //我们使用的是android v4包下的fragment,这里必须要继承自FragmentActivity,而不是Activity
public class MainActivity extends FragmentActivity implements OnClickListener{
//底部的4个导航控件
private LinearLayout mTabWeixin;
private LinearLayout mTabFrd;
private LinearLayout mTabAddress;
private LinearLayout mTabSetting;
//底部4个导航控件中的图片按钮
private ImageButton mImgWeixin;
private ImageButton mImgFrd;
private ImageButton mImgAddress;
private ImageButton mImgSetting;
//初始化4个Fragment
private Fragment tab01;
private Fragment tab02;
private Fragment tab03;
private Fragment tab04; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();//初始化所有的view
initEvents();
setSelect();//默认显示微信聊天界面
} private void initEvents() {
mTabWeixin.setOnClickListener(this);
mTabFrd.setOnClickListener(this);
mTabAddress.setOnClickListener(this);
mTabSetting.setOnClickListener(this); } private void initView() {
mTabWeixin = (LinearLayout)findViewById(R.id.id_tab_weixin);
mTabFrd = (LinearLayout)findViewById(R.id.id_tab_frd);
mTabAddress = (LinearLayout)findViewById(R.id.id_tab_address);
mTabSetting = (LinearLayout)findViewById(R.id.id_tab_setting);
mImgWeixin = (ImageButton)findViewById(R.id.id_tab_weixin_img);
mImgFrd = (ImageButton)findViewById(R.id.id_tab_frd_img);
mImgAddress = (ImageButton)findViewById(R.id.id_tab_address_img);
mImgSetting = (ImageButton)findViewById(R.id.id_tab_setting_img); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} @Override
public void onClick(View v) {
resetImg();
switch (v.getId()) {
case R.id.id_tab_weixin://当点击微信按钮时,切换图片为亮色,切换fragment为微信聊天界面
setSelect();
break;
case R.id.id_tab_frd:
setSelect();
break;
case R.id.id_tab_address:
setSelect();
break;
case R.id.id_tab_setting:
setSelect();
break; default:
break;
} } /*
* 将图片设置为亮色的;切换显示内容的fragment
* */
private void setSelect(int i) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();//创建一个事务
hideFragment(transaction);//我们先把所有的Fragment隐藏了,然后下面再开始处理具体要显示的Fragment
switch (i) {
case :
if (tab01 == null) {
tab01 = new WeixinFragment();
/*
* 将Fragment添加到活动中,public abstract FragmentTransaction add (int containerViewId, Fragment fragment)
*containerViewId即为Optional identifier of the container this fragment is to be placed in. If 0, it will not be placed in a container.
* */
transaction.add(R.id.id_content, tab01);//将微信聊天界面的Fragment添加到Activity中
}else {
transaction.show(tab01);
}
mImgWeixin.setImageResource(R.drawable.tab_weixin_pressed);
break;
case :
if (tab02 == null) {
tab02 = new FrdFragment();
transaction.add(R.id.id_content, tab02);
}else {
transaction.show(tab02);
}
mImgFrd.setImageResource(R.drawable.tab_find_frd_pressed);
break;
case :
if (tab03 == null) {
tab03 = new AddressFragment();
transaction.add(R.id.id_content, tab03);
}else {
transaction.show(tab03);
}
mImgAddress.setImageResource(R.drawable.tab_address_pressed);
break;
case :
if (tab04 == null) {
tab04 = new SettingFragment();
transaction.add(R.id.id_content, tab04);
}else {
transaction.show(tab04);
}
mImgSetting.setImageResource(R.drawable.tab_settings_pressed);
break; default:
break;
}
transaction.commit();//提交事务
} /*
* 隐藏所有的Fragment
* */
private void hideFragment(FragmentTransaction transaction) {
if (tab01 != null) {
transaction.hide(tab01);
}
if (tab02 != null) {
transaction.hide(tab02);
}
if (tab03 != null) {
transaction.hide(tab03);
}
if (tab04 != null) {
transaction.hide(tab04);
} } private void resetImg() {
mImgWeixin.setImageResource(R.drawable.tab_weixin_normal);
mImgFrd.setImageResource(R.drawable.tab_find_frd_normal);
mImgAddress.setImageResource(R.drawable.tab_address_normal);
mImgSetting.setImageResource(R.drawable.tab_settings_normal);
}
}
源码在这里:http://download.csdn.net/detail/hnyzwtf/9356533
Android仿微信界面--使用Fragment实现(慕课网笔记)的更多相关文章
- Android仿微信界面
效果图 原理介绍 1.先绘制一个颜色(例如:粉红) 2.设置Mode=DST_IN 3.绘制我们这个可爱的小机器人 回答我,显示什么,是不是显示交集,交集是什么?交集是我们的小机器人的非透明区域,也就 ...
- Android ActionBar仿微信界面
ActionBar仿微信界面 1.学习了别人的两篇关于ActionBar博客,在结合别人的文章来仿造一下微信的界面: 思路如下:1).利用ActionBar生成界面的头部,在用ActionBar的Ac ...
- Android仿微信图片上传,可以选择多张图片,缩放预览,拍照上传等
仿照微信,朋友圈分享图片功能 .可以进行图片的多张选择,拍照添加图片,以及进行图片的预览,预览时可以进行缩放,并且可以删除选中状态的图片 .很不错的源码,大家有需要可以下载看看 . 微信 微信 微信 ...
- Android 仿微信小视频录制
Android 仿微信小视频录制 WechatShortVideo和WechatShortVideo文章
- H5仿微信界面教程(一)
前言 先来张图,仿微信界面,界面如下,并不完全一模一样,只能说有些类似,希望大家见谅. 1 用到的知识点 jQuery WeUI 是WeUI的一个jQuery实现版本,除了实现了官方插件之外,它还提供 ...
- h5移动端聊天室|仿微信界面聊天室|h5多人聊天室
今年的FIFA世界杯甚是精彩,最近兴致高涨就利用HTML5开发了一个手机端仿微信界面聊天室,该h5聊天室采用750px全新伸缩flex布局,以及使用rem响应式配合fontsize.js,页面弹窗则是 ...
- react聊天室|react+redux仿微信聊天IM实例|react仿微信界面
一.项目概况 基于react+react-dom+react-router-dom+redux+react-redux+webpack2.0+react-photoswipe+swiper等技术混合开 ...
- es6 Object.assign ECMAScript 6 笔记(六) ECMAScript 6 笔记(一) react入门——慕课网笔记 jquery中动态新增的元素节点无法触发事件解决办法 响应式图像 弹窗细节 微信浏览器——返回操作 Float 的那些事 Flex布局 HTML5 data-* 自定义属性 参数传递的四种形式
es6 Object.assign 目录 一.基本用法 二.用途 1. 为对象添加属性 2. 为对象添加方法 3. 克隆对象 4. 合并多个对象 5. 为属性指定默认值 三.浏览器支持 ES6 O ...
- Android控件-Fragment+ViewPager(高仿微信界面)
什么是Fragment? Fragment是Android3.0后新增的概念,Fragment名为碎片,不过却和Activity十分相似,具有自己的生命周期,它是用来描述一些行为或一部分用户界面在一个 ...
随机推荐
- LinearLayout遇到的问题——利用LinearLayout做横向滑动冲突
问题:当我添加两个TextView的时候,然后滑动,发现只生成了一个TextView. 就是 <?xml version="1.0" encoding="utf-8 ...
- 基于DateTime Picker修改成类似旅游网站出发日期选择的功能
原版说明文档:http://www.bootcss.com/p/bootstrap-datetimepicker/ 修改后可支持多日期选择和控制可选日期,这样就能在后台设置哪些日期可选,前台展示时可以 ...
- Square Coins(母函数)
Square Coins 点我 Problem Description People in Silverland use square coins. Not only they have square ...
- iOS中判断设备系统版本
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- C语言--流程控制
一.流程控制 1.顺序结构 *默认的流程结构,按照书写顺序执行每一条语句 2.选择结构 *对给定的条件进行判断,再根据判断结果来决定执行那一段代码 3.循环结构 *在给定条件成立的情况下,反复执行某一 ...
- 使用Java管理Azure(1):基础配置
Azure针对Java开发人员提供了非常丰富的开发库,开发工具,和相关插件,让你通过Java对Azure进行服务管理和开发,本文第一步先介绍如何快速的配置Java开发工具,主要针对目前比较流行的Ecl ...
- 一步一步学python(三) - 使用字符串
1.基本字符串操作 序列和元组的索引.分片.乘法.判断成员资格.求长度.取最小值和最大值对字符串同样适用. 字符串是不可变的 2.字符串格式化 %左侧放字符串右侧放格式化的值.一般情况下使用元组 fo ...
- [python] 如何用python操作Excel
直接上代码: from openpyxl import Workbook from openpyxl.cell import get_column_letter wb = Workbook() des ...
- 竹林蹊径-深入浅出Windows内核开发作者的博客
http://blog.csdn.net/blog_index http://blog.csdn.net/blog_index/article/details/6012054 http://downl ...
- 基础巩固(二)- log4j的使用
日志是应用软件中不可缺少的部分,Apache的开源项目log4j是一个功能强大的日志组件,提供方便的日志记录.在apache网站:jakarta.apache.org/log4j 可以免费下载到Log ...