Fragment实例之微信界面
今天我们来看看微信界面是怎么实现的。
(一)首先,我们先在主布局XML中实现三部分:
第一个部分:<include layout="@layout/activity_top"></include>,这里面用<include.../>元素设计出了微信界面头部的XML,这里面也是一个XML文件,开发人员可以随意在里面制作界面。
第二部分:运用<FrameLayout.../>元素构建一个容器,里面可以放入不同的frameLayout,创建容器的ID,让你能在java代码中找到。当然,你也可以使用ViewFlipper充当第二部分,采用 手势滑动。viewFlipper这里不作详解,以后会介绍。
第三部分:<includelayout="@layout/activity_bottom"></include>这里又运用了<include.../>元素制作微信底部。
下面是整个主XML布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.wc.wei_xin.MainActivity"><include layout="@layout/activity_top" ></include><FrameLayout android:id="@+id/frame_content" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout><include layout="@layout/activity_bottom"></include> </LinearLayout>
(二)创建微信底部的XML文件:
这里面使用了<LinearLayout.../>元素,里面包含了一个ImageView,这是微信的底部图片,图片的下面用一个TextView来显示文字。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="45dp" android:layout_gravity="bottom"><LinearLayout android:id="@+id/line1" android:orientation="vertical" android:layout_weight="1" android:gravity="center" android:layout_width="wrap_content" android:layout_height="match_parent"><ImageView android:layout_weight="1" android:id="@+id/ic_1" android:src="@drawable/weixin" android:layout_width="wrap_content" android:layout_height="match_parent" /><TextView android:text="微信" android:id="@+id/textview_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="11dp" /></LinearLayout><LinearLayout android:id="@+id/line2" android:orientation="vertical" android:layout_weight="1" android:gravity="center" android:layout_width="wrap_content" android:layout_height="match_parent"><ImageView android:layout_weight="1" android:id="@+id/ic_2" android:src="@drawable/tongxin" android:layout_width="wrap_content" android:layout_height="match_parent" /><TextView android:text="通讯录" android:id="@+id/textview_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="11dp" /></LinearLayout><LinearLayout android:id="@+id/line3" android:orientation="vertical" android:layout_weight="1" android:gravity="center" android:layout_width="wrap_content" android:layout_height="match_parent"><ImageView android:layout_weight="1" android:id="@+id/ic_3" android:src="@drawable/faxian" android:layout_width="wrap_content" android:layout_height="match_parent" /><TextView android:text="通讯录" android:id="@+id/textview_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="11dp" /></LinearLayout><LinearLayout android:id="@+id/line4" android:orientation="vertical" android:layout_weight="1" android:gravity="center" android:layout_width="wrap_content" android:layout_height="match_parent"><ImageView android:layout_weight="1" android:id="@+id/ic_04" android:src="@drawable/wo" android:layout_width="wrap_content" android:layout_height="match_parent" /><TextView android:text="我" android:id="@+id/textview_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="11dp" /></LinearLayout> </LinearLayout>
(三)然后就是创建4个Fragment让他继承基类,然后实现onCreateView()方法返回一个视图。同时你也要创建相应的布局加载文件 weixin_layout ,布局文件里面可以创作很多你的想法,这里不给出。
public class Weixin extends Fragment {public Weixin(){};@Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {return inflater.inflate(R.layout.weixin_layout,container,false);} }
(四)编写主Activity的代码:
这里面使用了上一章介绍的Fragment的基础知识,可以去看下。由于篇幅过长,
我在代码的每块做了详细介绍。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {//Fragment private Fragment Weixin;private Fragment Tongxin;private Fragment Faxian;private Fragment Wo;//底端菜单栏LinearLayout private LinearLayout linear_1;private LinearLayout linear_2;private LinearLayout linear_3;private LinearLayout linear_4;//底端菜单栏Imageview private ImageView iv_1;private ImageView iv_2;private ImageView iv_3;private ImageView iv_4;//底端菜单栏textView private TextView tv_1;private TextView tv_2;private TextView tv_3;private TextView tv_4;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//初始化各个控件 InitView();//初始化点击触发事件 InitEvent();//初始化Fragment InitFragment(1);//将第一个图标设置为选中状态 iv_1.setImageResource(R.drawable.weixin);}private void InitView(){ //找到XML文件中的LinearLayoutlinear_1 = (LinearLayout) findViewById(R.id.line1);linear_2 = (LinearLayout) findViewById(R.id.line2);linear_3 = (LinearLayout) findViewById(R.id.line3);linear_4= (LinearLayout) findViewById(R.id.line4); //绑定XML文件中的ImageViewiv_1 = (ImageView) findViewById(R.id.ic_1);iv_2 = (ImageView) findViewById(R.id.ic_2);iv_3= (ImageView) findViewById(R.id.ic_3);iv_4= (ImageView) findViewById(R.id.ic_04);
tv_2 = (TextView) findViewById(R.id.textview_2);
tv_3 = (TextView) findViewById(R.id.textview_3);
tv_4 = (TextView) findViewById(R.id.textview_4);
}
private void InitFragment(int index){
//得到Fragment的布局管理器,目的是开启FragmentTransaction
FragmentManager fragmentManager = getSupportFragmentManager();android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
//使用transaction里面的add()方法,向FragmentLayout容器里添加Fragment,第一个ID是容器的id。 hideAllFragment(transaction);switch (index){case 1:if (Weixin== null){Weixin = new Weixin();transaction.add(R.id.frame_content,Weixin);}else{transaction.show(Weixin);}break;case 2:if (Tongxin== null){Tongxin = new Tongxin();transaction.add(R.id.frame_content,Tongxin);}else{transaction.show(Tongxin);}break;case 3:if (Faxian== null){Faxian =new Faxian();transaction.add(R.id.frame_content,Faxian);}else{transaction.show(Faxian);}break;case 4:if (Wo== null){Wo = new Wo();transaction.add(R.id.frame_content,Wo);}else{transaction.show(Wo);}break;}transaction.commit();}private void InitEvent(){
//把注册监听器放在一个方法里,有利于后期的维护。linear_1.setOnClickListener(this);linear_2.setOnClickListener(this);linear_3.setOnClickListener(this);linear_4.setOnClickListener(this);}@Override public void onClick(View view) { //单击事件,单击微信底部的LinearLayout,则呈现不同的Fragmentswitch(view.getId()){case R.id.line1:InitFragment(1);break;case R.id.line2:InitFragment(2);break;case R.id.line3:InitFragment(3);break;case R.id.line4:InitFragment(4);break;}}private void hideAllFragment(android.support.v4.app.FragmentTransaction transaction){
//使用transaction的隐藏方法,使Fragment隐藏。if (Weixin!= null){transaction.hide(Weixin);}if (Tongxin!= null){transaction.hide(Tongxin);}if (Faxian!= null){transaction.hide(Faxian);}if (Wo!= null){transaction.hide(Wo);}}
//绑定XML文件中的TextView
tv_1 = (TextView) findViewById(R.id.textview_1);
Fragment实例之微信界面相关推荐
- flutter 微信语言选择_Flutter/dart聊天实例|仿微信界面|红包|朋友圈
FlutterChatroom项目是基于flutter+dart+image_picker等技术实现的仿微信app聊天室实战项目. 一.技术框架编码/技术:Vscode + Flutter 1.12. ...
- flutter图片聊天泡泡_Flutter/dart聊天实例|仿微信界面|红包|朋友圈
FlutterChatroom项目是基于flutter+dart+image_picker等技术实现的仿微信app聊天室实战项目. 一.技术框架编码/技术:Vscode + Flutter 1.12. ...
- android studio开发微信界面
微信界面 主要是做微信的简单的聊天界面,利用Fragment,进行微信界面的跳转 项目代码: 源代码 微信界面 图片: 这是我们要做的界面主要分为头部(top.xml)文件,底部(bottom.xml ...
- android studio微信界面设计,android studio开发微信界面
android studio开发微信界面 android studio开发微信界面 功能说明:主要是做微信的简单的聊天界面,利用Fragment,进行微信界面的跳转 项目代码: 源代码地址 MainA ...
- Kotlin实现微信界面切换(Fragment练习)
使用kotlin实现微信界面切换功能 1主登录界面 2登录界面的Activity,来处理登录界面,登录成功后即可跳转到微信首页 2.1用于回收键盘的工具类(是一个java文件,但是可以很好的融入到ko ...
- Android ViewPager和Fragment实现顶部导航界面滑动效果
在项目中,我们常常需要实现界面滑动切换的效果.例如,微信界面的左右滑动切换效果.那这种效果是怎么实现的?今天我就带大家简单了解ViewPager,并通过实例来实现该效果. 一. ViewPager 官 ...
- Android微信界面的设计
Android微信界面的设计 [尊重原创,转载请注明出处]http://blog.csdn.net/guyuealian/article/details/51777792 微信6.0主界面: (1 ...
- 安卓APP_ Fragment(5)—— Fragment + ViewPager2 模拟微信首页 (2)两者联动翻页
摘自:安卓APP_ Fragment(5)-- Fragment + ViewPager2 模拟微信首页 (2)两者联动实现翻页 作者:丶PURSUING 发布时间: 2021-04-22 00:11 ...
- 安卓APP_ Fragment(4)—— Fragment + ViewPager2 模拟微信首页 (1)两者联动实现翻页
摘自:安卓APP_ Fragment(4)-- Fragment + ViewPager2 模拟微信首页 (1)两者联动实现翻页 作者:丶PURSUING 发布时间: 2021-04-20 17:46 ...
最新文章
- 吴恩达:企业如何实现人工智能转型?
- ubuntu server安装php mysql_Ubuntu Server 下Apache+MySQL+PHP安装
- 012_Switch开关
- leetcode -python 三数之和原创
- 计算机系统基础:磁盘调度知识笔记
- 远控免杀专题7 ---shellter免杀
- [Leedcode][JAVA][第300题][最长上上子序列][动态规划][压缩空间]
- 炒股炒成亿万富翁? 胡润财富报告称人数还不少
- HTML5全屏浏览器兼容方案
- swagger的使用(com.spring4all)
- 计算机算法设计与分析 旅行售货员问题
- Python for和if的连写
- unity IEnumerator 协程的理解
- 大写加下划线转换驼峰规则
- Firewalld防火墙IP伪装与端口转发
- 解决lightdm启动时黑屏的问题
- DevOps 转型实践
- 从零开始创建一个Android主屏幕Widget
- 人类的15个欲望与游戏设计
- 目标检测之Softer-NMS