android – FAB(浮动动作按钮)不浮动

前端之家收集整理的这篇文章主要介绍了android – FAB(浮动动作按钮)不浮动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图使用floatingactionbutton元素,但它不会在UI布局中浮动,浮动按钮占据布局的一部分而不是在组件上,在我的情况下是一个viewpager,

button doesn’t float

这是我的布局:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. tools:context="com.frases.frases.FraseMain"
  7. android:orientation="vertical">
  8.  
  9. <android.support.v7.widget.Toolbar
  10. xmlns:android="http://schemas.android.com/apk/res/android"
  11. xmlns:app="http://schemas.android.com/apk/res-auto"
  12. android:id="@+id/appbar"
  13. android:layout_height="wrap_content"
  14. android:layout_width="match_parent"
  15. android:minHeight="?attr/actionBarSize"
  16. android:background="?attr/colorPrimary"
  17. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  18. app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >
  19.  
  20. </android.support.v7.widget.Toolbar>
  21.  
  22. <android.support.design.widget.TabLayout
  23. android:id="@+id/sliding_tabs"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. app:tabMode="fixed"/>
  27.  
  28. <android.support.v4.view.ViewPager
  29. android:id="@+id/viewpager"
  30. android:layout_width="match_parent"
  31. android:layout_height="0dp"
  32. android:layout_weight="1"
  33. android:background="@android:color/white" />
  34.  
  35. <android.support.design.widget.FloatingActionButton
  36. android:id="@+id/fab"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:layout_gravity="bottom|end"
  40. android:src="@android:drawable/ic_dialog_email" />
  41.  
  42.  
  43. </LinearLayout>

解决方法

这是因为您使用LinearLayout作为根布局.您需要FrameLayout将元素堆叠在一起,并且FAB需要是最后一个子节点,因为它需要保持在每个其他视图之上.
  1. <FrameLayout>
  2. <LinearLayout>
  3. ...
  4. </LinearLayout>
  5.  
  6. <!-- FAB be the last node inside FrameLayout here -->
  7. <android.support.design.widget.FloatingActionButton />
  8. </FrameLayout>

猜你在找的Android相关文章