java - 约束布局中的 dp 和 wrap_content
问题描述
使用约束布局时 0 dp 和 wrap_content 有什么区别?是效率问题吗?我已经尝试过同时使用它们,但我只是不明白有什么区别。
解决方案
实际上,0dp
它完全占据width/height
了它的受限区域,而wrap_content
占据了它所需要的任何东西来保持它的内容。
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<Button
android:id="@+id/button_wrap"
android:text="Wrap Button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/button_0dp"
android:text="0dp Button"
app:layout_constraintTop_toBottomOf="@+id/button_wrap"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
输出:
在上面的例子中
Button
with只占用保存Wrap Buttonwrap_content
所需的空间,而Button
with0dp
占用其受限区域中的所有空间
推荐阅读
- android - 在 APK 的 Manifest 文件和 Application 类中,先调用哪个?
- git - 如何重新启动rebase中的最后一个冲突情况?
- python - 使用管道将命令发送到 python 解释器
- python - 将 Keras 模型权重和架构转换为 TensorFlow Lite 模型
- angular - 当路线角度发生变化时如何运行函数?
- unity3d - 变换在 Unity3D 中有什么作用?在幕后
- c# - 同时打开新标签和加载当前标签
- akka - 配置文件 Akka 集群
- .net-core - .NET Core - 如何在团队中管理用户机密
- java - 创建从数组构建的多个 JTable 不太奏效