简体   繁体   中英

How do I set my ImageView to the top of the screen no matter what resolution?

So I am trying to get the image to align with the top of the screen and there is currently a gap between the ImageView and the bar where you can see the battery etc.

It doesn't show the gap in layout preview but it does on my phone.

在此处输入图片说明

<?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"
    android:background="@color/Background"
    android:orientation="vertical"
    tools:context=".SomeActivity"
    android:gravity="center">


    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:src="@drawable/man"
        tools:layout_editor_absoluteX="0dp" />

I am using this. You can try this.

  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:fitsSystemWindows="true">


        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="16dp"
            android:src="@drawable/logo" />
   </ScrollView>

To set any view on parent top to use this attribute

app:layout_constraintTop_toTopOf="@+id/parent_id"

this is my code

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical"
android:id="@+id/main">


<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/demo_img_4"
    app:layout_constraintTop_toTopOf="@+id/main" />

another code using linear layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical">
<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="350dp"
    app:srcCompat="@drawable/demo_img_4" />
 </LinearLayout>

note: try another image

I hope it work for you

All you need to do is add following code in your imageView

 app:layout_constraintTop_toTopOf="parent"

and other approach would be to use LinearLayout or RelativeLayout as your parent Layout

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