ConstainLayout代码布局
时间: 2023-11-27 21:53:21 浏览: 38
ConstraintLayout 是 Android 中比较强大的布局管理器之一,它可以用来实现复杂的布局效果。下面是一个简单的 ConstraintLayout 代码示例:
```
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="24sp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintTop_toBottomOf="@+id/text_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</android.support.constraint.ConstraintLayout>
```
这个布局包含一个 TextView 和一个 Button,Button 在 TextView 的下方并且居中对齐。其中 `app:layout_constraintTop_toBottomOf="@+id/text_view"` 表示 Button 的顶部约束到 TextView 的底部,`app:layout_constraintStart_toStartOf="parent"` 表示 Button 的左侧约束到父容器的左侧,`app:layout_constraintEnd_toEndOf="parent"` 表示 Button 的右侧约束到父容器的右侧。这些约束可以用来控制控件的位置和大小。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)