Android studio keeps replacing match_parent with fixed dp value

前端 未结 3 724
情歌与酒
情歌与酒 2020-12-25 13:39

Android studio (v 2.3.1) keeps replacing the match_parent of a RelativeLayout with a fixed dp vanue. For example, when I type match_parent as the width, it repl

3条回答
  •  误落风尘
    2020-12-25 14:30

    In short:

    You cannot use match_parent as the dimension for children of ConstraintLayout. You should use 0dp which means "match_constraint" and constraint the sides to the parent's sides:

    android:layout_width="0dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    

    or

    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    

    With a bit of background (or - from where I believe the confusion stems):

    Although I can't seem to find a proof, and my colleague claims I'm wrong - I'm certain the android documentation for constraintLayout used to direct us, the developers, to use "either 0dp or match_parent" as values for a layout_width or layout_height to indicate that the corresponding view's dimension should be determined by the constraintLayout using the specified constraints (rather than using a specified fixed value or by determining its content's dimension). I'm also pretty sure I used this value (match_parent) in this manner and it worked before switching to AndroidStudio 2.3.1.

    Whether I'm right or delusional, the fact is that currently the documentation states:

    Important: MATCH_PARENT is not supported for widgets contained in a ConstraintLayout, though similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".

    Additionally, the 0dp value is regarded the same as I remember it used to:

    The dimension of the widgets can be specified by setting the android:layout_width and android:layout_height attributes in 3 different ways:

    (...)

    • Using 0dp, which is the equivalent of "MATCH_CONSTRAINT"

    The editor, using "infer constraints" as in the answer of Sirnivas actually uses the 0dp approach.

    NOTE: "match_constraint" doesn't seem to be a value that can be used. 0dp is what seems to be the actual value for the dimension to match the constraints.

提交回复
热议问题