web-dev-qa-db-ja.com

フローティングアクションボタンを中央に揃える方法

FloatingActionButtonが1つあります。 2つのLinearLayoutsの中央で、このように画面の中央にします。 enter image description here

現在私のデザインはこんな感じです

enter image description here

画面の正確な中央に配置したいと思います。どうやってするか?

これは私の全体のxmlコードです

<Android.support.design.widget.CoordinatorLayout
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">
<LinearLayout Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:orientation="vertical">
    <LinearLayout
        Android:id="@+id/viewA"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:layout_weight="1.6"
        Android:orientation="horizontal"/>
    <LinearLayout
        Android:id="@+id/viewB"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:layout_weight="0.2"
        Android:orientation="horizontal"/>
</LinearLayout>
<Android.support.design.widget.FloatingActionButton
    Android:id="@+id/fab"
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:clickable="true"
    Android:src="@drawable/ic_plus"
    app:layout_anchor="@id/viewA"
    Android:layout_centerHorizontal="true"
    app:layout_anchorGravity="bottom"
    Android:layout_centerInParent="true" />
9
dev

あなたのCoordinatorLayoutFrameLayoutなので、この方法を試してみてください。お役に立てば幸いです。

 app:layout_anchorGravity="bottom|center"
10
IntelliJ Amiya

上記の解決策はどういうわけか私にはうまくいきませんでした。 Android:layout_gravityをに変更しました

Android:layout_gravity="bottom|center"

それが役に立てば幸い :-)

2
Mustafa Yousef

Layout_centerHorizo​​ntalとlayout_centerInParentの設定は、CoordinatorLayoutでは機能しません。これらはRelativeLayoutでのみ許可されます。あなたはこのようにそれをするべきです:

app:layout_anchorGravity="center|bottom"
1
Hussein El Feky

これに変更

app:layout_anchor="@id/viewA"
Android:layout_centerHorizontal="true"
0