【问题标题】:Constraint Layout's child margins约束布局的子边距
【发布时间】:2018-09-03 12:44:36
【问题描述】:

我正在尝试在约束布局中使用两个视图。但是一旦分配了边距,子视图就会出现未定义的行为。我们将不胜感激。

这是我的代码:

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

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:src="@drawable/ic_action_google_play"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/txtNew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="TextHere"
        android:textSize="24sp"
        app:layout_constraintLeft_toRightOf="@+id/img"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 您可以尝试填充 textview 而不是边距。
  • 感谢 Codesalot 先生。

标签: android android-constraintlayout


【解决方案1】:

在 constraintLayout 中使用 padding 而不是在每个子项中使用 layout_margin 来实现您的要求。

将您的代码更改为以下代码:

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

<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_action_google_play"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/txtNew"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextHere"
    android:textSize="24sp"
    app:layout_constraintLeft_toRightOf="@+id/img"
    app:layout_constraintTop_toTopOf="parent" />

this 链接将帮助您区分填充和边距。

【讨论】:

  • 因此强制为约束布局内的每个子视图使用填充。
  • 如果在父视图(ConstraintLayout)中使用padding,那么每个子元素都会有父视图中指定的padding值的layout_margin
【解决方案2】:

在 ConstraintLayout 中,要在任何一侧(左、上、右、下)应用边距,必须明确定义该侧的约束。如果您的情况,您希望 ImageView 的每一侧都有“16dp”边距,因此您需要为该 ImageView 的每一侧声明约束。您在 XML 中编写了以下内容:

<ImageView   
    android:layout_margin="16dp"       
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

像这样替换:

<ImageView
    android:id="@+id/img"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginLeft="16dp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="16dp"
     />

要使右边和底部的边距起作用,您还需要添加以下内容:

    app:layout_constraintRight_toLeftOf="@+id/txtNew"
    android:layout_marginRight="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="16dp"

TextView 也是如此。

【讨论】:

    【解决方案3】:

    在 Constraintlayout 中定义视图之间的边距时,请确保指定您希望该边距的位置:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:src="@drawable/ic_launcher_background"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/txtNew"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginTop="8dp"
            android:text="TextHere"
            android:textSize="24sp"
            app:layout_constraintStart_toEndOf="@+id/img"
            app:layout_constraintTop_toTopOf="parent" />
    
    </android.support.constraint.ConstraintLayout> 
    

    结果

    【讨论】:

    • 是的,我想要所有四个边的边距,但不是边距填充工作!!
    • 好的,简单提醒一下,padding 是视图内部的空间,margin 是视图外部的空间。
    • 这里有一个更好的解释:stackoverflow.com/questions/21959050/…
    猜你喜欢
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    相关资源
    最近更新 更多