简体   繁体   中英

Android Constraint Layout doesn't stretch on full screen

I have a problem with layout which won't stretch within high resolution like 1920x1080. As root layout I use Constraint Layout with following style:

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

and in 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" />

I want to stretch layout to any screen resolution but on emulator with high resolution it looks like this:

在此处输入图片说明

Actually I didn't figured out what is the problem

Do it like this, and tell me what are you getting, because this is the ideal way of doing this, since everybody is saying the same thing but do it like this. match_parent is generally the key

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

Remove adjustViewBound and set the scaleType as centreCrop, you'll see the change

**NOTE : ** This is for the layout to take up the whole screen and adjust (stretching to the whole screen)

Don't hardcode the height and width of Constraint layout and the ImageView inside it. Set it to match_parent and it will stretch full screen in any device.

try this:

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

You have given hardcoded width and height. It will never stretch if you keep working on hardcoded width and height.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM