第一步添加依赖:
implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'
implementation 'androidx.navigation:navigation-runtime-ktx:2.3.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
步骤二:创建多个要调配的Fragment
这里我创建了5个Fragment,第一个是AIListeningFragment,第二个是AudiometryFragment,第三个是DeafAidFragment。第四个是MyFragment,第五个是ServiceFragment 其中一个如下:
class AIListeningFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_a_i_listening, container, false)
}
companion object {
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
AIListeningFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
步骤三:在res下面创建navigation文件夹,并创建navigation文件 需要注意的是navigation文件夹必须在res文件夹下面并且名称为固定写法,这里我新建了一个nav_graph.xml文件,如下所示:
依次添加完5个fragment之后把它依次连接起来
连接完成之后nav_graph.xml代码如下:
<?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/nav_graph"
app:startDestination="@id/AIListeningFragment">
<fragment
android:id="@+id/AIListeningFragment"
android:name="com.eyuai.fragment.AIListeningFragment"
android:label="fragment_a_i_listening"
tools:layout="@layout/fragment_a_i_listening" >
<action
android:id="@+id/action_AIListeningFragment_to_audiometryFragment"
app:destination="@id/audiometryFragment" />
<action
android:id="@+id/action_AIListeningFragment_to_audiometryFragment2"
app:destination="@id/audiometryFragment" />
</fragment>
<fragment
android:id="@+id/audiometryFragment"
android:name="com.eyuai.fragment.AudiometryFragment"
android:label="fragment_audiometry"
tools:layout="@layout/fragment_audiometry" >
<action
android:id="@+id/action_audiometryFragment_to_deafAidFragment"
app:destination="@id/deafAidFragment" />
</fragment>
<fragment
android:id="@+id/deafAidFragment"
android:name="com.eyuai.fragment.DeafAidFragment"
android:label="fragment_deaf_aid"
tools:layout="@layout/fragment_deaf_aid" >
<action
android:id="@+id/action_deafAidFragment_to_myFragment"
app:destination="@id/myFragment" />
</fragment>
<fragment
android:id="@+id/myFragment"
android:name="com.eyuai.fragment.MyFragment"
android:label="fragment_my"
tools:layout="@layout/fragment_my" />
<fragment
android:id="@+id/serviceFragment"
android:name="com.eyuai.fragment.ServiceFragment"
android:label="fragment_service"
tools:layout="@layout/fragment_service" />
</navigation>
步骤四:创建menu文件夹,添加menu_main.xml文件,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/audiometryMenu"
android:icon="@drawable/audiometry"
android:title="测听" />
<item
android:id="@+id/deafAidMenu"
android:icon="@drawable/deafaid"
android:title="助听器" />
<item
android:id="@+id/AiListeningMenu"
android:icon="@drawable/ailistening"
android:title="AI辅听" />
<item
android:id="@+id/serviceMenu"
android:icon="@drawable/service"
android:title="服务"></item>
<item
android:id="@+id/myMenu"
android:icon="@drawable/service"
android:title="我的"></item>
</menu>
其中audiometry代码如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/audiometrytoobarimg" android:state_selected="true" />
<item android:drawable="@mipmap/noaudiometrytoobarimg" />
</selector>
第五步修改activity_main.xml布局如下:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</androidx.appcompat.widget.Toolbar>
<ImageView
android:id="@+id/topimg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@mipmap/toolbarbg"
app:layout_constraintTop_toTopOf="parent"></ImageView>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
<!-- android:theme="@style/myNavigationDrawerStyle"-->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bnv_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemTextAppearanceInactive="@style/bottom_tab_title_inactive"
app:itemTextAppearanceActive="@style/bottom_tab_title_active"
app:itemIconSize="@dimen/dp_20"
android:background="@android:color/white"
app:itemIconTint="@drawable/main_bottom_color_selector"
app:itemTextColor="@drawable/main_bottom_color_selector"
app:menu="@menu/menu_main" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
bottom_tab_title_inactive风格代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="myNavigationDrawerStyle">
<item name="android:textSize">10sp</item>
</style>
<!--选中的样式,可以设置选择的字体颜色,这里只是为了改变选择时的字体大小-->
<style name="bottom_tab_title_active">
<item name="android:textSize">10sp</item>
</style>
<!--没有选中的样式,导航默认的字体大小就是12sp-->
<style name="bottom_tab_title_inactive">
<item name="android:textSize">10sp</item>
</style>
</resources>
main_bottom_color_selector代码如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#1742F9" android:state_checked="true"/>
<item android:color="#1F1F21" android:state_checked="false"/>
</selector>
第六步:添加MainActivity代码:
package com.eyuai.navigation
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.navigation.NavController
import androidx.navigation.Navigation
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.google.android.material.bottomnavigation.BottomNavigationMenuView
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.bottomnavigation.LabelVisibilityMode
class MainActivity : AppCompatActivity() {
lateinit var navigationView: BottomNavigationView
lateinit var menuView: BottomNavigationMenuView
lateinit var itemView: BottomNavigationItemView
lateinit var controller: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initNav()
}
fun initNav() {
controller = Navigation.findNavController(this@MainActivity, R.id.my_nav_host_fragment)
navigationView = findViewById(R.id.bnv_view)
navigationView.labelVisibilityMode =
LabelVisibilityMode.LABEL_VISIBILITY_LABELED//BottomNavigationView3个以上图标不显示文字
menuView = navigationView.getChildAt(0) as BottomNavigationMenuView
supportFragmentManager.registerFragmentLifecycleCallbacks(object :
FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentCreated(
fm: FragmentManager,
f: Fragment,
savedInstanceState: Bundle?
) {
Log.i("NavActivity", "commonLog - onFragmentCreated: $f")
}
override fun onFragmentStarted(fm: FragmentManager, f: Fragment) {
Log.i("NavActivity", "commonLog - onFragmentStarted: $f")
}
override fun onFragmentResumed(fm: FragmentManager, f: Fragment) {
Log.i("NavActivity", "commonLog - onFragmentResumed: $f")
}
override fun onFragmentPaused(fm: FragmentManager, f: Fragment) {
Log.i("NavActivity", "commonLog - onFragmentPaused: $f")
}
override fun onFragmentStopped(fm: FragmentManager, f: Fragment) {
Log.i("NavActivity", "commonLog - onFragmentStopped: $f")
}
override fun onFragmentDestroyed(fm: FragmentManager, f: Fragment) {
Log.i("NavActivity", "commonLog - onFragmentDestroyed: $f")
}
override fun onFragmentActivityCreated(
fm: FragmentManager,
f: Fragment,
savedInstanceState: Bundle?
) {
Log.i("NavActivity", "commonLog - onFragmentActivityCreated: $f")
}
override fun onFragmentViewCreated(
fm: FragmentManager,
f: Fragment,
v: View,
savedInstanceState: Bundle?
) {
}
override fun onFragmentViewDestroyed(fm: FragmentManager, f: Fragment) {
Log.i("NavActivity", "commonLog - onFragmentViewDestroyed: $f")
}
}, true)
navigationView.setOnNavigationItemSelectedListener(BottomNavigationView.OnNavigationItemSelectedListener { it ->
var canClick = true
when (it.itemId) {
R.id.audiometryMenu -> {
controller.navigate(R.id.audiometryFragment);
canClick = true
}
R.id.deafAidMenu -> {
// canClick=false//以后可以用来限制底部状态是否点击状态
controller.navigate(R.id.deafAidFragment);
}
R.id.AiListeningMenu -> {
canClick = true
controller.navigate(R.id.AIListeningFragment);
}
R.id.serviceMenu -> {
canClick = true
controller.navigate(R.id.serviceFragment);
}
R.id.myMenu -> {
canClick = true
controller.navigate(R.id.myFragment);
}
}
canClick
})
}
}
如fragment 想返回那个fragment里可以
val navController = findNavController()
navController.navigate(R.id.AIListeningFragment)
为底部添加消息提醒,原文章//www.greatytc.com/p/4c8dfe6a0cf3,如有冒犯,请联系删除,只是为了记录
第一步添加notifications_badge.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/notifications_badge"
android:layout_width="@dimen/dp_16"
android:layout_height="@dimen/dp_16"
android:layout_gravity="top|center_horizontal"
android:layout_marginLeft="@dimen/dp_20"
android:background="@drawable/notification_badge_bg"
android:gravity="center"
android:text="5"
android:textColor="@color/white"
android:textSize="8sp" />
</LinearLayout>
notification_badge_bg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="28sp"
android:height="28sp" />
<solid android:color="#ff00ff" />
</shape>
创建BavNotifacationBadgeUtil,代码如下
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;
import com.eyuai.navigation.R;
import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class BavNotifacationBadgeUtil {
public static void addNotificationBadge(BottomNavigationView mBNV, int itemIndex, String desc) {
BottomNavigationItemView mCurrentItemView = getBottomNavigationItemView(mBNV, itemIndex);
LayoutInflater.from(mBNV.getContext()).inflate(R.layout.notifications_badge, mCurrentItemView, true);
TextView mBadge = (TextView) mCurrentItemView.findViewById(R.id.notifications_badge);
if (mBadge != null) {
mBadge.setText(desc);
}
}
public static void removeNotificationBadge(BottomNavigationView mBNV, int itemIndex) {
BottomNavigationItemView mCurrentItemView = getBottomNavigationItemView(mBNV, itemIndex);
TextView mBadge = (TextView) mCurrentItemView.findViewById(R.id.notifications_badge);
if (mBadge != null) {
((ViewGroup) mBadge.getParent()).removeView(mBadge);
}
}
public static void modifyNotificationBadgeContent(BottomNavigationView mBNV, int itemIndex, String desc) {
BottomNavigationItemView mCurrentItemView = getBottomNavigationItemView(mBNV, itemIndex);
TextView mBadge = (TextView) mCurrentItemView.findViewById(R.id.notifications_badge);
if (mBadge != null) {
mBadge.setText(desc);
}
}
public static Boolean judgeBadgeExist(BottomNavigationView mBNV, int itemIndex) {
BottomNavigationItemView mCurrentItemView = getBottomNavigationItemView(mBNV, itemIndex);
TextView mBadge = (TextView) mCurrentItemView.findViewById(R.id.notifications_badge);
if (mBadge == null) {
return false;
} else {
return true;
}
}
private static BottomNavigationItemView getBottomNavigationItemView(BottomNavigationView mBNV, int itemIndex) {
BottomNavigationMenuView bottomNavigationMenuView = (BottomNavigationMenuView) mBNV.getChildAt(0);
return (BottomNavigationItemView) bottomNavigationMenuView.getChildAt(itemIndex);
}
}
使用:
BavNotifacationBadgeUtil.addNotificationBadge(navigationView,2,"10")