删除锚点约束时,如何在约束布局中删除视图的边距

发布于 2025-02-08 00:51:33 字数 3988 浏览 2 评论 0原文

我的约束视图如下,其图像在

<androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/SellerProfileContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="16dp"
            android:visibility="@{sellerFullName != null ? View.VISIBLE : View.GONE}"
            app:layout_constraintTop_toBottomOf="@id/Title"
            app:layout_constraintStart_toStartOf="@id/Title"
            app:layout_goneMarginTop="24dp">

            <ImageView
                android:id="@+id/SellerProfileImage"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                android:layout_marginEnd="16dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:contentDescription="@{sellerProfileImageAccessibilityText}"
                android:scaleType="centerCrop"
                android:visibility="@{sellerProfileImage != null ? View.VISIBLE : View.GONE}"
                android:src="@{sellerProfileImage}" />

            <TextView
                android:id="@+id/SellerProfile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:contentDescription="@{sellerFullName}"
                android:text="@{sellerFullName.text}"
                android:visibility="@{sellerFullName != null ? View.VISIBLE : View.GONE}"
                app:layout_constraintTop_toTopOf="@id/SellerProfileImage"
                app:layout_constraintBottom_toBottomOf="@id/SellerProfileImage"
                app:layout_constraintStart_toEndOf="@id/SellerProfileImage"
                app:layout_constraintEnd_toEndOf="parent"
                tools:text="Seller Profile Name"
                />

            <TextView
                android:id="@+id/SellerRatings"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{sellerFeedbackPercent}"
                android:contentDescription="@{sellerFeedbackPercent}"
                android:textColor="@color/style_guide_neutral6"
                android:visibility="@{sellerFeedbackPercent != null ? View.VISIBLE : View.GONE}"
                app:layout_constraintStart_toStartOf="@id/SellerProfile"
                app:layout_constraintTop_toBottomOf="@id/SellerProfile"
                tools:text = "100% positive rating"/>
        </androidx.constraintlayout.widget.ConstraintLayout>

我尝试实现布局,如下所示:

  1. 如果sellerProfileimage不是null,则sellererProfileimagesellerProfile在它们之间的保证金应为16DP。
  2. 如果sellerProfileimage为null,则sellerProfile在父(约束布局)和本身之间应为零。

我尝试了sellerProfile的以下方法

  1. ,我做了以下android:layout_marginstart =“ @{uxcontent.sellerprofileimage!= null? @dimen/margin_16dp: @dimen/margin_16dp: @dimen/margin_0dp} > Margin_16DPMargin_0dp分别等于16DP和0DP。

但是,当我编译代码时,我会收到以下错误,该错误

Cannot find a setter for <android.widget.TextView android:layout_marginStart> that accepts parameter type 'float'

If a binding adapter provides the setter, check that the adapter is annotated correctly and that the parameter type matches.

如何根据是否存在卖方配置文件有条件地调整布局的开始边距?

I have a constraint view as follows, the image of which is given below

<androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/SellerProfileContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="16dp"
            android:visibility="@{sellerFullName != null ? View.VISIBLE : View.GONE}"
            app:layout_constraintTop_toBottomOf="@id/Title"
            app:layout_constraintStart_toStartOf="@id/Title"
            app:layout_goneMarginTop="24dp">

            <ImageView
                android:id="@+id/SellerProfileImage"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                android:layout_marginEnd="16dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:contentDescription="@{sellerProfileImageAccessibilityText}"
                android:scaleType="centerCrop"
                android:visibility="@{sellerProfileImage != null ? View.VISIBLE : View.GONE}"
                android:src="@{sellerProfileImage}" />

            <TextView
                android:id="@+id/SellerProfile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:contentDescription="@{sellerFullName}"
                android:text="@{sellerFullName.text}"
                android:visibility="@{sellerFullName != null ? View.VISIBLE : View.GONE}"
                app:layout_constraintTop_toTopOf="@id/SellerProfileImage"
                app:layout_constraintBottom_toBottomOf="@id/SellerProfileImage"
                app:layout_constraintStart_toEndOf="@id/SellerProfileImage"
                app:layout_constraintEnd_toEndOf="parent"
                tools:text="Seller Profile Name"
                />

            <TextView
                android:id="@+id/SellerRatings"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{sellerFeedbackPercent}"
                android:contentDescription="@{sellerFeedbackPercent}"
                android:textColor="@color/style_guide_neutral6"
                android:visibility="@{sellerFeedbackPercent != null ? View.VISIBLE : View.GONE}"
                app:layout_constraintStart_toStartOf="@id/SellerProfile"
                app:layout_constraintTop_toBottomOf="@id/SellerProfile"
                tools:text = "100% positive rating"/>
        </androidx.constraintlayout.widget.ConstraintLayout>

