android - Match_parent 在 Android Studio 2.3 的 ImageView 中不起作用

标签 android android-layout

我是新来的,正在尝试学习 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 and android:layout_height attributes in 3 different ways:

  • Using a specific dimension (either a literal value such as 123dp or a Dimension reference)
  • Using WRAP_CONTENT, which will ask the widget to compute its own size
  • Using 0dp, which is the equivalent of "MATCH_CONSTRAINT"

Dimension Contraints

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 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".

来源: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/

相关文章:

java - this.getWritableDatabase() 导致我的 Android 应用程序停止

安卓兼容性 - micromax funbook

Android 如何设置百分比填充/边距,以便 EditText 两边都有 10% 的边距?

android - 安装错误 : INSTALL_FAILED_DEXOPT

Android 布局 - layout-normal vs layout

android - 如何在 Android View 下方设置高程颜色?

android - RadioButtons 下方的文本

android - 网格布局不显示项目

java - 使用 Java 访问 Android 中的原始分区

android - Mediarecorder 视频 RTP/RTSP 流!