【问题标题】:Navigation Drawer, Toolbar, and Recycler View all in same xml layout file?Navigation Drawer、Toolbar 和 Recycler View 都在同一个 xml 布局文件中?
【发布时间】:2016-03-23 19:27:51
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    DrawerLayout 有两个部分

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

    换句话说,它包含两个子视图,其中一个充当MainContent,另一个充当Drawer。 如果你看简单的视图

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


    在您的情况下,假设您想在 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>
    

    【讨论】:

    • 感谢您的回复!底部的片段部分是干什么用的?
    • 你可以用任何东西替换。正如我上面解释的,DrawerLayout 有两个孩子,所以你可以用任何 View 替换它。在我的示例中,我只是添加了Fragment 来说明,您可以添加任何视图。
    • 好的,因为我要在导航抽屉中添加的是片段。因此,当您单击其中的两件事之一时,视图将更改为片段
    • 如果你想在抽屉里添加片段,那么我写的代码,你可以使用它。
    • 好的。我真正想要的是 recyclerview 卡片(卡片在不同的布局文件中)和工具栏将在开始时显示。然后按下导航抽屉按钮,或向右滑动抽屉打开
    猜你喜欢
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多