Android:约束布局 - 比例调整后无法调整 ImageView 的 View 边界

标签 android android-constraintlayout

我正在使用 ConstraintLayout。

我有一个 ImageView ,我希望它是 16:9。在这个 ImageView 下方,我试图放置一个 TextView 。

现在,当 ImageView 设置为消失时,预计 TextView 会向上移动。但这没有发生。

有人可以尝试找出我的 XML 布局中的问题吗:

<?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:background="@color/dark_background"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:padding="15dp"
    tools:layout_width="300dp"
    tools:layout_height="300dp"
    >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"
        android:src="@drawable/logo"
        app:layout_constraintDimensionRatio="W,16:9"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintVertical_bias="1.0" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/image"
        android:ellipsize="marquee"
        android:maxLines="4"
        android:textSize="28sp"
        android:textColor="#000000"
        android:text="The is the test that does not seem to work"
        android:layout_marginTop="12.41dp"
        app:layout_goneMarginTop="0dp"/>


</android.support.constraint.ConstraintLayout>

最佳答案

我认为您看到的行为是 ConstraintLayout 中设计的。 gone View 似乎应该占用任何空间,这似乎违反直觉,但在 this issue 的评论 #8 中在问题跟踪器上发表了以下声明:

@herrbert74 no, tadej is correct -- this is indeed a bug. In this example where the view is constrained on all sides and goes to gone, it should resolves to a point -- but constraints should still be respected. Meaning, in this case it should be at the center of all the constraints. Currently, the widget only set its size to zero, which works on single constraints, but doesn't on dual constraints. Beta 5 will have the correct behavior.

如果我理解正确,当 Viewgone 并且它像你的那样被限制在顶部和底部时,View 缩小为零(一个点),但仍然遵守其约束条件。换句话说,View 成为布局中心附近的一个点。您的 TextView 对 ImageView 的底部有一个顶部约束,它占用零空间,但零空间位于布局的中间。

所以,这并没有真正帮助,但它确实解释了正在发生的事情。

做你想做的事情的一种方法是在你制作 ImageView gone 时删除 ImageView 的底部约束.如果没有 bottom 约束,ImageView 将 float 到布局的顶部并带走 TextView

关于Android:约束布局 - 比例调整后无法调整 ImageView 的 View 边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42757258/

相关文章:

android - 是否可以创建自定义视频播放器并将其放置在 Android 的 View 中?

android - "Empty suite" Espresso 测试

android - ConstraintLayout 不考虑最大宽度/高度与 DimensionRatio 的结合

android - 以编程方式更改约束布局中的高度?

android - 如何将渐变定义为圆形progressBar

android - onPrepareOptionsMenu 重复 ActionBar 中的项目

java - 如何将字符串写入txt文件并将其发送到服务器

Android:AlertDialog 中的固定宽度字体

android - 约束 View 内的 ListView 被剪切并且不滚动

android - Textview maxlines 取决于它的高度