【问题标题】:Right and Bottom Constraint on FloatingActionButton makes it unseenFloatingActionButton 上的右下约束使其不可见
【发布时间】:2017-12-18 20:58:07
【问题描述】:

我在ConstraintLayout 中有一个FloatingActionButton,如下所示:

<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab_add_topic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/ic_add"
                app:layout_constraintRight_toLeftOf="parent"
                app:layout_constraintBottom_toTopOf="parent"
                android:foregroundGravity="right|bottom"
                android:layout_marginRight="@dimen/material_tooltip_margin_top"
                android:layout_marginBottom="@dimen/material_tooltip_margin_top"
                android:elevation="6dp"
                app:pressedTranslationZ="12dp"
                app:borderWidth="0dp"/>

</android.support.constraint.ConstraintLayout>

但是,当我使用 Android 数据绑定(在 Kotlin 类上)在 Fragment 上扩展布局时,它没有出现。

我试图实现这里提到的解决方案,仍然在ConstraintLayout:Bottom Align Floating Action Button

如果我用 Top 和 Left 约束来显示它看起来就好了。

我错过了什么?谢谢!

【问题讨论】:

    标签: android android-layout android-xml


    【解决方案1】:

    您的布局包含以下两个约束:

    app:layout_constraintRight_toLeftOf="parent"
    app:layout_constraintBottom_toTopOf="parent"
    

    这将在功能上将您的 FAB 的右下角定位在屏幕的左上角。换句话说,您已将 FAB 限制在屏幕外。

    很有可能您的意思是您希望 FAB 的右侧位于屏幕的右侧,而 FAB 的底部位于 bottom的屏幕。所以改用这些:

    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    

    【讨论】:

    • 您是否为 FAB 或父 ConstraintLayout 设置了附加属性?不幸的是,单独替换约束是行不通的。
    • 原来我有一个 ViewPager 的问题,它的边界远离屏幕。如果有人还没有,我会发布一个不同的问题。谢谢@Ben P。
    • 支持 RTL 使用 app:layout_constraintEnd_toEndOf="parent"
    【解决方案2】:

    FloatingActionButton可以对齐到父ConstraintLayout的所有角:

    FAB constraint relation XML markup
    top & start app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    top & end app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    bottom & start app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    bottom & end app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"

    要均匀定位,请使用layout_margin

    android:layout_margin="24dp"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-06
      • 2023-03-06
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      相关资源
      最近更新 更多