【问题标题】:Place ImageView on top of button in ConstraintLayout将 ImageView 放在 ConstraintLayout 中的按钮顶部
【发布时间】:2019-09-20 21:01:24
【问题描述】:

将ImageView 或FrameLayout 放在Button 上时,我遇到了constraintlayout 的问题。

当我尝试在 FrameLayout 上填充 fragment 时,作为 Activity 一部分的按钮在片段 UI 顶部可见。

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


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button"/>

    <FrameLayout
        android:background="@color/blue"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 看到这个答案stackoverflow.com/a/47987945/3286819 很可能你的Button 超过你的FrameLayout 因为海拔。
  • 嗨..你期待什么结果,你能指定吗?
  • 其实这个框架布局并不是在按钮之上,而是占据了整个屏幕
  • 我的意思是 FrameLayout 必须在按钮上方(应该屏蔽按钮)
  • 我发现默认情况下按钮的高度是 2dp,所以我将 FrameLayout 提升到 4dp,我的问题就解决了

标签: android android-fragments android-constraintlayout


【解决方案1】:

试试下面的代码

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


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="638dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:background="@color/appGrey"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button">

    </FrameLayout>

</android.support.constraint.ConstraintLayout>

【讨论】:

    【解决方案2】:

    您的代码看起来不错,但如果您想在 ConstraintLayout 中告诉 1 个视图位于另一个视图之上,您可以转到设计选项卡并将较高的部分移到较低的视图上方:

    示例:

    在图片中,你可以看到按钮被放置在frameLayout下方,所以他会低于frameLayout(frameLayout会在按钮上方)


    另外,您的按钮缺少一些限制:

      <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/frameLayout2" />
    
    <FrameLayout
        android:id="@+id/frameLayout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
    

    如果所有这些都不能帮助您检查 this thread,正如 @Xavier Rubio Jansana 所说,那可能是因为海拔高度。

    【讨论】:

      【解决方案3】:

      要设置按钮和 FrameLayout 的约束, 按钮:

          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
      

      框架布局:

          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/button"
      

      【讨论】:

        【解决方案4】:

        要将任何视图置于另一个视图之上,您必须提供适当的约束,如下所示。

        适当地给予顶部和底部约束,并且当我将所有约束(开始、结束、顶部、底部)赋予 FrameLayout 时,它的高度和宽度赋予 0dp(你不能使用高度来 ma​​tch_parent否则它会覆盖整个屏幕并重叠按钮)。

        >

        <?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"
                android:layout_width="match_parent" android:layout_height="match_parent">
        
        
            <Button
                    android:id="@+id/button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="button"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintBottom_toBottomOf="parent"/>
        
            <FrameLayout
                    android:background="@color/colorPrimary"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintBottom_toTopOf="@+id/button"/>
            
        </android.support.constraint.ConstraintLayout>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-12-03
          • 1970-01-01
          • 1970-01-01
          • 2015-02-21
          • 1970-01-01
          • 2013-02-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多