Constraint layout image

I am trying implement the layout as follows:

  1. If the sellerProfileImage is not null, then sellerProfileImage and sellerProfile should have a margin of 16dp between them.
  2. If the sellerProfileImage is null, then sellerProfile should have zero margin between the parent (constraint layout) and itself.

I tried the following approach

  1. For sellerProfile, I did the following android:layout_marginStart="@{uxContent.sellerProfileImage != null ? @dimen/margin_16dp : @dimen/margin_0dp}" where the margin_16dp and margin_0dp equals 16dp and 0dp respectively.

But when I compiled the code, I got the following error

Cannot find a setter for <android.widget.TextView android:layout_marginStart> that accepts parameter type 'float'

If a binding adapter provides the setter, check that the adapter is annotated correctly and that the parameter type matches.

How can I conditionally adjust the start margin of the layout depending on whether the seller profile is present or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假面具 2025-02-15 00:51:33

您将需要调整布局中的边距,以利用“消失”边距。看
“ noreferrer”>“ noreferrer”> Margins> Margins> Margins> Margins当连接到gothing dogget

这是您的布局的简化版本:

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/SellerProfileContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="16dp"
    android:background="@android:color/holo_blue_light"
    app:layout_constraintEnd_toEndtOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_goneMarginTop="24dp">

    <ImageView
        android:id="@+id/SellerProfileImage"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_launcher_foreground"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/SellerProfile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="sellerFullName"
        android:visibility="@{sellerFullName != null ? View.VISIBLE : View.GONE}"
        app:layout_constraintBottom_toBottomOf="@id/SellerProfileImage"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@id/SellerProfileImage"
        app:layout_constraintTop_toTopOf="@id/SellerProfileImage"
        app:layout_goneMarginStart="0dp"
        tools:text="Seller Profile Name" />

</androidx.constraintlayout.widget.ConstraintLayout>

我对两个小部件的边缘进行了调整。关键更改是设置app:layout_gonemarginstart =“ 0dp” textView 上。当图像为gone时,这将设置为0DP

16DP当图像为可见时。

您应该能够在不使用数据绑定的情况下完成布局的这一方面。

You will want to adjust the margins in your layout to make use of "gone" margins. See
Margins when connected to a GONE widget.

Here is a simplified version of your layout as an example:

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/SellerProfileContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="16dp"
    android:background="@android:color/holo_blue_light"
    app:layout_constraintEnd_toEndtOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_goneMarginTop="24dp">

    <ImageView
        android:id="@+id/SellerProfileImage"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_launcher_foreground"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/SellerProfile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="sellerFullName"
        android:visibility="@{sellerFullName != null ? View.VISIBLE : View.GONE}"
        app:layout_constraintBottom_toBottomOf="@id/SellerProfileImage"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@id/SellerProfileImage"
        app:layout_constraintTop_toTopOf="@id/SellerProfileImage"
        app:layout_goneMarginStart="0dp"
        tools:text="Seller Profile Name" />

</androidx.constraintlayout.widget.ConstraintLayout>

I made adjustments to the margins of the two widgets. The key change is setting app:layout_goneMarginStart="0dp" on the TextView. This will set the start margin to 0dp when the image is gone.

enter image description here

and to 16dp when the image is visible.

enter image description here

You should be able to accomplish this aspect of your layout without the use of data binding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文