简体   繁体   English

Android 无法将 ImageView 与活动顶部对齐

[英]Android cannot align ImageView to top of activity

For some reason, I'm having difficulty aligning ImageView's to the top of the activities in my Android applications.出于某种原因,我很难将 ImageView 与 Android 应用程序中的活动顶部对齐。 For example:例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:scaleType="fitStart"
        android:src="@drawable/myImage" />

    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

I was able to get the ImageView to align to the top of the activity with "fitStart", but without that then the imageView wouldn't be all the way at the top.我能够使用“fitStart”让 ImageView 与活动的顶部对齐,但如果没有它,那么 imageView 就不会一直处于顶部。 Now that the ImageView is on top, the ListView isn't directly below it.现在 ImageView 位于顶部,ListView 不在其正下方。 Instead, it is a quarter of the screen down.相反,它是屏幕向下的四分之一。 This is happening on all of my activities, even ones without ListView's.这发生在我的所有活动中,即使是没有 ListView 的活动。

As a side note, I did remove the system title bars from my activities in AndroidManifest.作为旁注,我确实从 AndroidManifest 中的活动中删除了系统标题栏。 However, even when I add them back in, the problem is still occurring.但是,即使我重新添加它们,问题仍然存在。

Any ideas?有任何想法吗?

Fixed by using RelativeLayout's通过使用 RelativeLayout 修复

hi the problem is that u r using android:layout_gravity="center_horizontal" for the imageview which will align the view to the center of the parent layout...嗨,问题是你 r 使用android:layout_gravity="center_horizontal"用于 imageview 的父布局的中心...

also have look at this也看看这个

Hope this solves your problem...希望这能解决您的问题...

Maybe that's because your image has transparent borders?也许那是因为您的图像具有透明边框? Here is what I mean:这就是我的意思:

在此处输入图像描述

It seems that the image that I was using was too large, and after it scaled the problem was occurring.我使用的图像似乎太大了,并且在缩放后出现了问题。 Once I used a smaller image, everything aligned perfectly to the top.一旦我使用了较小的图像,一切都与顶部完美对齐。 Thanks!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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