【问题标题】:Android - Add navigation drawer to multiple activitiesAndroid - 向多个活动添加导航抽屉
【发布时间】: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


【解决方案1】:

要向多个活动添加导航抽屉,而不需要重新编写每个活动的布局,您可以利用基本活动类和ViewStub 标记。

创建这样的布局(我稍后将其称为navdrawer_activity.xml):

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="start">

    <ViewStub
        android:id="@+id/navdrawer_stub"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <!-- 
         your drawer here
         you can use android.support.design.widget.NavigationView
         or some other view if you want 
    -->

</android.support.v4.widget.DrawerLayout>

然后为所有要扩展的活动创建一个类:

public class NavDrawerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.navdrawer_activity);
    }

    @Override
    public final void setContentView(@LayoutRes int layoutResID) {
        ViewStub stub = findViewById(R.id.navdrawer_stub);
        stub.setLayoutResource(layoutResID);
        stub.inflate();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // set up nav drawer as normal, using whatever
        // callback methods or helper methods you want
    }
}

有了这些,您唯一需要做的就是更改您希望导航抽屉扩展NavDrawerActivity 而不是AppCompatActivity 的任何活动。

【讨论】:

    【解决方案2】:

    正如您包含的指南所说:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout ...>
    
        <!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
        <content goes here
            ... />
    
        <!-- Container for contents of drawer -->
        <left pane of drawer goes here
            ...
            android:layout_gravity="start" />
    
    </android.support.v4.widget.DrawerLayout>
    

    您可以将抽屉的左窗格定义为复合视图,将内容定义为复合视图。有点像这样:

    your_view.xml

    <YourView 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="match_parent">
        <Button android:id="@+id/..."
            android:layout_width="..."
            android:layout_height="..."
            android:layout_centerInParent="true"/>
    </YourView>
    

    public class YourView extends RelativeLayout {
        public YourView(Context context) {
            super(context);
        }
    
        public YourView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        // ... two more constructors are possible here
    
        private Button button;
    
        @Override
        public void onFinishInflate() {
            super.onFinishInflate();
            button = findViewById(R.id.button);
            button.setOnClickListener((view) -> {
                ...
            }
        }
    }
    

    您可以对左窗格的视图执行相同的操作。确保在其 xml 中包含 android:layout_gravity="start" 作为 &lt;YourLeftPane 的属性。


    现在你可以在你的activity_whatever.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout ...>
    
        <include layout="@layout/your_view"/>
    
        <include layout="@layout/your_left_pane" />
    
    </android.support.v4.widget.DrawerLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-22
      • 2013-09-12
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多