Android studio keeps replacing match_parent with fixed dp value

前端 未结 3 709
情歌与酒
情歌与酒 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:21
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
        <Button
            android:id="@+id/buttonLogin"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:background="@color/colorPrimary"
            android:text="Login"
            android:textColor="@android:color/white"
            android:textSize="18sp"
            tools:layout_constraintRight_creator="1"
            tools:layout_constraintBottom_creator="1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="@+id/editTextCode"
            tools:layout_constraintLeft_creator="1"
            android:layout_marginBottom="169dp"
            app:layout_constraintLeft_toLeftOf="@+id/editTextCode" />
    
        <EditText
            android:id="@+id/editTextCode"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_above="@+id/buttonLogin"
            android:layout_alignParentStart="true"
            android:ems="10"
            android:hint="Secret code here"
            android:inputType="numberPassword"
            tools:layout_constraintRight_creator="1"
            tools:layout_constraintBottom_creator="1"
            app:layout_constraintBottom_toTopOf="@+id/buttonLogin"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            app:layout_constraintRight_toRightOf="parent"
            tools:layout_constraintLeft_creator="1"
            android:layout_marginBottom="8dp"
            app:layout_constraintLeft_toLeftOf="parent" />
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:fontFamily="monospace"
            android:text="example"
            android:textAlignment="center"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="36sp"
            android:textStyle="italic"
            tools:layout_constraintTop_creator="1"
            tools:layout_constraintRight_creator="1"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_marginTop="16dp"
            tools:layout_constraintLeft_creator="1"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        </android.support.constraint.ConstraintLayout>
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-25 14:42

    Do it like as shown in picture

    0 讨论(0)
提交回复
热议问题