原文鏈接https://www.shanya.world/archives/39c4da83.html
前言
本例是基于AndroidStudio以Kotlin語言開發(fā),實(shí)現(xiàn)Android應(yīng)用中常見的側(cè)邊劃出欄。
就像下面這樣
接下來進(jìn)入實(shí)現(xiàn)部分
首先新建一個工程
添加兩個依賴
implementation 'androidx.navigation:navigation-ui:2.0.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.0.0'
implementation 'androidx.navigation:navigation-fragment:2.0.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.0.0'
對 AndroidManifestest.xml 的一點(diǎn)改動
看圖片標(biāo)紅部分
這里用到了res的style,直接將這個style的代碼貼出,后續(xù)還有部分xml用到了
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
新建一個xml顯示我的首頁內(nèi)容
需要注意的是,這里還需要對這個xml進(jìn)行一個設(shè)置,看下圖標(biāo)紅處代碼
處理完之后再看我們的圖形化界面,多出一個標(biāo)題欄
新建一個ToolBar
用系統(tǒng)自帶的ActionBar會把側(cè)邊欄擋住(我的是這樣,如果能解決請告訴我一下_)
我們先新建一個xml文件
添加一個 AppBarLayout
還需要將 content_mian.xml 這一文件 include 引入
直接放出xml代碼,更加直觀
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
同樣標(biāo)紅處需要關(guān)注一下,可能需要手動加入
制作側(cè)邊欄的表頭
這里就是簡單的 ImageView 和 TextView 的組合,就不詳細(xì)贅述了,直接放出xml代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="176dp"
android:background="#91AD70"
android:gravity="bottom"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Navgaton Header"
android:paddingTop="8dp"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:text="Shanya"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="shanyaliux@qq.com" />
</LinearLayout>
效果圖如下
制作側(cè)邊欄的選項(xiàng)
先新建一個menu
這里僅僅是添加了幾個MenuItem,直接貼出xml代碼(這里為了好看一點(diǎn),加了幾個Vector Assert,這個就自行添加了,反正不重要)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_one"
android:icon="@drawable/ic_looks_one_black_24dp"
android:title="one" />
<item
android:id="@+id/nav_two"
android:icon="@drawable/ic_looks_two_black_24dp"
android:title="two" />
<item
android:id="@+id/nav_three"
android:icon="@drawable/ic_looks_3_black_24dp"
android:title="three" />
</group>
</menu>
這里的效果圖就不好看了
activity_main.xml 的修改
首先,先把ConstraintLayout 改為 DrawerLayout
指定一個id,這個就隨意了
然后,用include 把上面做的app_bar_main.xml 文件引入
再添加一個 NavigationView ,這就是放側(cè)邊欄的容器了
記得指定id,再為它添加表頭xml,和menu(可以直接在屬性欄哪里設(shè)置)
還有一些處理,直接放代碼查看吧
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/drawer_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
效果圖
菜單選項(xiàng)頁面
為了每一個菜單都有一個頁面與之對應(yīng),建立三個 Fragment (簡單Deomo ,使用BankFragment)
重復(fù)建立三個
在res添加一個navigatin
點(diǎn)擊添加我們的 Fragment
切記一定把每一個Fragment的ID改成和其對應(yīng)的Menu選項(xiàng)一致
在 content_main.xml 文件添加上 NavHostFragment
這里就不是一個鐵憨憨,可以把那個TextView刪除了
MainActivity 代碼
package com.shanya.drawerdemo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import androidx.appcompat.widget.Toolbar
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.navigation.NavigationView
class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
appBarConfiguration = AppBarConfiguration(setOf(
R.id.nav_one, R.id.nav_two, R.id.nav_three), drawerLayout)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.nav_host_fragment)
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
}