如何解决Navigation Drawer 和 ActionBar Android?
我正在 Kotlin 中开发一个 Android 应用程序,我需要在我的应用程序的所有活动中为侧栏菜单设置导航抽屉。由于其他规范,我无法使用 Android 推荐的 Fragments。 我已经阅读了本网站上针对此问题提出的许多问题,但似乎都没有。应用程序没有给我错误,但是在扩展 BaseActivity 的 Activity 中看不到 Drawer 和 ActionBar。
我写了代码,希望有人能救救我!
基础活动
open class BaseActivity : AppCompatActivity() {
var drawerLayout: DrawerLayout? = null
private var drawerList: ListView? = null
private var drawerToggle: ActionBarDrawerToggle? = null
private lateinit var toolbar: Toolbar
var items = arrayOf("Item1","Item2")
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_base)
// R.id.drawer_layout should be in every activity with exactly the same id.
drawerLayout = findViewById(R.id.drawer_layout)
toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
drawerToggle = object :
ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_closed) {
override fun onDrawerClosed(view: View) {
supportActionBar?.setTitle(R.string.app_name)
}
override fun onDrawerOpened(drawerView: View) {
supportActionBar?.setTitle(R.string.menu)
}
}
drawerLayout!!.setDrawerListener(drawerToggle)
supportActionBar!!.setdisplayHomeAsUpEnabled(true)
supportActionBar!!.setHomeButtonEnabled(true)
drawerList = findViewById<View>(R.id.left_drawer) as ListView
drawerList!!.adapter = ArrayAdapter<String>(
this,android.R.layout.simple_list_item_activated_1,items
) //Items is an ArrayList or array with the items you want to put in the Navigation Drawer.
drawerList!!.onItemClickListener =
OnItemClickListener { arg0,arg1,pos,arg3 ->
// Do what you want when an item from the Navigation Drawer is clicked
}
}
override fun onoptionsItemSelected(item: MenuItem): Boolean {
return if (drawerToggle!!.onoptionsItemSelected(item)) {
true
} else super.onoptionsItemSelected(item)
}
override fun onPostCreate(savedInstanceState: Bundle?) {
super.onPostCreate(savedInstanceState)
drawerToggle!!.syncState()
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
drawerToggle!!.onConfigurationChanged(newConfig)
}
}
仪表板活动
class DashboardActivity : BaseActivity() {
var modifyFlag: Boolean = false
private lateinit var binding: ActivityDashboardBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dashboard)
binding = DataBindingUtil.setContentView(
this,R.layout.activity_dashboard
)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.content,menu)
return true
}
override fun onoptionsItemSelected(item: MenuItem): Boolean {
// Handle presses on the action bar menu items
when (item.itemId) {
}
return super.onoptionsItemSelected(item)
}
}
activity_base.xml
<?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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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/Theme.AppTheme.PopupOverlay"/>
</com.google.android.material.appbar.AppBarLayout>
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp">
<!-- Put what you want as your normal screen in here,you can also choose for a linear layout or any other layout,whatever you prefer -->
</FrameLayout>
</LinearLayout>
<!-- The navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</androidx.drawerlayout.widget.DrawerLayout>
themes.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Base application theme. -->
<style name="Theme.AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#861258</item>
<item name="colorPrimaryVariant">#91115E</item>
<item name="colorOnPrimary">#090808</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#F6F8F8</item>
<item name="colorSecondaryVariant">#373636</item>
<item name="colorOnSecondary">@color/black</item>
</style>
<style name="Theme.AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="Theme.AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="Theme.AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
android_manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:roundIcon="@drawable/icon"
android:supportsRtl="true"
android:theme="@style/Theme.AppTheme.NoActionBar">
<activity android:name=".ui.dashboard.DashboardActivity" />
<activity android:name=".login.LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
有人可以帮我吗?
感谢您的耐心和帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。