我最近開始在 android studio 中為一個學校專案編程,但我似乎無法讓這個底部導航正常作業。我已經觀看了多個關于如何使用片段進行導航的視頻。問題是,每次我嘗試導航到片段時,它都會使圖示被選中,但不會呈現片段本身
選單項
<item android:id="@ id/mainActivity" android:enabled="true" android:icon="@drawable/receipt" android:title=""/> <item android:id="@ id/firstFragment" android:enabled="true" android:icon="@drawable/search" android:title="" />
主要活動
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavigationView);
val navController = findNavController(R.id.fragment)
bottomNavigationView.setupWithNavController(navController)
主要活動 XML
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@ id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:itemIconTint="@color/black"
app:itemTextColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:menu="@menu/menu"/>
<fragment
android:id="@ id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="411dp"
android:layout_marginRight="411dp"
android:layout_marginBottom="731dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@ id/bottomNavigationView"
app:navGraph="@navigation/my_nav"/>
導航
<activity
android:id="@ id/mainActivity"
android:name="nl.bunus.bunusmobileapp.MainActivity"
android:label="activity_main"
tools:layout="@layout/activity_main" />
<fragment
android:id="@ id/firstFragment"
android:name="nl.bunus.bunusmobileapp.firstFragment"
android:label="fragment_first"
tools:layout="@layout/fragment_first" />
uj5u.com熱心網友回復:
問題
你提供的代碼太不重要了,但這就是我重建你的目標的原因。確實發生了一些錯誤。主要是依賴和 gradle 版本。我認為最好將整個代碼作為片段提供,最后作為專案檔案下載提供。
解決方案
梯度和依賴
由于 gradle 很重要,我想更詳細地指出這一點。你的build.gradle("projectname"):
buildscript {
ext.kotlin_version = '1.3.61'
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
還有你build.gradle(module)的依賴:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.kotlinapplication"
minSdk 30
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
testImplementation 'junit:junit:4. '
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
}
活動
讓我們從MainActivity.kt包含BottomNavigationViewwithloadFragment()方法的 開始。
重要提示:不要忘記下binding import圖!:
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title=resources.getString(R.string.fragment1)
loadFragment(FirstFragment())
navigationView.setOnNavigationItemSelectedListener {
when(it.itemId){
R.id.navigation_appoint-> {
title=resources.getString(R.string.fragment1)
loadFragment(FirstFragment())
return@setOnNavigationItemSelectedListener true
}
R.id.navigation_new_appoint-> {
title=resources.getString(R.string.second_menu)
loadFragment(SecondFragment())
return@setOnNavigationItemSelectedListener true
}
R.id.navigation_settings-> {
title=resources.getString(R.string.third_menu)
loadFragment(ThirdFragment())
return@setOnNavigationItemSelectedListener true
}
}
false
}
}
private fun loadFragment(fragment: Fragment) {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.container, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
}
它的布局activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<FrameLayout
android:id="@ id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@ id/navigationView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="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_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
分段
現在我們已經Activity完成了,還Fragment剩下3 個。為簡單起見,我只是發布FirstFragment.kt它的布局fragment_first.xml。您需要為做同樣SecondFragment和ThirdFragment課程。
class FirstFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_first, container, false)
}
}
匹配布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF9800">
<TextView android:layout_width="match_parent"
android:layout_centerInParent="true"
android:textAlignment="center"
android:textSize="18sp"
android:text="@string/first_menu"
android:layout_height="wrap_content"/>
</RelativeLayout>
選單
現在,我們將創建一個menu名為bottom_navigation.xml在res/menu這樣的:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@ id/navigation_appoint"
android:icon="@drawable/ic_menu_black_24dp"
android:title="@string/fragment1"/>
<item
android:id="@ id/navigation_new_appoint"
android:icon="@drawable/ic_menu_black_24dp"
android:title="@string/fragment2"/>
<item
android:id="@ id/navigation_settings"
android:icon="@drawable/ic_menu_black_24dp"
android:title="@string/fragment3"/>
</menu>
字串和圖示
我們需要為TextViewin定義字串strings.xml:
<resources>
<string name="app_name">KotlinApplication</string>
<string name="fragment1">Fragment 1</string>
<string name="fragment2">Fragment 2</string>
<string name="fragment3">Fragment 3</string>
<string name="first_menu">First Fragment</string>
<string name="second_menu">Second Fragment</string>
<string name="third_menu">Third Fragment</string>
</resources>
也不要忘記添加圖示(在這里我只用一個所有片段)在res/drawable叫ic_menu_black_24dp。
下載專案
你可以在
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/391790.html
標籤:安卓 科特林 android-fragments android-jetpack
