安卓类微信开发

界面效果展示

 

 

 

 目录树展示

 

 

 说明

.ui

  存放四个页面的Fragment,以及MainActivity

drawable

  存放图片资源

layout  

  主空间以及不同页面的xml

menu  

  底部导航栏的xml,配置名称以及图标等属性

navigation

  配置不同页面的label以及Fragment

代码

MainActivity

复制代码
 1 package com.example.wechat;
 2 
 3 import android.os.Bundle;
 4 
 5 import com.google.android.material.bottomnavigation.BottomNavigationView;
 6 
 7 import androidx.appcompat.app.AppCompatActivity;
 8 import androidx.navigation.NavController;
 9 import androidx.navigation.Navigation;
10 import androidx.navigation.ui.AppBarConfiguration;
11 import androidx.navigation.ui.NavigationUI;
12 
13 import com.example.wechat.databinding.ActivityMainBinding;
14 
15 public class MainActivity extends AppCompatActivity {
16 
17     private ActivityMainBinding binding;
18 
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22 
23         binding = ActivityMainBinding.inflate(getLayoutInflater());
24         setContentView(binding.getRoot());
25 
26         BottomNavigationView navView = findViewById(R.id.nav_view);
27         // Passing each menu ID as a set of Ids because each
28         // menu should be considered as top level destinations.
29         AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
30                 R.id.navigation_chats, R.id.navigation_contacts, R.id.navigation_discover, R.id.navigation_my)
31                 .build();
32         NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
33         NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
34         NavigationUI.setupWithNavController(binding.navView, navController);
35     }
36 
37 }
MainActivity
复制代码

ChatsFragment

复制代码
package com.example.wechat.ui.chats;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.wechat.databinding.FragmentChatsBinding;

public class ChatsFragment extends Fragment {

    private FragmentChatsBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        ChatsViewModel chatsViewModel =
                new ViewModelProvider(this).get(ChatsViewModel.class);

        binding = FragmentChatsBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}
ChatsFragment
复制代码

ChatsViewModel

复制代码
package com.example.wechat.ui.chats;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class ChatsViewModel extends ViewModel {

    private final MutableLiveData<String> mText;

    public ChatsViewModel() {
        mText = new MutableLiveData<>();
        mText.setValue("This is chats fragment");
    }

    public LiveData<String> getText() {
        return mText;
    }
}
ChatsViewModel
复制代码

activity_main

复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment_activity_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>
activity_main
复制代码

fragment_chats

复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".ui.chats.ChatsFragment">


</androidx.constraintlayout.widget.ConstraintLayout>
fragment_chats
复制代码

bottom_nav_menu

复制代码
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_chats"
        android:icon="@drawable/chats"
        android:title="chats" />

    <item
        android:id="@+id/navigation_contacts"
        android:icon="@drawable/contacts"
        android:title="contacts" />

    <item
        android:id="@+id/navigation_discover"
        android:icon="@drawable/discover"
        android:title="discover" />
    <item
        android:id="@+id/navigation_my"
        android:icon="@drawable/my"
        android:title="My" />

</menu>
bottom_nav_menu
复制代码

mobile_navigation

复制代码
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/mobile_navigation"
    app:startDestination="@+id/navigation_chats">

    <fragment
        android:id="@+id/navigation_chats"
        android:name="com.example.wechat.ui.chats.ChatsFragment"
        android:label="Chats"
        tools:layout="@layout/fragment_chats" />

    <fragment
        android:id="@+id/navigation_contacts"
        android:name="com.example.wechat.ui.contacts.ContactsFragment"
        android:label="Contacts"
        tools:layout="@layout/fragment_contacts" />

    <fragment
        android:id="@+id/navigation_discover"
        android:name="com.example.wechat.ui.discover.DiscoverFragment"
        android:label="Discover"
        tools:layout="@layout/fragment_discover" />
    <fragment
        android:id="@+id/navigation_my"
        android:name="com.example.wechat.ui.my.MyFragment"
        android:label="My"
        tools:layout="@layout/fragment_my" />
</navigation>
mobile_navigation
复制代码

 项目地址

https://github.com/yoyo-sanchez/yoyo/tree/main/Wechat
posted @   yoyo_sanchez  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
点击右上角即可分享
微信分享提示