android底部导航栏_Kotlin实现底部导航栏
在我们开发app过程中,免不了使用底部导航栏,所以今天我们就来看看怎么样用kotlin实现底部导航栏。
新建项目
新建一个KotlinBottomNavigationDemo项目。
在gradle中添加依赖
implementation 'com.google.android.material:material:1.0.0'
完成布局工作
首先准备三张图标放到drawable目录下,分别命名为ic_home.png、ic_news.png、ic_user.png。作为轮换图标。
在layout目录下新建三个xml文件,分别表示三个页面下的显示效果。
在三个页面下分别编写内容:
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/home"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:gravity="center"android:textSize="14sp" />
LinearLayout>
fragment_news.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent">
<TextViewandroid:id="@+id/news"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:gravity="center"android:textSize="14sp" />LinearLayout>
fragment_user.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent">
<TextViewandroid:id="@+id/user"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:gravity="center"android:textSize="14sp" />
LinearLayout>
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"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity">
<FrameLayoutandroid:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationViewandroid:id="@+id/bottomNavigation"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="bottom"android:background="#fff"app:itemIconTint="@color/itemTint"app:itemTextColor="@color/itemTextColor"app:labelVisibilityMode="labeled"app:menu="@menu/bottom_nav_menu" />
FrameLayout>
androidx.constraintlayout.widget.ConstraintLayout>
res目录下新建一个menu文件夹,在menu文件夹下新建bottom_nav_menu.xml文件。要几个界面就在这里写几个item,写法如下:
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">
<itemandroid:id="@+id/nav_home"android:icon="@drawable/ic_home" android:title="@string/home"/>
<itemandroid:id="@+id/nav_news"android:icon="@drawable/ic_news"android:title="@string/news"/>
<itemandroid:id="@+id/nav_user"android:icon="@drawable/ic_user"android:title="@string/user"/>
menu>
最后再在color.xml中设置一下颜色。
<?xml version="1.0" encoding="utf-8"?><resources><color name="colorPrimary">#008577color><color name="colorPrimaryDark">#00574Bcolor><color name="colorAccent">#D81B60color><color name="itemTint">#878787color><color name="itemTextColor">#121212color>resources>
编写逻辑代码
按此格式新建三个.kt文件,分别编写对应界面的逻辑显示。
HomeFragment
package com.lkzhang.kotlinbottomnavigation.fragments
import android.annotation.SuppressLintimport android.os.Bundleimport androidx.fragment.app.Fragmentimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport com.lkzhang.kotlinbottomnavigation.Rimport kotlinx.android.synthetic.main.fragment_home.*
class HomeFragment : Fragment() {
override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {return inflater.inflate(R.layout.fragment_home, container, false)}
@SuppressLint("SetTextI18n")override fun onViewCreated(view: View, savedInstanceState: Bundle?) {super.onViewCreated(view, savedInstanceState) home.text = "This is Home Fragment"}
}
NewsFragment
package com.lkzhang.kotlinbottomnavigation.fragments
import android.annotation.SuppressLintimport android.os.Bundleimport androidx.fragment.app.Fragmentimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport com.lkzhang.kotlinbottomnavigation.Rimport kotlinx.android.synthetic.main.fragment_home.*import kotlinx.android.synthetic.main.fragment_news.*
class NewsFragment : Fragment() {
override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {return inflater.inflate(R.layout.fragment_news, container, false)}
@SuppressLint("SetTextI18n")override fun onViewCreated(view: View, savedInstanceState: Bundle?) {super.onViewCreated(view, savedInstanceState)
news.text = "This is News Fragment"}
}
UserFragment
package com.lkzhang.kotlinbottomnavigation.fragments
import android.annotation.SuppressLintimport android.os.Bundleimport androidx.fragment.app.Fragmentimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport com.lkzhang.kotlinbottomnavigation.Rimport kotlinx.android.synthetic.main.fragment_home.*import kotlinx.android.synthetic.main.fragment_user.*
class UserFragment : Fragment() {
override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {return inflater.inflate(R.layout.fragment_user, container, false)}
@SuppressLint("SetTextI18n")override fun onViewCreated(view: View, savedInstanceState: Bundle?) {super.onViewCreated(view, savedInstanceState) user.text = "This is User Fragment"}}
MainActivity
在主活动中编写对应的逻辑,设计点击事件。
package com.lkzhang.kotlinbottomnavigation
import androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport androidx.fragment.app.Fragmentimport com.lkzhang.kotlinbottomnavigation.fragments.HomeFragmentimport com.lkzhang.kotlinbottomnavigation.fragments.NewsFragmentimport com.lkzhang.kotlinbottomnavigation.fragments.UserFragmentimport kotlinx.android.synthetic.main.activity_main.*import com.google.android.material.bottomnavigation.BottomNavigationView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main) title = resources.getString(R.string.home)loadFragment(HomeFragment())
bottomNavigation.setOnNavigationItemSelectedListener{when(it.itemId){ R.id.nav_home -> { title = resources.getString(R.string.home)loadFragment(HomeFragment())return@setOnNavigationItemSelectedListener true}
R.id.nav_news -> { title = resources.getString(R.string.news)loadFragment(NewsFragment())return@setOnNavigationItemSelectedListener true}
R.id.nav_user -> { title = resources.getString(R.string.user)loadFragment(UserFragment())return@setOnNavigationItemSelectedListener true}
}false}
}
private fun loadFragment(fragment: Fragment) {val transaction = supportFragmentManager.beginTransaction() transaction.replace(R.id.container, fragment) transaction.addToBackStack(null) transaction.commit()}
override fun onBackPressed() {super.onBackPressed()finish()}}
最后效果
参考源码
https://github.com/gunawanasch/BottomNavigationAndroid
android底部导航栏_Kotlin实现底部导航栏相关推荐
- android仿咸鱼底部导航栏,Flutter沉浸式状态栏/AppBar导航栏/仿咸鱼底部凸起导航栏效果...
如下图:状态栏是指android手机顶部显示手机状态信息的位置. android 自4.4开始新加入透明状态栏功能,状态栏可以自定义颜色背景,使titlebar能够和状态栏融为一体,增加沉浸感. 如上 ...
- 转载:Android (争取做到)最全的底部导航栏实现方法
原文出处 标题:Android (争取做到)最全的底部导航栏实现方法 作者:野狼谷 原文链接:Android (争取做到)最全的底部导航栏实现方法 - 野狼谷 - 博客园 前言 本文(争取做到)And ...
- Android 11.0 SystemUI导航栏固定在底部显示的修改
目录 1.概述 2.SystemUI导航栏固定在底部显示的修改的相关代码
- React Navigation 导航栏样式调整+底部角标消息提示
五一佳节匆匆而过,有人选择在外面看人山人海,有人选择宅在家中度过五一,也有人依然坚守在第一线,致敬! 这是坚持学习react-native的第二篇文章,可能会迟到,但是绝不会缺席,这篇要涉及到的是re ...
- 【Flutter】底部导航栏实现 ( BottomNavigationBar 底部导航栏 | BottomNavigationBarItem 导航栏条目 | PageView )
文章目录 一.Scaffold 组件 二.底部导航栏整体架构 三.BottomNavigationBar 底部导航栏 四.BottomNavigationBarItem 导航栏条目 五.PageVie ...
- 微信小程序自定义导航栏机型适配--底部Tabbar--view高度--底部按钮适配
自定义微信小程序头部导航栏 自定义微信小程序头部导航栏,有几种方式 方式一 {"navigationStyle": "custom" // 将navigatio ...
- android判断多个按钮,Android开发之判断有无虚拟按键(导航栏)的实例
判断有无虚拟按键(导航栏) 现在很大一部分手机没有虚拟按键,一部分有.我们在做适配的时候可能会用到这方面的知识. 例如:屏幕填充整个屏幕的时候,没办法只能连导航栏一起填充了,但是这个不是我们想要的,我 ...
- xmarin.android导航栏,android – 如何在xamarin表单中更改导航页面后退按钮
我想在导航页面中更改后退箭头图像.为此在Android应用程序中我创建了导航页面渲染器,然后使用方法toolbar.SetNavigationIcon和它不工作,但当我使用toolbar.SetLog ...
- Android Navigation + Fragment 制作APP主页面导航(步骤 + 源码)
运行效果图 Navigation + Fragment制作APP主页面 前言 正文 1. 添加依赖 2. 添加导航图 3. 添加NavHost 4. NavController控制显示Fragment ...
最新文章
- 集成开发环境(IDE)
- 1.2.6 错题整理(组成原理)
- mac与phy如何实现网络自适应
- window.opener方法的使用 刷新父页面
- android split工具,合并APKS为APK工具 Apktool M – AntiSplit on Android v2.4.0|张小北
- ios知识点扩充(1)
- 计算机网络安全与防护第三版课后答案,网络安全与防护—笔试题答案
- 0819 - 要想富,追新不守旧
- 数值分析与算法——读书笔记(一)
- Linux构建一个deb软件安装包
- antv图例出现分页_AntV - G2
- r语言做绘制精美pcoa图_R语言:Bary-Curtis PCoA
- 详解通往Web3的护照:去中心化身份DID
- C++ 数组array与vector的比较
- 微软Google等互联网公司经典面试智力题和解答
- 前端检测图片加载失败,替换图片
- 物联网设备管理的未来
- android 自定义canvas,android随笔之自定义View的Canvas用法
- sql注入新手入门 从实战讲解SQL注入(手动注入+MySQL+靶场源码)
- 【testNG】执行多个suit
热门文章
- React的source code init时会自动检测Chrome dev tool的react extension装了没
- where is application controller bound to application main view
- SAP云平台点了subscription菜单后的roundtrip
- new Grammar in 740 - Internal table group by
- esp8266是linux系统吗,ESP8266 Linux开发环境搭建
- 2021CCPC河北省省赛F题(河南省CCPC测试赛重现)
- 单词搜索Python解法
- ttf_openfont可以多次调用吗_【译文】Rust futures: async fn中的thread::sleep和阻塞调用...
- python中添加高斯噪声_关于python:高斯噪声与高斯白噪声
- java绑定变量怎么加_在JAVA 源程序中编写SQL语句时使用ORACLE 绑定变量