1.创建新项目

首先,在Android Studio中创建一个新的项目,选择“Empty Activity”

2. 添加依赖

确保在你的build.gradle文件中包含必要的依赖项。通常,BottomNavigationView已经包含在material库中。

dependencies {
    implementation 'com.google.android.material:material:1.4.0'
    
}

3. 修改布局文件

打开activity_main.xml文件,添加BottomNavigationView和用于显示内容的FrameLayout

<?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">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_navigation" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_nav_menu"
        app:itemIconTint="@color/nav_item_color"
        app:itemTextColor="@color/nav_item_color"
        app:labelVisibilityMode="labeled" />

</RelativeLayout>

4. 创建菜单资源

res/menu目录下创建一个新的XML文件,命名为bottom_nav_menu.xml,并添加三个菜单项。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_contacts"
        android:icon="@drawable/ic_contacts"
        android:title="联系人" />

    <item
        android:id="@+id/navigation_moments"
        android:icon="@drawable/ic_moments"
        android:title="朋友圈" />

    <item
        android:id="@+id/navigation_chat"
        android:icon="@drawable/ic_chat"
        android:title="聊天" />
</menu>

5. 创建Fragment

为联系人、朋友圈和聊天分别创建三个Fragment。

ContactsFragment.java
package com.example.weixinportal;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ContactsFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_contacts, container, false);
    }
}
MomentsFragment.java
package com.example.weixinportal;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MomentsFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_moments, container, false);
    }
}
ChatFragment.java
package com.example.weixinportal;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ChatFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_chat, container, false);
    }
}

6. 创建Fragment布局

为这三个Fragment分别创建布局文件:

fragment_contacts.xml

fragment_moments.xml

fragment_chat.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="联系人页面"
        android:layout_gravity="center"
        android:textSize="20sp" />

</FrameLayout>

7. 设置MainActivity在MainActivity.java中设置BottomNavigationView的点击事件,并替换Fragment。

package com.example.weixinportal;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        FragmentManager fragmentManager = getSupportFragmentManager();

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment fragment = null;
                switch (item.getItemId()) {
                    case R.id.navigation_contacts:
                        fragment = new ContactsFragment();
                        break;
                    case R.id.navigation_moments:
                        fragment = new MomentsFragment();
                        break;
                    case R.id.navigation_chat:
                        fragment = new ChatFragment();
                        break;
                }
                if (fragment != null) {
                    FragmentTransaction transaction = fragmentManager.beginTransaction();
                    transaction.replace(R.id.content_frame, fragment);
                    transaction.commit();
                }
                return true;
            }
        });

        // 默认显示联系人页面
        Fragment initialFragment = new ContactsFragment();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.content_frame, initialFragment);
        transaction.commit();
    }
}

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