约束布局是在安卓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

本文介绍安卓9后推出的约束布局,通过示例详细展示了如何使用约束布局进行UI设计,包括视图间的相对定位及权重分配,适用于安卓开发者学习与实践。
最低0.47元/天 解锁文章
1073

被折叠的 条评论
为什么被折叠?



