【问题标题】:goneMargin is not respected in a wrap_content ConstraintLayout?在 wrap_content ConstraintLayout 中不尊重goneMargin?
【发布时间】:2017-11-06 09:12:47
【问题描述】:

我有一个ConstraintLayout,它的高度是wrap_content

我希望它的高度能够根据其 Child 的高度折叠或展开。足够简单和常见,对吧?

现在我的布局如下所示:
(首先请忽略底部异常的超大margin,可以看到margin只有16dp,但是预览渲染的margin非常大。)

我的问题是,如果大矩形的可见性设置为gone
根据ConstraintLayout 的文档,如果我将其goneMarginTop 设置为某个值,即使其可见性为gone,它也会保留该边距。这样我的Request Date 将在父级底部留出一些空间。

但是,这并没有按预期工作。 Request Date 粘在其父级的底部:
(这又是一个损坏的预览。在我的真实应用中,我可以看到完整的请求日期)

我做错了什么吗?这是我的完整代码:

<?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="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@color/colorBasicGrey"
    android:layout_marginBottom="2dp">

    <View
        android:id="@+id/item_indicator"
        android:layout_width="8dp"
        android:layout_height="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@color/white"
        android:layout_marginTop="24dp"
        android:layout_marginLeft="24dp"/>

    <TextView
        android:id="@+id/group_member_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/group_member_join_group_request"
        app:layout_constraintTop_toBottomOf="@id/item_indicator"
        app:layout_constraintBottom_toTopOf="@id/item_indicator"
        app:layout_constraintLeft_toRightOf="@id/item_indicator"
        android:layout_marginLeft="8dp"
        style="@style/general45"/>

    <TextView
        android:id="@+id/group_member_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/general45"
        android:textAllCaps="true"
        app:layout_constraintBaseline_toBaselineOf="@id/group_member_label"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="180dp"
        tools:text="ABCDEFGHIJK"/>

    <TextView
        android:id="@+id/group_request_date_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/group_member_request_date_label"
        app:layout_constraintTop_toBottomOf="@id/group_member_label"
        app:layout_constraintLeft_toLeftOf="@id/group_member_label"
        android:layout_marginTop="8dp"
        style="@style/general45"/>

    <TextView
        android:id="@+id/group_request_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/general45"
        android:textAllCaps="true"
        app:layout_constraintBaseline_toBaselineOf="@id/group_request_date_label"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="180dp"
        tools:text="28/10/2017"/>

    <LinearLayout
        android:id="@+id/admin_button_container"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginTop="12dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/group_request_date_label"
        app:layout_constraintVertical_bias="0.0"
        app:layout_goneMarginTop="16dp"
        app:layout_goneMarginLeft="24dp"
        app:layout_goneMarginRight="24dp"
        app:layout_goneMarginBottom="0dp"
        android:visibility="gone">

        <!--To simplify the question, I hided elements inside this LinearLayout-->

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 我希望 Stackoverflow 需要人们给出拒绝投票的理由。

标签: android android-layout android-constraintlayout


【解决方案1】:

想通了。

goneMargin 用于指示到 GONE 目标的边距,而不是本身是 GONE。

因此实际上我应该将goneMargin 属性放在Request Date 中而不是大矩形中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    相关资源
    最近更新 更多