在xml中的Fragment中ListView上方的浮动操作按钮(FAB)?

4

我使用了这里的代码。 在我的情况下,它看起来像这样:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.FragmentList"
    android:orientation="vertical"
    android:id="@+id/linear">

    <ListView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:fab="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView" />

</LinearLayout>

<android.support.design.widget.CoordinatorLayout

    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_add_white_24dp"
    android:layout_gravity="bottom|end"
    fab:elevation="6dp"
    fab:pressedTranslationZ="12dp"
    fab:backgroundTint="@color/accent"
    app:layout_anchor="@id/listView"/>
</android.support.design.widget.CoordinatorLayout>

但我在Android Studio中有“多个根标签”。我错了什么吗?请帮帮我,我不明白。

将所有内容放入协调布局中。 - Vivek Mishra
谢谢!为什么android:layout_gravity="bottom|end"不起作用?FAB在左上角。 - Evgeny GooDi
3个回答

3
请使用以下布局:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.FragmentList"
android:orientation="vertical"
android:id="@+id/linear">

<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView" />


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_add_white_24dp"
    android:layout_gravity="bottom|end"
    fab:elevation="6dp"
    fab:pressedTranslationZ="12dp"
    fab:backgroundTint="@color/accent"
    app:layout_anchor="@id/listView"/>

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

android:fitSystemWindow="true"添加到CoordinatorLayout - Ahmed Mostafa

0

将此行代码更改为 android:layout_gravity="top" 而不是 android:layout_gravity="bottom|end"


0
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"

这将把 FAB 设置为右下角。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接