屏幕上的嵌套 ConstraintLayout 可见性 ON/OFF
nested ConstraintLayout visibility on Screen ON/OFF
我的布局具有以下层次结构
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 class 中,我隐藏了 SwipeRefreshLayout 及其子 RecyclerView 并显示 empty_layout。这工作正常,但是如果我关闭和打开设备屏幕,empty_layout 就会消失。
仅供参考:我已将嵌套 ConstrainLayout 的可见性设置为始终可见,但如果我转动 off/on 屏幕,它就会消失。
仅供参考:如果我曾经显示过 ProgressView,ProgressView 在屏幕上仍然可见 on/off 我假设它的原因是 ProgressView 没有包含在嵌套的 ConstraintLayout 下!?。
知道为什么嵌套的 ConstrainLayout 只在屏幕上消失 on/off,而如果我旋转屏幕它仍然存在吗?
嗯,奇怪,我将 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
时消失的问题,如果有人能启发我,我将不胜感激.
我的布局具有以下层次结构
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 class 中,我隐藏了 SwipeRefreshLayout 及其子 RecyclerView 并显示 empty_layout。这工作正常,但是如果我关闭和打开设备屏幕,empty_layout 就会消失。
仅供参考:我已将嵌套 ConstrainLayout 的可见性设置为始终可见,但如果我转动 off/on 屏幕,它就会消失。
仅供参考:如果我曾经显示过 ProgressView,ProgressView 在屏幕上仍然可见 on/off 我假设它的原因是 ProgressView 没有包含在嵌套的 ConstraintLayout 下!?。
知道为什么嵌套的 ConstrainLayout 只在屏幕上消失 on/off,而如果我旋转屏幕它仍然存在吗?
嗯,奇怪,我将 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
时消失的问题,如果有人能启发我,我将不胜感激.