我需要在 android 中锚定 float 操作按钮,但遇到了麻烦: fab 将位于导航 View 中 这是我的 Activity_main 布局。 它包括抽屉导航。
<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:background="@color/colorBackground">
<!-- The main content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer -->
<include
layout="@layout/nav_drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
抽屉式导航布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/clDrawer"
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"
android:background="@color/colorPrimary">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.NavigationView
android:id="@+id/navView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/menu_drawer_view"
app:headerLayout="@layout/anav_header"
android:background="@color/colorPrimary2"
app:itemIconTint="@color/colorIcon"
app:itemTextColor="@color/colorIcon"/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/navFAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/activity_horizontal_margin"
android:clickable="true"
android:src="@drawable/plus_icon"
app:backgroundTint="@color/colorIcon"
app:layout_anchor="@id/headerLayout"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
我的导航 View anav_header
仅包含少数 TextView 。
我需要将 fab 锚定到页眉底部,位于 ND 的页眉和主要内容之间。
问题是 layout_anchor
仅当标题和导航 View 处于同一布局时才有效。如果我使用 navigationview 属性 headerLayout
, layout_anchor
不起作用。另外,如果我使用 NavigationView.addHeaderView()
,它也不起作用。
如果我通过这种方式在导航 View 布局中包含标题
<include
layout="@layout/anav_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
它可以工作,但是标题和导航 View 像不同的部分一样移动。我需要它们像一个抽屉导航一样。 I need navigation drawer to lookes smth like that.
最佳答案
我找到了我的路。
首先,我删除了 NavigationView
并将其替换为 drawer_header.xml
,其中包含带有标题图像的简单 LinearLayout
,并在其后面添加了带有静态菜单项的 LinearLayout
的抽屉列表。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:background="@color/colorPrimary2">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="@color/colorPrimary2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- <android.support.design.widget.NavigationView
android:id="@+id/navView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary2"
app:headerLayout="@layout/drawer_header"
app:itemIconTint="@color/colorIcon"
app:itemTextColor="@color/colorIcon"
app:menu="@menu/menu_drawer_view" />-->
<include
layout="@layout/drawer_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_horizontal_margin"/>
<include
layout="@layout/layout_drawer_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/navFAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:clickable="true"
android:src="@drawable/plus_icon"
app:backgroundTint="@color/colorIcon"
app:layout_anchor="@id/headerLayout"
app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>
所以,我将标题和菜单设置为LinearLayout
,否则它们都滚动了,但我需要它们一起滚动。要在标题或菜单中添加元素,您可以使用 addView(View v)
。
关于java - float 操作按钮 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36623992/