Android studio(v 2.3.1)一直用固定的dp vanue替换RelativeLayout的match_parent
。例如,当我输入match_parent作为宽度时,它将其替换为368dp。当我测试应用程序时,我发现RelativeLayout确实是错误的。
有谁知道如何解决这个问题?
<?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"
tools:context="be.mawey.lamachat.LoginActivity">
<RelativeLayout
android:layout_width="match_parent" <!-- THIS IS BEING REPLACED WITH 368dp -->
android:layout_height="match_parent" <!-- THIS IS BEING REPLACED WITH 495dp -->
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
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_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="292dp" />
<EditText
android:id="@+id/editTextCode"
android:layout_width="match_parent"
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_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="234dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
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_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="16dp" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:24)
答案 1 :(得分:19)
简而言之:
您不能将match_parent
用作ConstraintLayout
子项的维度。您应该使用0dp
,这意味着“match_constraint”并将边界约束到父母的边:
android:layout_width="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
或
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
有一点背景(或 - 从我相信混乱的地方):
虽然我似乎无法找到证据,但我的同事声称我错了 - 我确信the android documentation for constraintLayout曾经指示我们开发人员使用“0dp或match_parent”作为值对于layout_width
或layout_height
,表示相应视图的维度应由constraintLayout
使用指定的约束(而不是使用指定的固定值或通过确定其内容的维度)来确定。我也非常确定我以这种方式使用了这个值(match_parent
),并且在切换到AndroidStudio 2.3.1之前它已经工作了。
无论我是对还是妄想,事实是目前the documentation州:
重要说明:ConstraintLayout中包含的小部件不支持MATCH_PARENT,但可以使用MATCH_CONSTRAINT定义相似的行为,并将相应的左/右或上/下约束设置为“父级”。
此外,0dp
值与我以前记得的相同:
可以通过3种不同的方式设置android:layout_width和android:layout_height属性来指定小部件的维度:
(...)
- 使用0dp,相当于“MATCH_CONSTRAINT”
编辑使用“推断约束”,如Sirnivas的答案,实际上使用0dp
方法。
注意:“match_constraint”似乎不是可以使用的值。 0dp
似乎是维度匹配约束的实际值。
答案 2 :(得分:0)
<?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>