如何设置我的ImageView到屏幕顶部不管什么分辨率?

问题描述 投票:-1回答:3

因此,我试图去与屏幕的顶部对齐图像和因此目前ImageView在那里你可以看到电池的酒吧等之间的差距

它不显示在预览布局的差距,但它确实在我的手机。

enter image description here

<?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" />
java android android-layout android-constraintlayout
3个回答
0
投票

我用这个。你可以试试这个。

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

0
投票

要设置任何视图上的父顶部使用此属性

应用程式:layout_constraintTop_toTopOf = “@ + ID / PARENT_ID”

这是我的代码

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

使用线性布局另一个代码

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

注意:尝试使用其他图片

我希望它为你工作


0
投票

所有你需要做的就是在你的imageView添加以下代码

 app:layout_constraintTop_toTopOf="parent"

和其他的方法是使用LinearLayoutRelativeLayout为你的父母布局

© www.soinside.com 2019 - 2024. All rights reserved.