【发布时间】:2018-03-28 04:18:46
【问题描述】:
我是 Android / Kotlin 的新手,我有一个包含多个活动的应用程序。 到目前为止,我已经创建了所有需要的活动,并且应用程序运行良好,除了现在我需要添加一个导航抽屉。
来自 iOS,我设计了所有布局,认为我可以在之后添加导航抽屉:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="@color/backgroundColor"
android:backgroundTint="@color/backgroundColor">
<RelativeLayout
android:id="@+id/mainRelativeLayout"
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="@drawable/navbar_border"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/ivNavBarLogo"
android:layout_width="102dp"
android:layout_height="24dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="@string/empty_description"
app:srcCompat="@mipmap/navbar_logo"/>
<ImageButton
android:id="@+id/btnSideMenu"
android:layout_width="25dp"
android:layout_height="15dp"
android:layout_centerVertical="true"
android:layout_marginStart="15dp"
android:background="@mipmap/sidemenu_button"
android:contentDescription="@string/empty_description"
app:srcCompat="@mipmap/sidemenu_button"/>
</RelativeLayout>
<!-- STUFF TO BE ADDED THERE -->
</android.support.constraint.ConstraintLayout>
我的意图是创建一个 NavigationDrawer 活动并在点击每个活动的 btnSideMenu 时调用它。
但是,我这几天一直在尝试解决这个问题,但没有任何运气。我从没想过这样一个基本的功能会这么难实现。我尝试了几乎所有在这里找到的建议和示例,还有这个:https://developer.android.com/training/implementing-navigation/nav-drawer.html
我是否以错误的方式思考这个问题?有没有什么方法可以做到这一点,而不必根据 Android Studio 提供的模板或使用片段重新创建我的所有活动?
LE:忘了说我从所有活动中删除了 ActionBar。
【问题讨论】:
-
导航抽屉是您的活动(视图控制器)的一部分。因此,您需要将其视为您活动中的另一种观点。因此,它也需要在您的 xml 中。您提供的链接确实表明需要在 xml 中指定导航抽屉,并且您需要在活动中为它实现一些 Kotlin 代码。如果这一切让您感到困惑,请从 Android Studio 创建一个新活动并选择导航抽屉活动,它已为您完成所有样板,将您的所有逻辑移至它。
-
@rgv - 我创建了空活动。我没有导航抽屉。或者这不是你的意思?
-
@rgv - 从 AS 提供的模板中移动逻辑是我的第一个意图,我看到 NullPointerException 和运行时的应用程序迷恋。我会再试一次,到目前为止,这对我来说似乎是最好的主意。
-
右键单击您的应用模块,然后选择新建,然后从中选择活动,然后选择导航抽屉活动
-
看看这个question
标签: java android android-activity kotlin navigation-drawer