我是新来的,正在尝试学习 Android 编程,但我遇到了问题并且无法解决这个问题,请帮助我解决这个问题。我正在使用 Android Studio 2.3,问题是当我尝试在
中使用“match_parent”值时android:layout_width="match_parent"
android:layout_height="match_parent"
它自动变成如下:-
android:layout_width="0dp"
android:layout_height="0dp"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="16dp" />
下面是我的完整代码,请帮助我。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rj.happybirthday.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="36sp"
android:textColor="@android:color/white"
android:fontFamily="sans-serif-light"
android:text="Happy Birthday Rj!"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="0dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:fontFamily="sans-serif-light"
android:textColor="@android:color/white"
android:textSize="36sp"
android:text="From, Rj!"
/>
<ImageView
android:src="@drawable/party"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="16dp" />
</android.support.constraint.ConstraintLayout>
最佳答案
ConstraintLayout
subview 无法指定 match_parent
作为其所需的尺寸。使用 match_constraint
(0dp),并将左/右或顶部/底部约束设置为“parent”
。
Widgets dimension constraints
The dimension of the widgets can be specified by setting the
android:layout_width
andandroid:layout_height
attributes in 3 different ways:
- Using a specific dimension (either a literal value such as
123dp
or aDimension
reference)- Using
WRAP_CONTENT
, which will ask the widget to compute its own size- Using
0dp
, which is the equivalent of "MATCH_CONSTRAINT
"Fig. 7 - Dimension Constraints
The first two works in a similar fashion as other layouts. The last one will resize the widget in such a way as matching the constraints that are set (see Fig. 7, (a) is wrap_content, (b) is 0dp). If margins are set, they will be taken in account in the computation (Fig. 7, (c) with 0dp).
Important:
MATCH_PARENT
is not supported for widgets contained in aConstraintLayout
, though similar behavior can be defined by usingMATCH_CONSTRAINT
with the corresponding left/right or top/bottom constraints being set to"parent"
.
来源:https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html
关于android - Match_parent 在 Android Studio 2.3 的 ImageView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43028605/