FAB(浮动操作按钮)不浮动 [英] FAB (floating action button) doesn't float

查看:97
本文介绍了FAB(浮动操作按钮)不浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用floatactionbutton元素,但是它不会在UI布局中浮动,浮动按钮占据布局的一部分,并且不在组件上方,在我的情况下是viewpager

I've tried to use the floatingactionbutton element but it doesn't float in UI layout, the floating button occupies part of the layout and isn't over the component, in my case is a viewpager,

按钮不浮动

这是我的布局:

<LinearLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.frases.frases.FraseMain"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/appbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:color/white" />

    <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="@android:drawable/ic_dialog_email" />


</LinearLayout>

推荐答案

这是因为您使用LinearLayout作为根布局.您需要FrameLayout来使元素彼此堆叠,而FAB必须是最后一个子节点,因为它需要保持在其他所有视图之上.

It's because you are using LinearLayout as the root layout. You need FrameLayout to stack elements over one another, and FAB needs to be the last child node because it needs to stay on top of every other views.

<FrameLayout>
  <LinearLayout>
    ...
  </LinearLayout>

  <!-- FAB be the last node inside FrameLayout here -->
  <android.support.design.widget.FloatingActionButton />
</FrameLayout>

这篇关于FAB(浮动操作按钮)不浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