繁体   English   中英

浮动动作按钮锚

[英]Floating action button anchor

我需要在android中锚定浮动操作按钮,但有麻烦:fab将在navigationview中这是我的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>

我的anav_header导航视图只包含少量文本视图。 我需要fab来锚定标题的botom,正好在ND的标题和主要内容之间。 问题是layout_anchor仅在标题和导航视图位于同一布局layout_anchor有效。 如果我使用navigationview属性headerLayoutlayout_anchor不起作用。 此外,如果我使用NavigationView.addHeaderView() ,它也不起作用。 如果我通过这种方式在导航视图布局中包含标题

<include
        layout="@layout/anav_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

它工作,但标题和导航视图像不同的部分一样移动。 我需要它们像一个导航抽屉。 我需要导航抽屉看起来像那样。

我找到了自己的方式。 首先我删除了NavigationView并将其替换为drawer_header.xml其中包含带有标题图像的简单LinearLayout ,以及带有drawer_list的抽象列表,其中包含静态菜单项的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)

暂无
暂无

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

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