Android开发教程实战案例源码分享-BottomNavigationView 使用以及中间凸起效果

113 阅读2分钟
Android开发教程实战案例源码分享-BottomNavigationView 使用以及中间凸起效果

主页中间往往有凸起效果的需求

一、思路:

BottomNavigationView,中间用盖一层方式

二、效果图:

在这里插入图片描述

三、关键代码:

xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    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"
    android:focusable="true"
    android:focusableInTouchMode="true">
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    >

    <fragment
        android:id="@+id/fragment_nav_main"
        android:name="com.cong.mybottomnavigationdemo.fragment.TabNavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/navigation_main"
        app:defaultNavHost="false"
        app:navGraph="@navigation/nav_simple"
        />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation_main"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#ffffff"
        app:elevation="1px"
        app:itemBackground="@null"
        app:itemIconSize="24dp"
        app:itemTextColor="@drawable/bottom_navigation_item_selector"
        app:labelVisibilityMode="labeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:menu="@menu/bottom_nav_menu"/>


    <View
        android:id="@+id/view_left"
        android:layout_width="0dp"
        android:layout_height="1px"
        android:background="#ededed"
        app:layout_constraintBottom_toTopOf="@id/view_left_1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/view_arc" />
    <!--加多一个view填白色撑高,ed线好对齐-->
    <View
        android:id="@+id/view_left_1"
        android:layout_width="0dp"
        android:layout_height="1px"
        android:background="@color/white"
        app:layout_constraintBottom_toBottomOf="@id/view_arc"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/view_arc"/>

    <ImageView
        android:id="@+id/view_arc"
        android:layout_width="67dp"
        android:layout_height="11dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_main_arc"
        app:layout_constraintBottom_toTopOf="@id/navigation_main"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />


    <View
        android:id="@+id/view_right"
        android:layout_width="0dp"
        android:layout_height="1px"
        android:background="#ededed"
        app:layout_constraintBottom_toTopOf="@id/view_right_1"
        app:layout_constraintLeft_toRightOf="@id/view_arc"
        app:layout_constraintRight_toRightOf="parent" />

    <View
        android:id="@+id/view_right_1"
        android:layout_width="0dp"
        android:layout_height="1px"
        android:background="@color/white"
        app:layout_constraintBottom_toBottomOf="@id/view_arc"
        app:layout_constraintLeft_toRightOf="@id/view_arc"
        app:layout_constraintRight_toRightOf="parent" />



</androidx.constraintlayout.widget.ConstraintLayout>

    <!--要大一点,避免点中BottomNavigationView中间的-->
    <ImageView
        android:id="@+id/iv_main_publish_mood"
        android:layout_width="100dp"
        android:layout_height="60dp"
        android:src="@mipmap/ic_main_publish_mood"
        android:scaleType="centerCrop"
        android:layout_gravity="center_horizontal|bottom"
        android:paddingTop="14dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp" />
</FrameLayout>

Main主页代码:

class MainActivity : AppCompatActivity() {

    lateinit var navigationMain: BottomNavigationView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        navigationMain = findViewById(R.id.navigation_main)

        //icon显示我们的
        navigationMain.itemIconTintList = null
        val mMainNavFragment: Fragment? = supportFragmentManager.findFragmentById(R.id.fragment_nav_main)
        val navController = NavHostFragment.findNavController(mMainNavFragment!!)
        navigationMain.setupWithNavController(navController)

        //指明指向,防止重复点击不见的问题
        navigationMain.setOnNavigationItemSelectedListener { item ->
            navController.navigate(item.itemId)
            true
        }

        //设置未读数,原本控件的位置和颜色不满足需求
        val menuView = navigationMain.getChildAt(0) as BottomNavigationMenuView
        val tabItemView = menuView.getChildAt(1) as BottomNavigationItemView
        val badgeView = LayoutInflater.from(this).inflate(R.layout.main_tab_msg_num,navigationMain,false)
        tabItemView.addView(badgeView)
        val tvUnRead = badgeView.findViewById<TextView>(R.id.tv_msg_count)
        tvUnRead.text = "${15}"
        tvUnRead.visibility = View.VISIBLE

        findViewById<ImageView>(R.id.iv_main_publish_mood).setOnClickListener{
            Toast.makeText(this,"跳转到发布动态",Toast.LENGTH_LONG).show()
        }
    }
}
四、项目demo源码结构图:

在这里插入图片描述
有问题或者需要完整源码demo的可以看简介联系我,也可以私信我,我每天都看私信的