gpt4 book ai didi

Android ConstraintLayout - 如何将 View 移动到占用的空间?

转载 作者:行者123 更新时间:2023-11-29 23:46:26 25 4
gpt4 key购买 nike

请看这张图片,这是我目前创建的:

enter image description here

但我的问题是在某些时候颜色段可能会变得不可见或消失。而此时我需要将尺寸段移动到与颜色段相同的位置。我怎样才能做到这一点 ?基本上,我希望在颜色部分不可见/消失的情况下用尺寸部分代替颜色部分。

我尝试创建一个指南,但它是不变的,不会移动。我想知道障碍是否有效?

最佳答案

我可以想到两种方法。一种只使用 XML 但不太灵活,第二种需要对代码进行一些更改并提供对 Views 的更多控制。请注意,只有当颜色段的可见性设置为 GONE 时,这两种方法才会起作用,因为 INVISIBLE 的 View 仍然占用空间。

  1. 我们将两个 View 的宽度百分比设置为 50%。当颜色segment is GONE Size 段将向左滑动,因为水平偏差。如果您设置此解决方案可能无法正常工作这些 View 的边距,因为它们的大小固定为 50%布局的可用宽度。

    <?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
    android:id="@+id/color"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Color"
    app:layout_constraintWidth_percent="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintHorizontal_bias="0"/>

    <TextView
    android:id="@+id/size"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Size"
    app:layout_constraintWidth_percent="0.5"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@id/color"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintHorizontal_bias="0"/>

    </android.support.constraint.ConstraintLayout>
  2. 我们将两个 View 放在一个加权链中,但还添加了一个 Space 助手当'Color'时,它充当剩余空间的填充物 View 是 GONE。可见的 View 都具有权重0.5,每个占用可用空间的一半。 空间助手的权重为 0,因此不可见。

    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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout">

    <TextView
    android:id="@+id/color"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Color"
    app:layout_constraintEnd_toStartOf="@id/size"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintHorizontal_bias="0"
    app:layout_constraintHorizontal_weight="0.5"/>

    <TextView
    android:id="@+id/size"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="Size"
    app:layout_constraintEnd_toStartOf="@id/space"
    app:layout_constraintStart_toEndOf="@id/color"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintHorizontal_weight="0.5"/>

    <Space
    android:id="@+id/space"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@id/size"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintHorizontal_weight="0"/>

    </android.support.constraint.ConstraintLayout>

    现在当你需要隐藏你的颜色段时你会做一些事情像这样:

    ConstraintSet cs = new ConstraintSet();
    ConstraintLayout layout = findViewById(R.id.layout);
    Space space = findViewById(R.id.space);
    TextView color = findViewById(R.id.color);
    cs.clone(layout);
    cs.setHorizontalWeight(space.getId(), 0.5f);
    cs.applyTo(layout);
    color.setVisibility(View.GONE);

    这将使颜色段消失,但尺寸段将向左滑动,因为约束甚至被保留然后 View 的可见性设置为 GONESpace 有它的权重设置为 0.5 并将占用空间的右半部分。

关于Android ConstraintLayout - 如何将 View 移动到占用的空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51332003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com