简体   繁体   English

约束布局中的 dp 和 wrap_content

[英]dp and wrap_content in Constraint Layout

What is the difference between 0 dp and wrap_content when using Constraint Layout?使用约束布局时 0 dp 和 wrap_content 有什么区别? Is it an efficiency problem?是效率问题吗? I have already tried using them both but I just don't understand which is the difference.我已经尝试过使用它们,但我只是不明白有什么区别。

Actually 0dp takes full width/height in it constrained areas whereas wrap_content takes whatever it's required to hold it's 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>

Output:输出:

在此处输入图片说明

In the above example在上面的例子中

  • Button with wrap_content takes only the space that required to hold Wrap Button whereas带有wrap_content Button仅占用容纳Wrap Button所需的空间,而
  • Button with 0dp takes all the space in it's constrained areas具有0dp Button占用其受限区域中的所有空间

The main difference is that the wrap_content will take only into account its size, and it won't use the constraints attached to it.主要区别在于wrap_content将仅考虑其大小,而不会使用附加到它的约束。

With the 0dp the constraints will be used.对于0dp ,将使用约束。

Wrap content mainly take exact space which it need and 0dp width means that in width side it less than 0dp size.包装内容主要占用它需要的确切空间,0dp 宽度意味着在宽度侧它小于 0dp 大小。 Better understand just change width size i mean 0 to upper value更好地理解只是改变宽度大小我的意思是 0 到上限值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM