009 Android Fragment实现微信底部导航
1.XML布局
(1)主界面
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" 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:orientation="horizontal" android:layout_alignParentBottom="true"> <Button android:id="@+id/btmain_weixin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="微信" /> <Button android:id="@+id/btmain_tongxunlu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="通讯录" /> <Button android:id="@+id/btmain_discover" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="发现" /> <Button android:id="@+id/btmain_wo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="我" /> </LinearLayout> </RelativeLayout>
(2)Fragment对应的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" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是weixin模块" /> <Button android:id="@+id/bt_weixin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试weixin" /> </LinearLayout>
2.java后台代码
(1)MainActivity.java
package com.example.administrator.test57wechat; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btmain_weixin=findViewById(R.id.btmain_weixin); Button btmain_tongxunlu=findViewById(R.id.btmain_tongxunlu); Button btmain_wo=findViewById(R.id.btmain_wo); Button btmain_discover=findViewById(R.id.btmain_discover); btmain_weixin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //1.获取Fragment的管理者 FragmentManager fragmentManager=getSupportFragmentManager(); //2.开启事务 FragmentTransaction beginTransaction=fragmentManager.beginTransaction(); //3.替换Fragment beginTransaction.replace(R.id.ll_layout,new WeixinFragment()); beginTransaction.commit(); } }); btmain_tongxunlu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //1.获取Fragment的管理者 FragmentManager fragmentManager=getSupportFragmentManager(); //2.开启事务 FragmentTransaction beginTransaction=fragmentManager.beginTransaction(); //3.替换Fragment beginTransaction.replace(R.id.ll_layout,new TongxunluFragment()); beginTransaction.commit(); } }); btmain_wo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //1.获取Fragment的管理者 FragmentManager fragmentManager=getSupportFragmentManager(); //2.开启事务 FragmentTransaction beginTransaction=fragmentManager.beginTransaction(); //3.替换Fragment beginTransaction.replace(R.id.ll_layout,new WoFragment()); beginTransaction.commit(); } }); btmain_discover.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //1.获取Fragment的管理者 FragmentManager fragmentManager=getSupportFragmentManager(); //2.开启事务 FragmentTransaction beginTransaction=fragmentManager.beginTransaction(); //3.替换Fragment beginTransaction.replace(R.id.ll_layout,new DiscoverFragment()); beginTransaction.commit(); } }); } }
(2)Fragment
package com.example.administrator.test57wechat; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; public class WeixinFragment extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_weixin,null); //测试fragment中的组件是否可以被点击 Button bt_weixin=view.findViewById(R.id.bt_weixin); bt_weixin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { System.out.println("hello weixin"); } }); return view; } }
3.效果图
对应的工程名:test57
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)