将浮动操作栏放在底部选项卡上方

时间:2016-05-13 04:37:22

标签: android android-layout floating-action-button

我有一个浮动动作按钮,但我需要将它放在底部标签上方。我尝试过增加底部边距或更改layout_anchor,但似乎没有任何效果。如何将浮动操作杆略微移动到底部标签上方,以便它不会覆盖它?

这是我的浮动动作栏xml:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_gravity="bottom|right|end"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    android:layout_width="wrap_content"
    android:src="@drawable/ic_add_white_24dp"
    app:layout_anchorGravity="bottom|right|end"
    app:layout_behavior=".FABScrollBehavior" />

screenshot

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:background="@color/color_primary"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            app:tabTextColor="@android:color/secondary_text_dark"
            app:tabSelectedTextColor="@android:color/primary_text_dark" />

    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_gravity="bottom|right|end"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:layout_width="wrap_content"
        android:src="@drawable/ic_add_white_24dp"
        app:layout_anchorGravity="bottom|right|end"
        app:layout_behavior=".FABScrollBehavior" />

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

2 个答案:

答案 0 :(得分:0)

取自https://stackoverflow.com/a/30879360/67808

您可以使用特定于API的样式轻松完成此操作。在普通值/ styles.xml中,输入如下内容:

<style name="floating_action_button">
    <item name="android:layout_marginLeft">0dp</item>
    <item name="android:layout_marginTop">0dp</item>
    <item name="android:layout_marginRight">8dp</item>
    <item name="android:layout_marginBottom">0dp</item>
</style>

然后在values-v21 / styles.xml下,使用:

<style name="floating_action_button">
    <item name="android:layout_margin">16dp</item>
</style>

并将样式应用于您的FloatingActionButton:

<android.support.design.widget.FloatingActionButton
...
style="@style/floating_action_button"
...
/>

答案 1 :(得分:-1)

在相对布局容器中取出所有这些并在FloatingActionButton中使用上面的属性然后取下边距并根据你进行调整。