试水Fragment(3)-5min仿微信主界面
来源:互联网 发布:阿里云域名管理平台 编辑:程序博客网 时间:2024/05/24 16:16
今天我们通过Fragment写一个微信主界面:
首先写好主布局文件:
线性布局(替换)+线性布局(选项卡),代码如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:paddingTop="40dp" android:id="@+id/content_ll_weixin" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true" > <Button android:id="@+id/weixin_btn_weixin" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="微信" /> <Button android:id="@+id/contact_btn_weixin" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="通讯" /> <Button android:id="@+id/discover_btn_weixin" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="发现" /> <Button android:id="@+id/me_btn_weixin" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="我的" /> </LinearLayout></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" 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="22sp" /> <Button android:id="@+id/test_btn_weixinFra" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="微信-测试" /></LinearLayout>
分别编写4个Fragment,这里以微信页面Fragment为例,主要还是复写onCreateView方法,这里我们还测试了一下Button的响应:
package com.quan.car.fragmenttest;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;import android.widget.Button;/** * Created by 权兴权意 on 2016/9/2. */public class WeiXinFragment extends Fragment{ private Button test_btn_weixinFra; private Boolean flag = true; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_weixin,null); test_btn_weixinFra = (Button) view.findViewById(R.id.test_btn_weixinFra); test_btn_weixinFra.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (flag){ test_btn_weixinFra.setText("TRUE"); flag = false; }else { test_btn_weixinFra.setText("FLASE"); flag = true; } } }); return view; }}
在主Activity找到4个Button控件并为其设置监听事件,在响应事件中按昨天的博客四步走,获取Manager,获取事务,替换,提交:
package com.quan.car.fragmenttest;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.view.View;import android.widget.Button;/** * Created by 权兴权意 on 2016/9/2. */public class MainWeiXin extends MainActivity implements View.OnClickListener{ private Button weixin_btn_weixin; private Button contact_btn_weixin; private Button discover_btn_weixin; private Button me_btn_weixin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.weixin_main); weixin_btn_weixin = (Button) findViewById(R.id.weixin_btn_weixin); contact_btn_weixin = (Button) findViewById(R.id.contact_btn_weixin); discover_btn_weixin = (Button) findViewById(R.id.discover_btn_weixin); me_btn_weixin = (Button) findViewById(R.id.me_btn_weixin); weixin_btn_weixin.setOnClickListener(this); contact_btn_weixin.setOnClickListener(this); discover_btn_weixin.setOnClickListener(this); me_btn_weixin.setOnClickListener(this); } @Override public void onClick(View view) { FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); switch (view.getId()){ case R.id.weixin_btn_weixin: transaction.replace(R.id.content_ll_weixin,new WeiXinFragment()); break; case R.id.contact_btn_weixin: transaction.replace(R.id.content_ll_weixin,new ContactFragment()); break; case R.id.discover_btn_weixin: transaction.replace(R.id.content_ll_weixin,new DiscoverFragment()); break; case R.id.me_btn_weixin: transaction.replace(R.id.content_ll_weixin,new MeFragment()); break; default: break; } transaction.commit(); }}
效果图:
0 0
- 试水Fragment(3)-5min仿微信主界面
- 试水Fragment(5)-5min实现Fragment之间的通信
- 滑动(仿微信主界面)Fragment、ViewPager
- 5min通过ViewPager实现简易微信主界面(1)-雏形
- 5min通过ViewPager实现简易微信主界面(2)-移动指示器
- fragment(防平板联系人界面,设置界面)、ViewPager+Fragment(防微信界面切换)
- 【Fragment精深系列3】实现Fragment的界面
- 【Fragment精深系列3】实现Fragment的界面
- Android Fragment详解(三):实现Fragment 界面
- Android ViewPager+RadioGroup+Fragment超高仿微信主界面
- Android ViewPager+RadioGroup+Fragment超高仿微信主界面
- Android ViewPager+RadioGroup+Fragment超高仿微信主界面
- Android ViewPager+Fragment超高仿微信主界面
- Android10.3-ViewPager+Fragment实现仿微信界面
- 应用开始的主界面(fragment)
- Fragment的简单使用(切换界面)
- 如何刷新整个fragment(数据+界面)
- Android中fragment选项卡功能(既可点击又可滑动,仿微信主界面)
- 类加载器的工作原理【转载】
- [React Native混合开发]React Native中的Flexbox布局
- C++类定义与实现(Class)
- poj 1679 次小生成树 The Unique MST
- HDU 5407 CRB and Candies(素数筛 + 逆元 + 组合数公式)——2015 Multi-University Training Contest 10
- 试水Fragment(3)-5min仿微信主界面
- 喷水装置(二) nyoj
- (转)解决Ubuntu自动挂载U盘/SD Card变成只读问题
- linux学习之计算机基础(四)
- Angular.js中使用$watch监听模型变化
- tableview或者collectionView列表滑动到顶部的三种方法
- 论IT行业的学习与进阶
- WebSphere MQ发送接收消息_二
- hahh