【问题标题】:Cannot change visibilty of a View that is being referenced inside a constraintlayout Group无法更改在约束布局组内引用的视图的可见性
【发布时间】:2020-01-10 10:11:38
【问题描述】:

无法更改在内部引用的单个视图的可见性 约束布局组

使用 实现'androidx.constraintlayout:constraintlayout:2.0.0-beta4'

尝试使用 implementation 'androidx.constraintlayout:constraintlayout:1.1.3'(稳定版) 但不工作。

 <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusableInTouchMode="true">

        <View
            android:id="@+id/bgView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@drawable/full"
            app:layout_constraintBottom_toTopOf="@id/bulletinsRV"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.github.mikephil.charting.charts.PieChart
            android:id="@+id/chart"
            android:layout_width="0dp"
            android:layout_height="250dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:background="@color/white"
            android:onClick="@{(v)->handler.onClickPieChart(v)}"
            app:centerText="@{`Total Due:`+  vm.home.payment.totalAmount +` AED`}"
            app:centerTextSize="@{vm.home.payment.paymentStatus.equals(`FullyPaid`)?0F:12F}"
            app:description="@{vm.home.payment.paymentStatus.equals(`FullyPaid`)?``:`My payment`}"
            app:dueCleared="@{vm.home.payment.paymentStatus.equals(`FullyPaid`)}"
            app:goneWhen="@{vm.home.payment == null}"
            app:isRotationEnabled="@{false}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:pieEntries="@{vm.pieEntries}"
            app:touchEnabled="@{false}" />

        <androidx.constraintlayout.widget.Group
            android:id="@+id/group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:constraint_referenced_ids="bgView,chart"
            app:visibleIf="@{vm.isUnitInitialized}" />
    </androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 尝试使用这个实现 'androidx.constraintlayout:constraintlayout:1.1.3' 修改 build.gradle 中的代码
  • 使用了这个,但它不起作用

标签: android android-layout android-constraintlayout


【解决方案1】:

如果您在 developer’s website 上看到有关 Group 的文档,似乎分组负责可见性和 Elevation。

该类控制一组引用小部件的可见性。 [...] 组的可见性将应用于引用的小部件。这是一种轻松隐藏/显示一组小部件的便捷方式,无需以编程方式维护该组。

这个想法似乎是,如果不是所有视图都共享相同的可见性和高度,那么它们很可能不应该属于同一个组。

默认的 Constraint Helper 类负责设置视图在组中的可见性,似乎将相同的值应用于组的所有子项,因此您无法为某些视图单独覆盖它。

这是默认Helper中的相关代码:

 public void updatePreLayout(ConstraintLayout container) {
    int visibility = getVisibility();
    float elevation = 0;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        elevation = getElevation();
    }
    for (int i = 0; i < mCount; i++) {
        int id = mIds[i];
        View view = container.getViewById(id);
        if (view != null) {
            view.setVisibility(visibility);
            if (elevation > 0 && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                view.setElevation(elevation);
            }
        }
    }
}

(source)

为了实现您想要的,您需要创建多个组,一个包含您想要更改的单个视图,另一个包含所有视图。如果要更改一个视图的可见性,则更改较小组的可见性,否则,更改包含所有视图的组的可见性。

在您只有两个视图的情况下,这似乎是一个荒谬的想法。所以我认为或者你必须创建你的自定义约束助手。

编辑: Here's a discussion on the same topic

【讨论】:

    猜你喜欢
    • 2017-11-10
    • 2019-09-24
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    相关资源
    最近更新 更多