我试过使用 floatingactionbutton 元素,但它在 UI 布局中不 float , float 按钮占据布局的一部分并且不在组件上方,在我的例子中是一个 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 需要成为最后一个子节点,因为它需要位于所有其他 View 之上。
<FrameLayout>
<LinearLayout>
...
</LinearLayout>
<!-- FAB be the last node inside FrameLayout here -->
<android.support.design.widget.FloatingActionButton />
</FrameLayout>
关于android - FAB( float 操作按钮)不 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38194370/