屏幕ON / OFF上的嵌套ConstraintLayout可见性

时间:2016-05-29 12:47:35

标签: android android-layout android-constraintlayout

我有一个具有以下层次结构的布局

list.xml

<android.support.constraint.ConstraintLayout
    android:id="@+id/layout_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/refresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="@+id/layout_content"
        app:layout_constraintStart_toStartOf="@+id/layout_content"
        app:layout_constraintTop_toTopOf="@+id/layout_content"
        tools:layout_constraintEnd_creator="0"
        tools:layout_constraintStart_creator="0"
        tools:layout_editor_absoluteX="0dp">

        <com.fastaccess.cheaphlight.ui.widgets.recyclerview.DynamicRecyclerView
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/spacing_large"
            app:layoutManager="@string/staggered_layout_manager"
            app:layout_constraintLeft_toLeftOf="@+id/refresh"
            app:spanCount="2"
            tools:listitem="@layout/county_row_item"/>

    </android.support.v4.widget.SwipeRefreshLayout>

    <include
        layout="@layout/empty_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="@+id/layout_content"
        app:layout_constraintStart_toStartOf="@+id/layout_content"
        app:layout_constraintTop_toTopOf="@+id/layout_content"
        tools:layout_constraintBottom_creator="0"
        tools:layout_constraintEnd_creator="0"
        tools:layout_constraintStart_creator="0"
        tools:layout_constraintTop_creator="0"/>

    <include
        layout="@layout/progress_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:paddingBottom="?attr/actionBarSize"
        app:layout_constraintBottom_toBottomOf="@+id/layout_content"
        app:layout_constraintEnd_toEndOf="@+id/layout_content"
        app:layout_constraintStart_toStartOf="@+id/layout_content"
        app:layout_constraintTop_toTopOf="@+id/layout_content"
        tools:layout_constraintBottom_creator="0"
        tools:layout_constraintEnd_creator="0"
        tools:layout_constraintStart_creator="0"
        tools:layout_constraintTop_creator="0"
        tools:layout_editor_absoluteX="205dp"
        tools:layout_editor_absoluteY="382dp"/>
</android.support.constraint.ConstraintLayout>

empty_layout.xml

    <android.support.constraint.ConstraintLayout
    android:id="@+id/empty_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:visibility="gone"
    tools:visibility="visible">

    <ImageView
        android:id="@+id/image_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_sad_face"
        app:layout_constraintBottom_toBottomOf="@+id/empty_layout"
        app:layout_constraintEnd_toEndOf="@+id/empty_layout"
        app:layout_constraintStart_toStartOf="@+id/empty_layout"
        app:layout_constraintTop_toTopOf="@+id/empty_layout"
        app:layout_constraintVertical_bias="0.19"
        tools:ignore="ContentDescription"
        tools:layout_constraintBottom_creator="0"
        tools:layout_constraintEnd_creator="0"
        tools:layout_constraintStart_creator="0"
        tools:layout_constraintTop_creator="0"
        tools:layout_editor_absoluteX="145dp"
        tools:layout_editor_absoluteY="321dp"/>

    <com.fastaccess.cheaphlight.ui.widgets.FontTextView
        android:id="@+id/text_message"
        style="@style/TextAppearance.AppCompat.Medium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/image_message"
        android:layout_centerHorizontal="true"
        android:layout_margin="@dimen/spacing_normal"
        android:gravity="center_horizontal"
        android:padding="@dimen/spacing_s_large"
        android:textColor="@color/divider"
        app:layout_constraintEnd_toEndOf="@+id/empty_layout"
        app:layout_constraintStart_toStartOf="@+id/empty_layout"
        app:layout_constraintTop_toBottomOf="@+id/image_message"
        tools:layout_constraintEnd_creator="0"
        tools:layout_constraintStart_creator="0"
        tools:layout_constraintTop_creator="0"
        tools:layout_editor_absoluteX="205dp"
        tools:layout_editor_absoluteY="441dp"
        tools:text="Error Message"/>

    <com.fastaccess.cheaphlight.ui.widgets.FontButton
        android:id="@+id/reload_button"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text_message"
        android:layout_centerHorizontal="true"
        android:layout_margin="@dimen/spacing_normal"
        android:padding="@dimen/spacing_s_large"
        android:text="@string/reload"
        android:textColor="@color/accent"
        app:layout_constraintEnd_toEndOf="@+id/empty_layout"
        app:layout_constraintStart_toStartOf="@+id/empty_layout"
        app:layout_constraintTop_toBottomOf="@+id/text_message"
        tools:layout_constraintEnd_creator="0"
        tools:layout_constraintStart_creator="0"
        tools:layout_constraintTop_creator="0"
        tools:layout_editor_absoluteX="205dp"
        tools:layout_editor_absoluteY="441dp"/>
