Android Constraintlayout in linearLayout emulator和IDE之间的预览是否不同?

Android Constraintlayout in linearLayout emulator和IDE之间的预览是否不同?,android,android-layout,Android,Android Layout,我试图在我的代码中实现一个ContstraintLayout,但问题是为什么在模拟器和IDE上运行它时会有不同? 对于信息,我将ConstraintLayout放在LinearLayout垂直中。 下面是我的XML代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to

我试图在我的代码中实现一个
ContstraintLayout
,但问题是为什么在模拟器和IDE上运行它时会有不同? 对于信息,我将
ConstraintLayout
放在
LinearLayout
垂直中。 下面是我的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main"
    tools:context="navotera.syasep.MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="170dp"
            >

            <RelativeLayout
                android:id="@+id/topBorder"
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:background="@android:color/holo_red_light">

            </RelativeLayout>

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:scaleType="fitCenter"
                android:foregroundGravity="right"
                android:src="@mipmap/profile"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintHorizontal_bias="0.99"
                android:id="@+id/imageView3"
                app:layout_constraintVertical_bias="0.25"
                android:paddingLeft="270dp" />

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:paddingLeft="18dp"
                android:paddingTop="10dp"
                android:layout_height="match_parent"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true">

                <TextView
                    android:textSize="@dimen/text_big"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/nama"
                    android:text="Hendra" />

                <TextView
                    android:textSize="@dimen/text_big"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/nim"
                    android:text="nim" />

                <android.support.constraint.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:foregroundGravity="right">

                    <TextView
                        android:textSize="@dimen/text_bigger"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:id="@+id/semester"
                        android:text="7"
                        android:layout_weight="1"
                        android:layout_marginEnd="184dp"
                        app:layout_constraintRight_toRightOf="parent"
                        android:layout_marginRight="184dp"
                        app:layout_constraintTop_toBottomOf="@+id/tsemester" />

                    <TextView
                        android:text="IPK"
                        android:textSize="@dimen/text_small"
                        android:layout_width="88dp"
                        android:layout_height="wrap_content"
                        android:id="@+id/tipk"
                        android:textColor="@color/colorAccent"
                        android:layout_weight="1"
                        tools:layout_editor_absoluteY="28dp"
                        app:layout_constraintRight_toLeftOf="@+id/tsemester"
                        android:layout_marginEnd="48dp"
                        android:layout_marginRight="48dp"
                        app:layout_constraintLeft_toLeftOf="@+id/ipk" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/ipk"
                        android:text="3.99"
                        android:textSize="@dimen/text_bigger"
                        android:layout_weight="1"
                        tools:layout_editor_absoluteY="49dp"
                        app:layout_constraintLeft_toLeftOf="parent"
                        android:layout_marginStart="16dp"
                        android:layout_marginLeft="16dp"
                        app:layout_constraintRight_toLeftOf="@+id/semester"
                        android:layout_marginEnd="8dp"
                        android:layout_marginRight="8dp"
                        app:layout_constraintHorizontal_bias="0.14" />

                    <TextView
                        android:text="Semester"
                        android:textSize="@dimen/text_small"
                        android:layout_width="94dp"
                        android:layout_height="wrap_content"
                        android:id="@+id/tsemester"
                        android:textColor="@color/colorAccent"
                        android:layout_weight="1"
                        tools:layout_editor_absoluteY="28dp"
                        tools:layout_editor_absoluteX="156dp" />
                </android.support.constraint.ConstraintLayout>
            </LinearLayout>


        </RelativeLayout>


        <android.support.constraint.ConstraintLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


        </android.support.constraint.ConstraintLayout>

        <TextView
            android:text="Jadwal / Kehadiran"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:paddingLeft="13dp"
            android:id="@+id/tkehadiran"
            android:textSize="@dimen/text_medium" />

    </LinearLayout>

</RelativeLayout>


因为模拟器是Nexus_5_API_21,但您的预览显示在Nexus_4_API_25屏幕上
只需将预览更改为适当的屏幕

,因为仿真器是Nexus_5_API_21,但预览显示在Nexus_4_API_25屏幕上
如果使用ConstraintLayout,只需将预览更改为适当的屏幕,然后将所有视图作为ConstraintLayout的直接子视图,如下所示

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
   android:layout_height="match_parent"
