如何使浮动动作栏居中

时间:2019-01-31 08:37:55

标签: java android xml android-studio android-layout

您能帮我如何制作这样的浮动动作栏,并将其固定在中间位置吗?

我使用ConstraintLayout

img

4 个答案:

答案 0 :(得分:0)

如果要在约束布局中将视图放置在屏幕的底部中心,则需要在视图中添加以下内容:

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"

这是一个例子: layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/floatingActionButton2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:srcCompat="@android:color/holo_orange_dark" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/floatingActionButton3"
        app:layout_constraintStart_toEndOf="@+id/floatingActionButton"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:srcCompat="@android:color/holo_orange_dark" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/floatingActionButton4"
        app:layout_constraintStart_toEndOf="@+id/floatingActionButton2"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:srcCompat="@android:color/holo_orange_dark" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/floatingActionButton5"
        app:layout_constraintStart_toEndOf="@+id/floatingActionButton3"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:srcCompat="@android:color/holo_orange_dark" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/floatingActionButton4"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:srcCompat="@android:color/holo_orange_dark" />
</android.support.constraint.ConstraintLayout>

底部的视图链接在一起,所有视图约束到屏幕底部,第一个视图向左约束到父对象的左端,最后一个向右约束到右端。的父母。

答案 1 :(得分:0)

对于constraintlaout中的底部居中,将此约束设置为浮动按钮 如果要添加多个浮动按钮,请在其中添加所有此FloatingActionButton LinearLayout orientation =“ horizo​​ntal”并将此约束添加到LinearLayout 在图片中,这不是FloatingActionButton,而是视图,因此您可以根据需要添加

  app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"

答案 2 :(得分:0)

在约束布局内使用水平方向的线性布局。将线性布局约束到约束布局的底部,末端和起点。

这是一个简单的例子。

Layout

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/floating_action_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    android:orientation="horizontal">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/x"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

答案 3 :(得分:0)

删除水平偏差属性。

我猜你有这样的app:layout_constraintHorizontal_bias="0.76"

只需将其删除或将其设置为0.5