【问题标题】:Aligning the FAB at bottom right corner在右下角对齐 FAB
【发布时间】:2015-07-31 10:32:22
【问题描述】:

我有这个布局文件。我正在尝试添加FloatingActionButton。我无法将它放在右下角。

<?xml version="1.0" encoding="utf-8"?>
<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/action_bar_size"
            android:background="@color/myPrimaryColor"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_scrollFlags="scroll|enterAlways" />

        <TextView
            android:id="@+id/offline_msg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#ff0000"
            android:gravity="center"
            android:padding="5dp"
            android:text="No Internet connection"
            android:textColor="@android:color/white" />

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_drawer_header"
    app:menu="@menu/navigation_drawer_menu" />

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

FAB 部分

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:src="@drawable/ic_create_white"
    app:elevation="3dp"
    app:fabSize="normal"
    app:pressedTranslationZ="6dp"
    app:rippleColor="@color/white" />

但无论我把它放在上面的布局中的什么地方,它都不会发生。任何帮助将不胜感激。

编辑在运行时FrameLayout 将被Fragment 替换

【问题讨论】:

  • 检查this是否有帮助

标签: android alignment material-design floating-action-button


【解决方案1】:

为什么你不尝试使用 FrameLayout 作为你的父布局,这样你就可以将 FAB 放在你想要的任何地方,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
 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.support.v4.widget.DrawerLayout 
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/action_bar_size"
            android:background="@color/myPrimaryColor"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_scrollFlags="scroll|enterAlways" />

        <TextView
            android:id="@+id/offline_msg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#ff0000"
            android:gravity="center"
            android:padding="5dp"
            android:text="No Internet connection"
            android:textColor="@android:color/white" />

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_drawer_header"
    app:menu="@menu/navigation_drawer_menu" />

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


<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:src="@drawable/ic_create_white"
app:elevation="3dp"
app:fabSize="normal"
app:pressedTranslationZ="6dp"
app:rippleColor="@color/white" />

</FrameLayout>

如您所见,您将 FAB 置于主布局之上 :)

希望对你有帮助:)

【讨论】:

    【解决方案2】:

    把它放在 NavigationView 之后

    <FrameLayout
        android:id="@+id/fab_rootLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:layout_marginBottom="@dimen/fab_margin_bottom"
            android:layout_marginRight="@dimen/fab_margin_right"
            android:src="@drawable/ic_add_shopping_cart_white_18dp"
            app:backgroundTint="@color/accent"
            app:borderWidth="0dp"
            app:elevation="6dp"
            app:fabSize="normal"
            app:pressedTranslationZ="12dp"
            app:rippleColor="@android:color/white"/>
    
    </FrameLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      • 2017-05-13
      • 1970-01-01
      相关资源
      最近更新 更多