简体   繁体   中英

dp and wrap_content in Constraint Layout

What is the difference between 0 dp and wrap_content when using Constraint Layout? 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.

<?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
  • Button with 0dp takes all the space in it's constrained areas

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.

With the 0dp the constraints will be used.

Wrap content mainly take exact space which it need and 0dp width means that in width side it less than 0dp size. Better understand just change width size i mean 0 to upper value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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