我试图让我的导航栏在滚动时出现在正文的顶部。目前它在下面,如图所示。
我还有一个抽屉导航,它在引入 ScrollView 后就停止工作了……我觉得这可能是因为当我单击“汉堡包”图标时,它实际上位于 ScrollView 后面,所以不起作用但不确定...只是想完成这一点然后我会找出我猜的原因。谢谢。
该页面的布局 xml 也在下方。
发生了什么:
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/RelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:theme="@style/AppTheme">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/NwApt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:background="@color/colorS"
android:padding="5dp"
android:text="New Appointment"
android:textColor="@color/PTextColour"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/activity_horizontal_margin"
android:orientation="vertical">
<EditText
android:id="@+id/NEngNme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="75dp"
android:background="@android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Engineer Name"
android:inputType="textPersonName" />
<EditText
android:id="@+id/NItm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Item"
android:inputType="textPersonName" />
<EditText
android:id="@+id/NDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Date"
android:inputType="date" />
<EditText
android:id="@+id/NTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Time"
android:inputType="time" />
<EditText
android:id="@+id/NLoc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Location"
android:inputType="textPersonName" />
<EditText
android:id="@+id/NTstNotes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Tester Notes"
android:inputType="textMultiLine" />
<EditText
android:id="@+id/NScdNotes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Scheduler Notes"
android:inputType="textMultiLine" />
<CheckBox
android:id="@+id/NntsChkBx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Show Notes at appointment" />
<CheckBox
android:id="@+id/NaptChkBx"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Missed Appointment" />
<LinearLayout
android:id="@+id/NButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="@+id/NSveNts"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save Notes" />
<Button
android:id="@+id/NSndEml"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Send Email" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
最佳答案
activity_edit_appointment.xml 包含一个 DrawerLayout
作为父 View ,其子后续 subview 为 CoordniatorLayout
和 NavigationView
像这样:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true">
<android.support.design.widget.CoordinatorLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/overview_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
app:layout_scrollFlags="enterAlways|scroll" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<include layout="@layout/your_contents" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
app:headerLayout="@layout/nav_header"
app:menu="@menu/navigationdrawer_main" />
</android.support.v4.widget.DrawerLayout>
your_contents.xml 将包含您要针对当前 Activity 显示的 View 。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">
<EditText
android:id="@+id/NEngNme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="75dp"
android:background="@android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Engineer Name"
android:inputType="textPersonName" />
<EditText
android:id="@+id/NItm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Item"
android:inputType="textPersonName" />
<EditText
android:id="@+id/NDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Date"
android:inputType="date" />
<EditText
android:id="@+id/NTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Time"
android:inputType="time" />
<EditText
android:id="@+id/NLoc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Location"
android:inputType="textPersonName" />
<EditText
android:id="@+id/NTstNotes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Tester Notes"
android:inputType="textMultiLine" />
<EditText
android:id="@+id/NScdNotes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Scheduler Notes"
android:inputType="textMultiLine" />
<CheckBox
android:id="@+id/NntsChkBx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Show Notes at appointment" />
<CheckBox
android:id="@+id/NaptChkBx"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Missed Appointment" />
<LinearLayout
android:id="@+id/NButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="@+id/NSveNts"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save Notes" />
<Button
android:id="@+id/NSndEml"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Send Email" />
</LinearLayout>
</LinearLayout>
你可以将你的抽屉导航放在这两个 xml 文件中:nav_header.xml
(会给你一个像 gmail 应用一样的标题)和 navigationdrawer_main.xml
(指定你的抽屉此菜单文件中的条目)
要为工具栏赋予标题,您可以这样做programatically或者只是将 android:label="Edit Appointment"
添加到 AndroidManifest.xml
中此 Activity 的 activity
标签。
关于android - 导航栏 - 滚动时不会在 body 前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47941613/