android - 抽屉导航、工具栏和回收器在同一个 xml 布局文件中查看所有内容?

标签 android xml android-recyclerview navigation-drawer android-toolbar

我不擅长 Android 布局文件,现在我有一个包含工具栏小部件和回收器 View 小部件的文件。这是代码。

<?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>

如何向其中添加 DrawerLayout?

最佳答案

DrawerLayout 有两部分

  1. 主要内容
  2. 抽屉式导航

换句话说,它包含两个 subview ,其中一个充当 MainContent,另一个充当 Drawer。 如果你看一下简单的 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>

enter image description here


就您的情况而言,假设您想在 Drawer 中添加 fragment ,然后 您可以执行以下操作:

<?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>

关于android - 抽屉导航、工具栏和回收器在同一个 xml 布局文件中查看所有内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36187056/

相关文章:

android - 从我的回收站 View 中删除最后一项时出现 IndexOutOfBoundsException

Android - 在 RecyclerView 的图像中点击动画作为 Whatsapp

java.lang.OutOfMemory 错误 - SD 卡图像导入 ImageView

java - Android - 从其他 Activity 中的 AsyncTask 调用时,ListFragment 中的上下文不起作用

android - Xamarin.Forms - ControlTemplate 不显示 ContentPresenter

java - 将 XML 文件转换为 Java 对象时出现 "Class has two properties of the same name"错误

android - 如何在android中将Zip文件从SD卡发送到邮件

xml - XSLT:文档分析:如何在文档中输出唯一路径?

python - xml.etree.ElementTree.ParseError -- 异常处理未捕获错误

java - Firebase 可扩展性 - 查询 DB 与自定义对象 ArrayList?