Android约束布局在全屏下不能拉伸

4

我有一个布局问题,无法在高分辨率(如1920x1080)下拉伸。 作为根布局,我使用以下样式的约束布局:

<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"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context="njc.nhutrinhgold.MainActivity">

<ImageView
    android:id="@+id/imgNen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:visibility="visible"
    app:layout_constraintDimensionRatio="16:9"
    app:srcCompat="@drawable/xxhdpibg"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp"
    tools:layout_width="match_parent" />

在 Manifest.xml 文件中

<supports-screens
    android:anyDensity="true"
    android:compatibleWidthLimitDp="960"
    android:largeScreens="true"
    android:largestWidthLimitDp="960"
    android:normalScreens="false"
    android:requiresSmallestWidthDp="960"
    android:resizeable="false"
    android:smallScreens="false"
    android:xlargeScreens="true" />

我想将布局延伸到任何屏幕分辨率,但在高分辨率的模拟器上,它看起来像这样:

enter image description here

实际上我还没弄清楚问题出在哪里。

为什么你给ConstraintLayout的宽度和高度设置为静态的? - Shweta Chauhan
首先,我使用了match_parent,但是当我选择match_parent作为高度时,我的图像会自动缩小。 - alongquan
4个回答

4

按照以下方式操作,并告诉我你得到了什么,因为这是最理想的做法,因为每个人都在说同样的话,但是要这样做。 match_parent 通常是关键

<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"
 android:focusable="true"
 android:focusableInTouchMode="true"
 tools:context="njc.nhutrinhgold.MainActivity">

<ImageView
   android:id="@+id/imgNen"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:scaleType="centreCrop"
   android:visibility="visible"
   app:layout_constraintDimensionRatio="16:9"
   app:srcCompat="@drawable/xxhdpibg"
   tools:layout_editor_absoluteX="0dp"
   tools:layout_editor_absoluteY="8dp"
   tools:layout_width="match_parent" />

移除adjustViewBound并将scaleType设置为centreCrop,您将看到变化。

**注意:**这是为了使布局占据整个屏幕并进行调整(拉伸到整个屏幕)


我设置了match_parent,但在运行应用程序后,代码自动调整为硬编码的宽度和高度.. android:layout_width="xxxdp" android:layout_height="xxxdp" - alongquan
这是因为您设置的比例不允许图像正确地扩展。请查看 app:layout_constraintDimentionRatio。我的代码是为了将图像拉伸以占据整个屏幕。 - Alok

3

试一下这个:

<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"
    android:focusable="true"
    android:focusableInTouchMode="true"
    tools:context=".MainActivity">

<ImageView
    android:id="@+id/imgNen"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:visibility="visible"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@mipmap/ic_launcher"
    />


</android.support.constraint.ConstraintLayout>

glUtilsParamSize:未知参数0x000082da。 - alongquan
尝试删除此行代码 app:layout_constraintDimensionRatio=""。 - Shweta Chauhan
我运行了我的代码,图片丢失了 http://www.upsieutoc.com/image/TJEwWG http://www.upsieutoc.com/image/TJE0eE - alongquan

1
不要硬编码约束布局和其中的ImageView的高度和宽度。将其设置为match_parent,它将在任何设备上拉伸至全屏。

1
我设置了match_parent,但它没有生效。 - alongquan
@alongquan 在 Constraint Layout 中,你应该从布局编辑器中设置而不是写 match_parent,因为它会将高度或宽度设置为 0dp,这在使用 match_parent 时是必须的。 - user8583580
首先,我将布局和图像设置为MatchParent,但它没有起作用,所以我尝试修复宽度和高度,但仍然不起作用。 - alongquan
请参考以下链接,它将帮助您理解:https://developer.android.com/training/constraint-layout/index.html - user8583580
现在,当我在菜单选项中选择match_parent时,它不被接受,系统会自动更改为另一个数字(223dp或0 dp...)。 - alongquan
显示剩余5条评论

0

您已经给定了硬编码的宽度和高度。如果您继续使用硬编码的宽度和高度,它将永远不会拉伸。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接