【问题标题】:Android. ConstraintLayout, how to apply goneMargin安卓。 ConstraintLayout,如何应用goneMargin
【发布时间】:2022-01-25 06:30:24
【问题描述】:

我有以 ConstraintLayout 作为父级的布局。在我的约束布局中有三个视图:ImageView 和两个 TextView。我需要,根据 ImageView 和 TextView (1) 的可见性是否可见,第二个 TextView 的边距顶部会发生变化。这是我想要实现的视觉示例:

我尝试将goneMarginTop = 32 用于我的第二个TextView。但是,在这种情况下,一旦第一个 TextView 的可见性消失,我就会在 ImageView 和 TextView = 32 dp 之间获得边距,但预期为 12 dp。仅当 ImageView 和第一个 TextView 消失时,我才需要 margin = 32 dp。这是我的代码:

 <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/ivImage"
            android:layout_width="0dp"
            android:layout_height="0dp"  
            android:visibility="gone"
            app:layout_constraintDimensionRatio="H,1:1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:visibility="visible" />

        <TextView         
            android:id="@+id/tvTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="14dp"
            android:layout_marginTop="12dp"
            android:layout_marginEnd="14dp"
            android:gravity="center"
            android:textColor="@color/black"
            android:textSize="18sp"       
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/ivImage"
            app:layout_goneMarginTop="32dp" />

        <TextView
            app:layout_goneMarginTop="32dp"
            android:id="@+id/tvDesc"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="14dp"
            android:layout_marginTop="12dp"
            android:layout_marginEnd="14dp"         
            android:textColor="@color/black"
            android:textSize="17sp"        
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvTitle"          />

    </androidx.constraintlayout.widget.ConstraintLayout> 

是否可以使用布局中的 ConstraintLayout 来实现图像中的行为(不是以编程方式)。

请帮帮我。

【问题讨论】:

    标签: android android-layout textview margin android-constraintlayout


    【解决方案1】:

    您可以通过编程方式完成:

      val layoutParams = imageView.layoutParams as RelativeLayout.LayoutParams
      layoutParams.setMargins(100, 100, 100, 100)
    

    【讨论】:

      【解决方案2】:

      我认为没有办法仅使用 XML 和 gone 边距来满足您的要求。您可以按照建议以编程方式执行此操作,但您也可以使用BarrierSpace 小部件获得所需的间距。请参阅下面示例 XML 中的 cmets。 (我在32dp 添加了背景颜色和可见的水平标记以显示小部件的范围)

      <androidx.constraintlayout.widget.ConstraintLayout 
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
      
          <!--
              Space is set to the top of the layout with a height of 20dp.
          -->
          <Space
              android:id="@+id/space"
              android:layout_width="wrap_content"
              android:layout_height="20dp"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent" />
      
          <!--
              A barrier is set to the bottom of the Space above and the bottom TextView (tvDesc). The
              vertical placement of this barrier will vary from the bottom of tvTitle when 
              tvTitle is visible to 20dp (the bottom of the Space widget) when tvTitle is gone.
          -->
          <androidx.constraintlayout.widget.Barrier
              android:id="@+id/barrier"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              app:barrierDirection="bottom"
              app:constraint_referenced_ids="space,tvTitle" />
      
          <ImageView
              android:id="@+id/ivImage"
              android:layout_width="0dp"
              android:layout_height="0dp"
              android:background="@android:color/holo_blue_light"
              android:visibility="gone"
              app:layout_constraintDimensionRatio="H,1:1"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              tools:visibility="visible" />
      
          <TextView
              android:id="@+id/tvTitle"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_marginStart="14dp"
              android:layout_marginTop="12dp"
              android:layout_marginEnd="14dp"
              android:background="@android:color/holo_red_light"
              android:gravity="center"
              android:textColor="@color/black"
              android:textSize="18sp"
              android:visibility="gone"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toBottomOf="@id/ivImage"
              app:layout_goneMarginTop="20dp"
              tools:visibility="visible" />
      
          <!--
              The top constraint of tvDesc is set to the barrier and the top margin is set to 12dp. If
              tvTitle and the ImageView are gone, the top margin will still be 12dp. Since the 
              minimum vertical placement of the barrier is 20dp (the height of the Space widget) the 
              effective top margin for tvDesc will be 32dp (20dp + 12dp) as desired.
          -->
          <TextView
              android:id="@+id/tvDesc"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_marginStart="14dp"
              android:layout_marginTop="12dp"
              android:layout_marginEnd="14dp"
              android:background="@android:color/holo_green_light"
              android:textColor="@color/black"
              android:textSize="17sp"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toBottomOf="@id/barrier"
              tools:visibility="visible" />
      
          <View
              android:layout_width="0dp"
              android:layout_height="1dp"
              android:layout_marginTop="32dp"
              android:background="@android:color/black"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent" />
      
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      所有视图可见:

      tvTitle gone

      ImageView 和 tvTitle gone

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-19
        • 2015-07-13
        • 2012-04-16
        相关资源
        最近更新 更多