【问题标题】:Floating action button (FAB) above a ListView in a Fragment in xml?xml片段中ListView上方的浮动操作按钮(FAB)?
【发布时间】:2016-08-26 06:54:05
【问题描述】:

我使用了来自here 的代码。 就我而言,它看起来像

<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 中有“多个根标签”。我做错了什么? 请帮帮我,我看不懂((

【问题讨论】:

  • 将所有内容放入协调器布局中
  • 谢谢!为什么 android:layout_gravity="bottom|end" 不起作用? FAB 位于左侧和顶部

标签: android xml listview android-fragments floating-action-button


【解决方案1】:

改用这个布局:

<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
【解决方案2】:

修改这行代码 android:layout_gravity="底部|结束" 到 android:layout_gravity="top"

【讨论】:

    【解决方案3】:
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    

    这会将 FAB 设置为右下角。

    【讨论】:

      猜你喜欢
      • 2015-12-20
      • 2023-03-15
      • 2016-03-15
      • 1970-01-01
      • 2015-06-25
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多