约束布局是在安卓9之后提出的一种布局方式,相比较之前的布局方式,优点是:
示例:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".YueshuActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="150dp"
android:textSize="100dp"
android:textColor="#3CB371"
android:gravity="right"
android:text="0"
android:id="@+id/tv1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<Button
app:layout_constraintHorizontal_weight="1"
app:layout_constraintRight_toLeftOf="@id/btn_back"
android:textColor="#ff0000"
app:layout_constraintTop_toBottomOf="@id/tv1"
app:layout_constraintLeft_toLeftOf="parent"
android:id="@+id/btn_c"
android:layout_width="0dp"
android:layout_height="100dp"
android:text="C"
android:textSize="35dp"
/>
<Button
android:layout_width="0dp"
android:layout_height="100dp"
app:layout_constraintHorizontal_weight="1"
android:text="Back"
android:textSize="20dp"
app:layout_constraintTop_toBottomOf="@id/tv1"
app:layout_constraintLeft_toRightOf="@id/btn_c"
app:layout_constraintRight_toLeftOf="@id/btn_per"
android:layout_below="@id/tv1"
android:id="@+id/btn_back"
/>
<Button
android:layout_width="0dp"
android:layout_height="100dp"
app:layout_constraintHorizontal_weight="1"
android:text="%"
android:textSize="30dp"
app:layout_constraintTop_toBottomOf="@id/tv1"
app:layout_constraintLeft_toRightOf="@id/btn_back"
app:layout_constraintRight_toLeftOf="@id/btn_muv"
android:id="@+id/btn_per"
/>
<Button
android:layout_width="0dp"
android:layout_height="100dp"
android:text="*"
android:textSize="30dp"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintTop_toBottomOf="@id/tv1"
app:layout_constraintLeft_toRightOf="@id/btn_per"
app:layout_constraintRight_toRightOf="parent"
android:id="@+id/btn_muv"
/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="9"
android:textSize="35dp"
app:layout_constraintTop_toBottomOf="@id/btn_c"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/btn_8"
android:id="@+id/btn_9"
/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="8"
android:textSize="35dp"
app:layout_constraintTop_toBottomOf="@id/btn_c"
app:layout_constraintLeft_toRightOf="@id/btn_9"
app:layout_constraintRight_toLeftOf="@id/btn_7"
android:id="@+id/btn_8"
/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="7"
android:textSize="35dp"
app:layout_constraintTop_toBottomOf="@id/btn_c"
app:layout_constraintLeft_toRightOf="@id/btn_8"
app:layout_constraintRight_toLeftOf="@id/btn_dev"
android:id="@+id/btn_7"
/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="/"
android:textSize="35dp"
app:layout_constraintTop_toBottomOf="@id/btn_c"
app:layout_constraintLeft_toRightOf="@id/btn_7"
app:layout_constraintRight_toRightOf="parent"
android:id="@+id/btn_dev"
/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="6"
android:textSize="35dp"
app:layout_constraintTop_toBottomOf="@id/btn_9"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/btn_5"
android:id="@+id/btn_6"
/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="5"
android:textSize="35dp"
app:layout_constraintTop_toBottomOf="@id/btn_9"
app:layout_constraintLeft_toRightOf="@id/btn_6"
app:layout_constraintRight_toLeftOf="@id/btn_4"
android:id="@+id/btn_5"
/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="4"
android:textSize="35dp"
app:layout_constraintTop_toBottomOf="@id/btn_9"
app:layout_constraintLeft_toRightOf="@id/btn_5"
app:layout_constraintRight_toLeftOf="@id/btn_muv"
android:id="@+id/btn_4"
/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="-"
android:textSize="35dp"
app:layout_constraintTop_toBottomOf="@id/btn_9"
app:layout_constraintLeft_toRightOf="@id/btn_4"
app:layout_constraintRight_toRightOf="parent"
android:id="@+id/btn_sub"
/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="3"
android:textSize="35dp"
app:layout_constraintTop_toBottomOf="@id/btn_6"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/btn_2"
android:id="@+id/btn_3"
/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="2"
android:textSize="35dp"
app:layout_constraintTop_toBottomOf="@id/btn_6"
app:layout_constraintLeft_toRightOf="@id/btn_3"
app:layout_constraintRight_toLeftOf="@id/btn_1"
android:id="@+id/btn_2"
/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="1"
android:textSize="35dp"
app:layout_constraintTop_toBottomOf="@id/btn_6"
app:layout_constraintLeft_toRightOf="@id/btn_2"
app:layout_constraintRight_toLeftOf="@id/btn_add"
android:id="@+id/btn_1"
/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="+"
android:textSize="35dp"
app:layout_constraintTop_toBottomOf="@id/btn_6"
app:layout_constraintLeft_toRightOf="@id/btn_1"
app:layout_constraintRight_toRightOf="parent"
android:id="@+id/btn_add"
/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="+/-"
android:textSize="35dp"
app:layout_constraintTop_toBottomOf="@id/btn_3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/btn_0"
android:id="@+id/btn_zf"
/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="0"
android:textSize="35dp"
app:layout_constraintTop_toBottomOf="@id/btn_3"
app:layout_constraintLeft_toRightOf="@id/btn_zf"
app:layout_constraintRight_toLeftOf="@id/btn_poi"
android:id="@+id/btn_0"
/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="."
android:textSize="35dp"
app:layout_constraintTop_toBottomOf="@id/btn_3"
app:layout_constraintLeft_toRightOf="@id/btn_0"
app:layout_constraintRight_toLeftOf="@id/btn_result"
android:id="@+id/btn_poi"
/>
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="="
android:textSize="35dp"
app:layout_constraintTop_toBottomOf="@id/btn_3"
app:layout_constraintLeft_toRightOf="@id/btn_poi"
app:layout_constraintRight_toRightOf="parent"
android:id="@+id/btn_result"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
结果: