converting match_parent to “0dp”

我只是一个虾纸丫 提交于 2019-12-12 04:29:57

问题


When I use android:layout_height="match_parent" or android:layout_width="match_parent" as height/width of children in Constraint Layout and build the Gradle file it automatically changes to android:layout_height="0dp" or android:layout_widtht="0dp" respectively.

Technically there is a huge difference between 0dp and match_parent.

Most of the time it doesn't effect my layout but sometimes this thing completely destroy my Constraint Layout.

I don't know why this happens.
Please guide me.


回答1:


match_parent isn't supported in ConstraintLayout. If you want the same behavior, use 0dp and set constraints on each side to the parent.




回答2:


You should not use match_parent for any view in a ConstraintLayout. Instead use "match constraints" (0dp)

match_parent means filling whats left where as 0dp means match constraints. In ConstraintLayout, match_parent is not supported.

match_parent relies on it's parent for it's position (x,y & width/height) but 0dp can be defined by a constraint/position




回答3:


The way worked with me for putting a Relative item, under another element, with width matching parent is:

<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/xxx"
    app:layout_constraintEnd_toEndOf="@+id/xxx"
    >


来源:https://stackoverflow.com/questions/40126678/converting-match-parent-to-0dp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!