28

次のようなレイアウトを使用しているとします。

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/flexible_space_image_height"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:statusBarScrim="@android:color/transparent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                />

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

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/mainView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

    <android.support.design.widget.FloatingActionButton
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_marginBottom="20dp"
        app:fabSize="normal"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|center_horizontal"
        />

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

これはほぼ標準のCheesesquareサンプルです。ただし、FloatingActionButton を除いて、約 20 dp 上に移動したいと考えています。

ただし、これは機能しません。マージンやパディングなどを使用しても、ボタンは次のように常にアンカーの端の中央に配置されます。

FAB はマージンを無視します

意図したとおりに FAB を 20 dp 上に移動するにはどうすればよいですか?

4

6 に答える 6

1

FloatingActionButton以下のように固定するにはAppBarAppBar へのアンカーとしての Fab

を拡張しFloatingActionButtonてオーバーライドしますoffsetTopAndBottom

public class OffsetFloatingActionButton extends FloatingActionButton
{
    public OffsetFloatingActionButton(Context context)
    {
        this(context, null);
    }

    public OffsetFloatingActionButton(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public OffsetFloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom)
    {
        super.onLayout(changed, left, top, right, bottom);
        ViewCompat.offsetTopAndBottom(this, 0);
    }

    @Override
    public void offsetTopAndBottom(int offset)
    {
        super.offsetTopAndBottom((int) (offset + (getHeight() * 0.5f)));
    }
}
于 2016-06-22T12:21:36.213 に答える
0

両方layout_anchor layout_anchorGravityをパディングとともに使用することで、この問題を回避することができました。アンカー属性を使用すると、View別のビューに対する相対的な位置を設定できます。ただし、まったく同じように機能するとは限りませんRelativeLayout。詳細は後述します。

layout_anchor必要なものを配置する必要があることを指定しViewますView(つまり、これは、属性によって指定されたものViewに対して相対的に配置する必要があります)。次に、一般的なGravity値 ( 、、など)を使用して、電流が相対位置のどちら側に配置されるかを指定します。Viewlayout_anchorlayout_anchorGravityViewViewtopbottomcenter_horizontal

これら 2 つの属性だけを使用する場合の問題は、アンカーのある の中心が他の に対して相対的に配置されることです。たとえば、を の下部に固定するように指定した場合、実際には、FAB の中心が の下端に沿って配置されます。ViewViewFloatingActionButtonTextViewTextView

この問題を回避するために、FAB にパディングを適用して、FAB の上端がTextView.

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_anchor="@id/your_buttons_id_here"
    android:layout_anchorGravity="bottom"
    android:paddingTop=16dp" />

目的の効果を得るには、パディングを増やす必要がある場合があります。それが役立つことを願っています!

于 2018-09-09T20:29:57.897 に答える