我正在尝试在我的代码中实现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>
感谢任何建议。
答案 0 :(得分:2)
如果使用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>`
答案 1 :(得分:0)
因为模拟器是Nexus_5_API_21,但您的预览显示在Nexus_4_API_25屏幕上 只需将预览更改为相应的屏幕
即可