Android 约束布局在预览和练习时显示另一个屏幕

Android 约束布局在预览和练习时显示另一个屏幕,android,android-constraintlayout,Android,Android Constraintlayout,我使用下面的代码来显示按钮和ViewPager。我想在按钮上方显示ViewPager,但它的内容高度和宽度为wrap_ <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.and

我使用下面的代码来显示按钮和ViewPager。我想在按钮上方显示ViewPager,但它的内容高度和宽度为wrap_

<android.support.constraint.ConstraintLayout 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:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/transparent_hd_image_scrim">

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

<LinearLayout
    android:id="@+id/buttons_bottom_layout"
    android:layout_width="0dp"
    android:layout_height="@dimen/hd_preview_buttons_height"
    android:layout_marginEnd="@dimen/basic_keyline"
    android:layout_marginRight="@dimen/basic_keyline"
    android:orientation="horizontal"
    android:layout_marginBottom="16dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@id/view_pager"
    app:layout_constraintBottom_toBottomOf="parent">

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:visibility="invisible" />

    <View
        android:id="@+id/stubBottom"
        android:layout_width="@dimen/basic_keyline"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/send_button"
        style="@style/HDPreviewButtonStyle"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/colorAccent"
        android:text="@string/send"
        android:textAllCaps="true" />

</LinearLayout>

但在模拟器上练习时,我看到了这个屏幕。


我所做的不正确?

从您发布的XML代码来看,ConstraintLayout可能存在一些问题

此外,ConstraintLayout还可以帮助您进行嵌套布局,您在希望避免的注释中也提到了这一点

我不确定您是否希望视图寻呼机表现为换行,而不是匹配其约束

我使用ConstraintLayout提供的功能创建了我认为您想要的内容,以减轻以前布局带来的痛苦

<android.support.constraint.ConstraintLayout android:id="@+id/linearLayout"
    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:background="@color/transparent_hd_image_scrim">

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/barrier"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/send_button"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view_pager" />

    <Button
        android:id="@+id/send_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="8dp"
        android:background="@color/colorAccent"
        android:textAllCaps="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/view_pager"
        tools:text="Send" />

    <android.support.constraint.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="top"
        app:constraint_referenced_ids="send_button, button"
        app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>

从您发布的XML代码来看,ConstraintLayout可能存在一些问题

此外,ConstraintLayout还可以帮助您进行嵌套布局,您在希望避免的注释中也提到了这一点

我不确定您是否希望视图寻呼机表现为换行,而不是匹配其约束

我使用ConstraintLayout提供的功能创建了我认为您想要的内容,以减轻以前布局带来的痛苦

<android.support.constraint.ConstraintLayout android:id="@+id/linearLayout"
    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:background="@color/transparent_hd_image_scrim">

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/barrier"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/send_button"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view_pager" />

    <Button
        android:id="@+id/send_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="8dp"
        android:background="@color/colorAccent"
        android:textAllCaps="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/view_pager"
        tools:text="Send" />

    <android.support.constraint.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="top"
        app:constraint_referenced_ids="send_button, button"
        app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>

老兄,你把你的主容器关在哪里?即使这不重要,请尝试在另一个布局中包含viewpage-linear或smth-elseBut用于什么?我没听说我应该这么做。你能添加任意链接吗?我使用ConstraintLayout来避免嵌套布局。我看不出这样做有什么好处。老兄,你把你的主容器关在哪里?即使这不重要,请尝试在另一个布局中包含viewpage-linear或smth-elseBut用于什么?我没听说我应该这么做。你能添加任意链接吗?我使用ConstraintLayout来避免嵌套布局。我看不出这样做有什么好处。