使用导航抽屉和工具栏

时间:2015-11-28 09:49:07

标签: android android-fragments material-design navigation-drawer floating-action-button

我可以使用toolbar创建一个应用,并使用navigation drawer滑动recycleView

现在我正在尝试添加Floating action button,但我无法确定添加按钮的正确位置。

我正在努力将fab右下角添加。

以下是我的activity_main.xml文件

     <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:id="@+id/container_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar" />
        </LinearLayout>

        <FrameLayout
            android:id="@+id/container_body"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>

    <fragment
        android:id="@+id/fragment_navigation_drawer"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer"
        tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

Fragment布局文件fragment_navigtion_drawer.xml是

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/drawerColor">

    <LinearLayout
        android:id="@+id/containerDrawerImage"
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        android:background="@drawable/drawertitlebackground">

        <ImageView
            android:id="@+id/drawerAppImage"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginTop="5dp"
            android:src="@mipmap/ic_launcher"
            android:layout_gravity="center"

             />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginLeft="5dp"
            android:text="Make search easy"
            android:id="@+id/drawerSubTitle"
            android:textStyle="bold|italic"
            android:layout_centerVertical="true"
            android:layout_gravity="bottom|center"
            android:textColor="@color/textColorPrimary" />

        <ImageButton
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginRight="10dp"
            android:layout_gravity="bottom|right"
            android:src="@android:drawable/ic_menu_preferences"/>

    </LinearLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/drawerList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_below="@+id/containerDrawerImage" />

</RelativeLayout>

以下是FAB的代码

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignBottom="@id/container_body"
        android:layout_gravity="right"
        android:layout_marginRight="10dp"
        app:rippleColor="#F06292"/>

我正在尝试将Linearlayout放在FrameLayout activity_main.xml下的fragment文件中。我可以看到按钮出现在右下方。 但是当我运行应用程序时,会显示一个高度相同的矩形条,它隐藏了它背后的内容。矩形条从左到右覆盖,包括fab按钮,但是fab按钮正在顶部。

背后的内容是Listview fragment。因此底部的条目被隐藏,因为fab按钮区域被覆盖为矩形。

我只需显示按钮,其他区域应覆盖listview数据,例如activity_main.xml

对于我来说,问题在于它的线索。

解决: 我可以通过以下方式重写 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:id="@+id/container_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content"> <include android:id="@+id/toolbar" layout="@layout/toolbar" /> </LinearLayout> <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <FrameLayout android:id="@+id/container_body" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"/> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignBottom="@id/container_body" android:layout_gravity="bottom|right" android:layout_marginRight="10dp" android:layout_marginBottom="6dp" app:fabSize="normal" android:src="@drawable/ic_add_white_24dp" app:backgroundTint="@color/colorPrimaryDark" app:rippleColor="#F06292"/> </FrameLayout> </LinearLayout> <fragment android:id="@+id/fragment_navigation_drawer" android:layout_width="280dp" android:layout_height="match_parent" android:layout_gravity="start" app:layout="@layout/fragment_navigation_drawer" tools:layout="@layout/fragment_navigation_drawer" /> </android.support.v4.widget.DrawerLayout> 来解决此问题。现在FAB按钮正确显示。下面是一个更新。

CircleCollider2D

0 个答案:

没有答案