android Xml 布局,可见性消失了调整我的父布局

Posted

技术标签:

【中文标题】android Xml 布局,可见性消失了调整我的父布局【英文标题】:android Xml layout, visibility gone resize my parent layout 【发布时间】:2020-12-24 18:26:20 【问题描述】:

我的 xml 布局有问题 我有一个开关,它应该隐藏/激活我的布局的可见性 但是当我隐藏我的布局时,不知何故我的根布局宽度被修改了

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
xmlns:app="http://schemas.android.com/apk/res-auto">

<!-- FREE COOLING  SETTINGS -->
<LinearLayout
    android:id="@+id/free_cooling_setting_linear"
    android:layout_
    android:layout_
    android:paddingStart="24dp"
    android:paddingEnd="24dp"
    android:orientation="vertical">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/presentation_relative"
        android:layout_
        android:layout_>

        <ImageView
            android:id="@+id/image_smoke"
            android:layout_
            android:layout_
            android:background="@drawable/smoke"
            android:contentDescription="smoke"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_
        android:layout_
        android:paddingTop="8dp">

        <TextView
            android:layout_
            android:layout_
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:text="@string/free_cooling_activated"/>

        <com.google.android.material.switchmaterial.SwitchMaterial
            android:id="@+id/free_cooling_activated"
            android:layout_
            android:layout_
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>


    <LinearLayout
        android:id="@+id/linear_freecooling_activated"
        android:layout_
        android:layout_
        android:visibility="gone"
        android:orientation="vertical">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/free_cooling_delta_relative"
            android:layout_
            android:layout_>

            <ImageView
                android:id="@+id/image_cool"
                android:layout_
                android:layout_
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:background="@drawable/delta_temp" />

            <TextView
                android:layout_
                android:layout_
                app:layout_constraintTop_toBottomOf="@id/image_cool"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_marginTop="8dp"
                android:text="@string/inf"
                android:textSize="12sp" />


            <EditText
                android:id="@+id/free_cooling_delta_temp"
                android:layout_
                android:layout_
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintRight_toLeftOf="@id/delta_temp_deg"
                android:layout_marginEnd="4dp"
                android:text="8.0"
                android:textColor="@color/black"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/delta_temp_deg"
                android:layout_
                android:layout_
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                android:text="@string/degrees"
                android:textColor="@color/black"
                android:textSize="12sp" />


        </androidx.constraintlayout.widget.ConstraintLayout>

        <View
            style="@style/dividerHorizontal"  android:layout_marginTop="8dp"/>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_
            android:layout_>

            <TextView
                android:layout_
                android:layout_
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:text="@string/free_cooling_temp_regulated"
                android:visibility="visible" />

            <com.google.android.material.switchmaterial.SwitchMaterial
                android:id="@+id/free_temp_regulated"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_
                android:layout_ />
        </androidx.constraintlayout.widget.ConstraintLayout>

        <LinearLayout
            android:id="@+id/free_cooling_relative_ventilationSpeed"
            android:layout_
            android:layout_
            android:layout_marginTop="16dp">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/free_cooling_ventilation_speed_min_layout"
                android:layout_
                android:layout_
                android:layout_weight="1"
                app:helperText="@string/pourcent"
                android:hint="@string/min"
                app:startIconTint="@color/black"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/free_cooling_ventilation_speed_min"
                    android:layout_
                    android:layout_
                    android:inputType="numberDecimal"
                    android:textSize="12sp"
                    android:maxLength="3"
                    android:digits="0123456789."/>
            </com.google.android.material.textfield.TextInputLayout>


            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/free_cooling_ventilation_speed_layout"
                android:layout_
                android:layout_
                app:startIconDrawable="@drawable/ventilo_32"
                app:helperText="@string/pourcent"
                android:layout_weight="1.5"
                android:hint="@string/speed"
                android:layout_marginStart="8dp"
                app:startIconTint="@color/black"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/free_cooling_ventilation_speed"
                    android:layout_
                    android:layout_
                    android:inputType="numberDecimal"
                    android:textSize="12sp"
                    android:maxLength="3"
                    android:digits="0123456789."/>
            </com.google.android.material.textfield.TextInputLayout>


            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/free_cooling_ventilation_speed_max_layout"
                android:layout_
                android:layout_
                android:layout_marginStart="8dp"
                android:layout_weight="1"
                app:helperText="@string/pourcent"
                android:hint="@string/max"
                app:startIconTint="@color/black"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/free_cooling_ventilation_speed_max"
                    android:layout_
                    android:layout_
                    android:inputType="numberDecimal"
                    android:textSize="12sp"
                    android:maxLength="3"
                    android:digits="0123456789."/>
            </com.google.android.material.textfield.TextInputLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/temp_set_point_comfort_mode_relative"
            android:layout_
            android:layout_
            android:layout_marginTop="16dp">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/setting_point_delta_under_cooling_setpoint_min_layout"
                android:layout_
                android:layout_
                android:layout_weight="1"
                app:helperText="@string/degrees"
                android:hint="@string/min"
                app:startIconTint="@color/black"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/setting_point_delta_under_cooling_setpoint_min"
                    android:layout_
                    android:layout_
                    android:inputType="numberDecimal"
                    android:textSize="12sp"
                    android:maxLength="3"
                    android:digits="0123456789."/>
            </com.google.android.material.textfield.TextInputLayout>


            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/setting_point_delta_under_cooling_setpoint_layout"
                android:layout_
                android:layout_
                app:startIconDrawable="@drawable/occupancy_out"
                app:helperText="@string/degrees"
                android:layout_weight="1.5"
                android:hint="@string/speed"
                android:layout_marginStart="8dp"
                app:startIconTint="@color/black"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/setting_point_delta_under_cooling_setpoint"
                    android:layout_
                    android:layout_
                    android:inputType="numberDecimal"
                    android:textSize="12sp"
                    android:maxLength="3"
                    android:digits="0123456789."/>
            </com.google.android.material.textfield.TextInputLayout>


            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/setting_point_delta_under_cooling_setpoint_max_layout"
                android:layout_
                android:layout_
                android:layout_marginStart="8dp"
                android:layout_weight="1"
                app:helperText="@string/degrees"
                android:hint="@string/max"
                app:startIconTint="@color/black"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/setting_point_delta_under_cooling_setpoint_max"
                    android:layout_
                    android:layout_
                    android:inputType="numberDecimal"
                    android:textSize="12sp"
                    android:maxLength="3"
                    android:digits="0123456789."/>
            </com.google.android.material.textfield.TextInputLayout>
        </LinearLayout>
        <View
            style="@style/dividerHorizontal"
            android:layout_marginTop="12dp"/>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/free_cooling_exemption_relative"
            android:layout_
            android:layout_>


            <ImageView
                android:id="@+id/exemption_picto"
                android:layout_
                android:layout_
                android:background="@drawable/clock"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:layout_
                android:layout_
                android:layout_marginTop="4dp"
                android:text="@string/exemption"
                android:textSize="12sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@id/exemption_picto" />

            <androidx.appcompat.widget.AppCompatSpinner
                android:id="@+id/free_cooling_exemption"
                android:layout_
                android:layout_
                android:backgroundTint="@color/black"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </LinearLayout>
</LinearLayout>

我设置为 Gone 的布局是线性布局,其 id 为“linear_freecooling_activated” 我正在使用带有 TabViews 的片段,我认为它与它有关,因为我试图在经典片段上重现此问题,它不会发生 感谢您的帮助

【问题讨论】:

【参考方案1】:

看起来问题来自用于替换我的片段的协调器布局,我不知道为什么,我用 FrameLayout 替换它并且它确实有效

【讨论】:

以上是关于android Xml 布局,可见性消失了调整我的父布局的主要内容,如果未能解决你的问题,请参考以下文章

Coordinatorlayout 中的 VIewpager 在可见性消失时占用了额外的空间

Imageview 在线性布局中不可见

如何设置滚动视图内并包含两个 TextView 的线性布局的可见性消失?

当可见性设置为Gone或Visible时,如何排列线性布局?

Android:隐形和消失的区别?

Android:将可见设置为在运行时消失会留下一个空白空间