简体   繁体   English

导航抽屉,工具栏和回收站是否都在同一x​​ml布局文件中查看?

[英]Navigation Drawer, Toolbar, and Recycler View all in same xml layout file?

I'm not good with android layout files and right now I have a file that has a Toolbar widget, and a recycler view widget. 我对android布局文件不好,现在我有一个包含工具栏小部件和回收视图小部件的文件。 Here's the code. 这是代码。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

     <android.support.v7.widget.Toolbar
       android:id="@+id/robot_chooser_toolbar"
       android:layout_width="match_parent"
       android:layout_height="?attr/actionBarSize"
       android:background="?attr/colorPrimary"
       android:elevation="4dp"
       android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

     <android.support.v7.widget.RecyclerView
       android:id="@+id/robot_recycler_view"
       android:paddingTop="5dp"
       android:scrollbars="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>

  <TextView
      android:id="@+id/robot_empty_view"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:textSize="20sp"
      android:text="@string/no_robots"
      android:elevation="3dp"
      android:layout_gravity="center"
      android:visibility="gone"
      android:gravity="center" />

</LinearLayout>

How could I add a DrawerLayout to this? 我如何添加一个DrawerLayout呢?

DrawerLayout has two parts DrawerLayout有两部分

  1. Main Content 主要内容
  2. Navigation Drawer 导航抽屉

In other words, it contains two child views, in which one act as MainContent and other act as Drawer . 换句话说,它包含两个子视图,其中一个充当MainContent ,另一个充当Drawer if you look at simple view 如果你看简单的看法

 <DrawerLayout>

   // VIEW 1 - MAIN CONTENT
    <RelativeLayout>

        //whatever you put in this, it will show in your main Content,
        // main Screen

    </RelativeLayout>

  //SECOND CHILD any view, it would be seen in your Side drawer

    <LinearLayout>

        //whatever you put inside, this will be seen on your drawerlayout

    </LinearLayout>
 </DrawerLayout>

在此处输入图片说明


In your case, suppose, you want to add fragment inside your Drawer then you can do as follows : 假设您想在Drawer添加片段,则可以执行以下操作:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="@+id/profileDrawer"
android:layout_height="match_parent" >

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

 <android.support.v7.widget.Toolbar
   android:id="@+id/robot_chooser_toolbar"
   android:layout_width="match_parent"
   android:layout_height="?attr/actionBarSize"
   android:background="?attr/colorPrimary"
   android:elevation="4dp"
   android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
   app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

 <android.support.v7.widget.RecyclerView
   android:id="@+id/robot_recycler_view"
   android:paddingTop="5dp"
   android:scrollbars="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent"/>

 <TextView
  android:id="@+id/robot_empty_view"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:textSize="20sp"
  android:text="@string/no_robots"
  android:elevation="3dp"
  android:layout_gravity="center"
  android:visibility="gone"
  android:gravity="center" />

 </LinearLayout>

<fragment
    android:layout_width="290dp"
    android:layout_gravity="end"
    android:layout_height="match_parent"
    android:name="yourFragmentPackageName"
    android:id="@+id/fragment"
    tools:layout="@layout/drawer_fragment" />

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM