ConstraintLayout Space 实例代码:创建简单布局
以下是一个使用ConstraintLayout创建一个简单的布局的示例代码:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint='Enter your name'
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
app:layout_constraintEnd_toEndOf="@id/editText"
app:layout_constraintStart_toStartOf="@id/editText"
app:layout_constraintTop_toBottomOf="@id/editText"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
在这个示例中,ConstraintLayout包含一个EditText和一个Button。EditText的顶部与父布局的顶部对齐,左右边缘与父布局的边缘对齐。Button的顶部与EditText的底部对齐,左右边缘与EditText的边缘对齐,底部与父布局的底部对齐。两个控件之间有16dp的上边距。
通过使用ConstraintLayout的约束属性,我们可以轻松地定义控件之间的关系和位置。这样,我们就可以创建灵活的布局,适应不同的屏幕大小和设备方向。
原文地址: https://www.cveoy.top/t/topic/qx7Y 著作权归作者所有。请勿转载和采集!