【问题标题】:Adjust my button slightly downward in Constraint Layout在约束布局中稍微向下调整我的按钮
【发布时间】:2021-03-15 05:02:53
【问题描述】:

我得到了以下布局,您也可以看到我的“查看更多”按钮与底部边缘线的垂直中心/中间完全对齐。

app:layout_constraintBottom_toBottomOf="@id/iv_content"
app:layout_constraintTop_toBottomOf="@id/iv_content"

现在,如果我想将“查看更多”按钮稍微向下推动大约 2% 的高度或 2dp,例如。

我试过 android:layout_marginTop="50dp" 没有什么不同

app:layout_constraintVertical_bias="0.8" 也不例外。

除了老派方法之外,我还能做什么“约束解决方案”

app:layout_constraintBottom_toBottomOf="parent"(申请btn_view_more)然后硬编码marginBottom并向上推

我知道这是解决方案之一,但我很想知道是否有任何我不知道的与约束相关的解决方案

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <ImageView
        android:id="@+id/iv_content"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginHorizontal="@dimen/_20sdp"
        android:layout_marginVertical="@dimen/_50sdp"
        android:background="@drawable/background_transparent_round_corner"
        android:scaleType="fitXY"
        android:src="@drawable/ic_launcher_background"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/iv_close"
        android:layout_width="@dimen/_30sdp"
        android:layout_height="@dimen/_30sdp"
        android:background="@drawable/background_circle"
        android:src="@drawable/ic_close"
        app:layout_constraintBottom_toTopOf="@id/iv_content"
        app:layout_constraintEnd_toEndOf="@id/iv_content"
        app:layout_constraintStart_toEndOf="@id/iv_content"
        app:layout_constraintTop_toTopOf="@id/iv_content" />

    <air.com.my.companyname.util.font.InterRegularButton
        android:id="@+id/btn_view_more"
        style="@style/ButtonSolidBlue"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="@dimen/_40sdp"
        android:layout_marginTop="50dp"
        android:text="View More"
        app:layout_constraintBottom_toBottomOf="@id/iv_content"
        app:layout_constraintEnd_toEndOf="@id/iv_content"
        app:layout_constraintStart_toStartOf="@id/iv_content"
        app:layout_constraintTop_toBottomOf="@id/iv_content" />
</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 有办法但不知道是不是你最终想要达到的。与其将“查看更多”按钮限制在图像视图的底部,不如将其限制在图像视图的顶部。这将使按钮以您的图像视图为中心。现在使用分数值大于 1 的垂直偏差,例如 '1.02' 等。
  • 我试过这个,但似乎 , 1 或 1.02 是对底部的约束,或者我会说向下移动,我试过放 -0.2 ,但似乎不可能,偏差 0.0 将停留在顶部(iv_content 的底部)
  • 值大于 1 导致部分按钮移出屏幕

标签: android android-constraintlayout


【解决方案1】:

将以下内容添加到按钮 XML:

android:translationY="2dp"

这会将按钮向下移动2dp。这种方法的缺点是任何限制在按钮顶部或底部的视图都将不会随着2dp 移位而移动,因为移位发生在布局后。该按钮仍将是 100% 可点击的,并且在其新位置仍可正常工作。

ConstraintLayout 不支持负边距,这将是一个不错的解决方案。如需其他解决方案,请查看here

【讨论】:

    【解决方案2】:

    将此添加到您的iv_content ImageView:

    android:paddingBottom="2dp"
    

    它会给你你想要的样子。 如果它在 ImageView 中与您的图像混淆,您可以像这样从iv_content 的底部边缘取出 2dp:

    android:layout_marginBottom="48dp"
    

    【讨论】:

      【解决方案3】:

      btn_view_more试试这个

              app:layout_constraintBottom_toBottomOf="@id/iv_content"
              app:layout_constraintEnd_toEndOf="@id/iv_content"
              app:layout_constraintStart_toStartOf="@id/iv_content"
              app:layout_constraintTop_toBottomOf="@id/iv_content"
      

      希望这会有所帮助。随时要求澄清...

      【讨论】:

      • 如果我没记错的话,这会使它位于白色区域内的某个地方,并且永远不要触摸内容区域
      • 按钮实际应该定位在哪里?
      • 与屏幕截图相同,但略微向下移动(部分按钮仍应覆盖内容)
      猜你喜欢
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 2021-07-25
      • 2018-10-16
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多