android:orientation="vertical">

<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button" android:layout_marginEnd="16dp"
    app:layout_constraintRight_toRightOf="parent" android:layout_marginStart="16dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="16dp"/>
<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button2" app:layout_constraintRight_toLeftOf="@+id/button"
    app:layout_constraintBottom_toTopOf="@+id/button" android:layout_marginBottom="16dp"/>
<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button3"
    app:layout_constraintLeft_toRightOf="@+id/button"
    app:layout_constraintBottom_toTopOf="@+id/button" android:layout_marginBottom="16dp"/>
<RadioButton
    android:text="RadioButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" tools:layout_editor_absoluteY="376dp"
    android:id="@+id/radioButton"
    app:layout_constraintRight_toLeftOf="@+id/button"/>
<QuickContactBadge
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" app:srcCompat="@mipmap/star_empty"
    android:id="@+id/quickContactBadge" android:layout_marginEnd="16dp"
    app:layout_constraintRight_toRightOf="parent" android:layout_marginStart="16dp"
    app:layout_constraintLeft_toLeftOf="parent" android:layout_marginTop="16dp"
    app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>`

`

如果使用ConstraintLayout,则将所有视图作为ConstraintLayout的直接子视图,如下所示

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
   android:layout_height="match_parent"
android:orientation="vertical">

<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button" android:layout_marginEnd="16dp"
    app:layout_constraintRight_toRightOf="parent" android:layout_marginStart="16dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="16dp"/>
<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button2" app:layout_constraintRight_toLeftOf="@+id/button"
    app:layout_constraintBottom_toTopOf="@+id/button" android:layout_marginBottom="16dp"/>
<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button3"
    app:layout_constraintLeft_toRightOf="@+id/button"
    app:layout_constraintBottom_toTopOf="@+id/button" android:layout_marginBottom="16dp"/>
<RadioButton
    android:text="RadioButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" tools:layout_editor_absoluteY="376dp"
    android:id="@+id/radioButton"
    app:layout_constraintRight_toLeftOf="@+id/button"/>
<QuickContactBadge
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" app:srcCompat="@mipmap/star_empty"
    android:id="@+id/quickContactBadge" android:layout_marginEnd="16dp"
    app:layout_constraintRight_toRightOf="parent" android:layout_marginStart="16dp"
    app:layout_constraintLeft_toLeftOf="parent" android:layout_marginTop="16dp"
    app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>`

`

为什么会发生这种情况,这是否意味着当我使用contstraint\u布局时,它会在不同的设备上产生不同的视图?这是否意味着生产中使用的约束_布局不稳定?谷歌声明说这种布局从API第9级开始就是支持的,这又如何呢?为什么会这样,这是否意味着当我使用contstraint_布局时,它会在不同的设备上产生不同的视图?这是否意味着生产中使用的约束_布局不稳定?谷歌声明说这个布局从API第9级开始就是受支持的,我会按照你的建议将约束布局作为根布局。你能解释一下这个吗呵呵。。。我也不熟悉constraintlayout,只检查了一两次,但每次我都先使用relativelayout和linearlayout将布局转换为constraintlayout,然后将文本视图放在linearlayout后面。你知道的?文本视图变得不可见??真奇怪我想分享的是,当constraintlayout中的任何组件小部件未连接时,它会重叠或不按预期显示。在这种情况下,我的android studio(2.3)显示一条错误消息,指出未设置约束,并且您的视图将在runtimeim期间向左移动,以按照您的建议将约束布局设置为根布局。你能解释一下这个吗呵呵。。。我也不熟悉constraintlayout,只检查了一两次,但每次我都先使用relativelayout和linearlayout将布局转换为constraintlayout,然后将文本视图放在linearlayout后面。你知道的?文本视图变得不可见??真奇怪我想分享的是,当constraintlayout中的任何组件小部件未连接时,它会重叠或不按预期显示。在这种情况下,我的android studio(2.3)显示一条错误消息,说明未设置约束,并且在运行时视图将向左移动您的LinearLayout未设置方向属性您的LinearLayout未设置方向属性