android - 设置应用程序 :layout_constraintTop_toBottomOf on ConstraintLayout

标签 android android-constraintlayout

我认为仅通过使用 app:layout_constraintTop_toBottomOf="@+id/label_proximity" 就足以将 ImageView 设置在 TextView 下方。我是否需要使用 marginTop 来使 TextView 可见?还是我缺少任何属性?为什么在这种情况下约束不起作用?

<?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"
android:layout_margin="16dp"
tools:context=".MainActivity">

<TextView
    android:id="@+id/label_light"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/label_light"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/label_proximity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/label_proximity"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/label_light" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="50dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/label_proximity" />

</android.support.constraint.ConstraintLayout>

最佳答案

你在这里和那里缺少一些约束..

用下面的代码替换你的整个代码,你的布局就可以正常工作了..

<?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"
    tools:context=".MainActivity">

<TextView
    android:id="@+id/label_light"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="@string/label_light"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/label_proximity"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="@string/label_proximity"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/label_light" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/label_proximity" />

</android.support.constraint.ConstraintLayout>

如果它工作正常,请在这里更新。

关于android - 设置应用程序 :layout_constraintTop_toBottomOf on ConstraintLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51965281/

相关文章:

android - 单击推送通知android后打开 Activity

android - 从下到上和从右到左排列 ConstraintLayout 流引用

android - 约束布局 0dp 无法正确呈现

android - 屏幕外的文本android约束布局

android - 从机超时

android - Kotlin 代码堆栈跟踪显示 Java 行号

Android ListView多项选择在点击后不显示突出显示

当我尝试创建可绘制资源文件时,Android Studio 出现奇怪的错误

android - ConstraintLayout 中的 TextView 剪辑文本和 layout_margin 无法正确显示

Android Constraint Layout inside Scrollview inside Constraint Layout