zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    BZOJ1513: [POI2006]Tet-Tetris 3D
    BZOJ3210: 花神的浇花集会
    BZOJ3207: 花神的嘲讽计划Ⅰ
    BZOJ3170: [Tjoi 2013]松鼠聚会
    BZOJ3747: [POI2015]Kinoman
    解题:POI 2008 Subdivision of Kingdom
    解题:JSOI 2007 重要的城市
    解题:USACO13NOV No Change
    解题:洛谷1120 小木棍
    解题:SCOI 2008 配对
  • 原文地址:https://www.cnblogs.com/luckyplj/p/10685837.html
Copyright © 2011-2022 走看看