【问题标题】:Bottom Align Floating Action Button - Constraint Layout底部对齐浮动操作按钮 - 约束布局
【发布时间】:2018-06-06 16:06:12
【问题描述】:

我只是想用 constraintlayout 做这种对齐,但我不确定这是否可能。

我该怎么做?

示例 xml

<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">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="320dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/com_facebook_profile_picture_blank_portrait" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/floatingActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    app:srcCompat="@android:color/holo_green_light" />

我的解决方法。

【问题讨论】:

  • 允许还是不允许是什么意思?
  • 一般用coordinator layout来完成。我的意思是这是否可能,如果可能的话,这是正确的方法。
  • 是的,一切皆有可能,除非它满足您的要求,否则它不会出错。这是可能的,我相信你已经做到了。 :)
  • @Umair 如果您的意思是我的解决方法,它可能会在不同的屏幕上显示不同。

标签: android android-layout android-constraintlayout


【解决方案1】:

FloatingActionButton 的顶部和底部都限制在ImageView 的底部将使其在边缘居中。此外,ImageViewandroid:layout_width="match_parent" 应更改为 0dp 以强制执行水平约束。示例XML 如下所示:

<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">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="320dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/com_facebook_profile_picture_blank_portrait" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="@id/imageView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imageView"
        app:srcCompat="@android:color/holo_green_light" />

</android.support.constraint.ConstraintLayout>

【讨论】:

  • 像魅力一样工作!谢谢。
【解决方案2】:

您可以使用以下代码通过约束布局来做到这一点。

<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">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="320dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/carrick" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="176dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/bcslogo" />
</android.support.constraint.ConstraintLayout>

否则您可以使用相对布局而不是约束,您可以将所有内容放在相对布局中,如下所示

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="20dp"
        android:id="@+id/notesbg"
        android:layout_gravity="bottom|end">
    <android.support.v7.widget.RecyclerView
        android:layout_marginTop="2dp"
        android:id="@+id/myRecyclernote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


        <android.support.design.widget.FloatingActionButton
            android:id="@+id/myFAB"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_gravity="end|bottom"
            android:layout_marginEnd="20dp"
            android:layout_marginBottom="50dp"
            app:backgroundTint="#00b4ed"
            android:src="@drawable/floatingbtn"
            app:borderWidth="0dp"
            app:elevation="8dp" />
    </RelativeLayout>  

【讨论】:

  • 你使用了android:layout_marginBottom="176dp",所以你的代码在我的屏幕上看起来不一样。
  • 那么您可以尝试使用相对布局,否则您的浮动按钮将显示在图像下方
  • 不,看 plaskoff 的回答。
猜你喜欢
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多