</android.support.constraint.ConstraintLayout>

progress_layout.xml

<ProgressBar
android:id="@+id/progress_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="?attr/actionBarSize"
android:visibility="gone"
tools:visibility="visible"/>

在我的Fragment类中,我隐藏了SwipeRefreshLayout及其子RecyclerView并显示了empty_layout。这工作正常,但如果我把设备屏幕关闭&amp;在empty_layout上消失。

仅供参考:我将嵌套的ConstrainLayout的可见性设置为始终可见,但如果我关闭/在屏幕上它会消失。

仅供参考:如果我曾经展示过ProgressView,那么ProgressView在屏幕开启/关闭时仍然可见,我假设它是由于ProgressView没有包装在嵌套的ConstraintLayout下!

关于为什么嵌套的ConstrainLayout仅在屏幕上开/关消失的任何想法,而如果我旋转屏幕它仍然存在?

1 个答案:

答案 0 :(得分:0)

嗯,很奇怪,我把empty_layout.xml包装在FrameLayout下,一切正常。 Layout变为如下:

<FrameLayout
    android:id="@+id/empty_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:visibility="gone"
    tools:visibility="visible">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/holder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

        <ImageView
            android:id="@+id/image_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_sad_face"
            app:layout_constraintBottom_toBottomOf="@+id/holder"
            app:layout_constraintEnd_toEndOf="@+id/holder"
            app:layout_constraintStart_toStartOf="@+id/holder"
            app:layout_constraintTop_toTopOf="@+id/holder"
            app:layout_constraintVertical_bias="0.19"
            tools:ignore="ContentDescription"
            tools:layout_constraintBottom_creator="0"
            tools:layout_constraintEnd_creator="0"
            tools:layout_constraintStart_creator="0"
            tools:layout_constraintTop_creator="0"
            tools:layout_editor_absoluteX="145dp"
            tools:layout_editor_absoluteY="321dp"/>

        <com.fastaccess.cheaphlight.ui.widgets.FontTextView
            android:id="@+id/text_message"
            style="@style/TextAppearance.AppCompat.Medium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/image_message"
            android:layout_centerHorizontal="true"
            android:layout_margin="@dimen/spacing_normal"
            android:gravity="center_horizontal"
            android:padding="@dimen/spacing_s_large"
            android:textColor="@color/divider"
            app:layout_constraintEnd_toEndOf="@+id/holder"
            app:layout_constraintStart_toStartOf="@+id/holder"
            app:layout_constraintTop_toBottomOf="@+id/image_message"
            tools:layout_constraintEnd_creator="0"
            tools:layout_constraintStart_creator="0"
            tools:layout_constraintTop_creator="0"
            tools:layout_editor_absoluteX="205dp"
            tools:layout_editor_absoluteY="441dp"
            tools:text="Error Message"/>

        <com.fastaccess.cheaphlight.ui.widgets.FontButton
            android:id="@+id/reload_button"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text_message"
            android:layout_centerHorizontal="true"
            android:layout_margin="@dimen/spacing_normal"
            android:padding="@dimen/spacing_s_large"
            android:text="@string/reload"
            android:textColor="@color/accent"
            app:layout_constraintEnd_toEndOf="@+id/holder"
            app:layout_constraintStart_toStartOf="@+id/holder"
            app:layout_constraintTop_toBottomOf="@+id/text_message"
            tools:layout_constraintEnd_creator="0"
            tools:layout_constraintStart_creator="0"
            tools:layout_constraintTop_creator="0"
            tools:layout_editor_absoluteX="205dp"
            tools:layout_editor_absoluteY="441dp"/>
    </android.support.constraint.ConstraintLayout>
</FrameLayout>

然而,这并没有回答我的问题,为什么当屏幕转为Nested ConstraintLayout时,OFF会消失。 ON回来,如果有人能够启发我,我会感激不尽。