【发布时间】:2022-01-25 18:29:01
【问题描述】:
我正在尝试使用 android studio(arctic ice) 和 kotlin 在活动中显示导航抽屉。当我在一个只有一个活动(主要活动)的新项目中尝试此代码时,它工作正常。如果我在我的项目中复制并粘贴相同的代码,则会显示活动,但没有应用栏/操作栏/导航抽屉。我的项目有多个活动,我从启动活动中使用导航栏调用最终活动。
Kotlin 活动代码
class LandingPage : AppCompatActivity() {
lateinit var toggle: ActionBarDrawerToggle
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_landing_page)
val drawerLayout: DrawerLayout = findViewById(R.id.drawerLayout)
val navView: NavigationView = findViewById(R.id.nav_view)
toggle = ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
navView.setNavigationItemSelectedListener {
when (it.itemId) {
R.id.firstmenu -> Toast.makeText(applicationContext, "Hello", Toast.LENGTH_SHORT)
.show()
}
true
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (toggle.onOptionsItemSelected(item)) {
return true
}
return super.onOptionsItemSelected(item)
}
NAV-HEADER.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/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold">
</TextView>
nav_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<item
android:id="@+id/firstmenu"
android:title="First"></item>
<item
android:id="@+id/secondmenu"
android:title="Second"></item>
<item
android:id="@+id/thirdmenu"
android:title="Third"></item>
landingpage.xml(活动)
<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/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LandingPage">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ACTIVITY 2"
android:textSize="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</TextView>
</androidx.constraintlayout.widget.ConstraintLayout>
<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"
app:menu="@menu/nav_menu">
</com.google.android.material.navigation.NavigationView>
我做错了什么?请帮忙。
【问题讨论】:
-
这可能是问题中的拼写错误,但我注意到您使用
activity_landing_page使用setContentView,而您说您的活动名为landingpage.xml。所以这意味着你需要改为setContentView(R.layout.landingpage) -
感谢您的阅读和回复。你指出的情况并非如此。非常抱歉误导了您。问题出在 Res-->Values-->Themes-->themes.xml 中的
。将其更改为 解决了这个问题。
标签: android-studio kotlin navigation-drawer