防止ImageView将视图推出屏幕外

8
我有一个线性布局,其中包含一个ImageView和另一个线性布局,后者是屏幕底部的底栏。我在项目中的其他地方使用底栏,我不认为它是问题所在。
问题是ImageView占据了整个屏幕。图像本身并没有,但视图确实如此。我似乎无法解决这个问题。如您在xml中所见,我没有使用fill_parent来设置ImageView,并尝试了所有可能的图像比例缩放类型。
有任何建议吗?如果可以的话,我正在构建Android API 8
编辑:我已更改以下xml,以便您可以完全使用我编写的内容。(这是我在ImageView中使用的图像:http://i.imgur.com/qN5aT.jpg
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#FFFFFF" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitStart"
        android:src="@drawable/quote_sample_img" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="This is text" />
    </LinearLayout>

</LinearLayout>

http://i.imgur.com/m3msH.png

这是一个指向图片的链接。

你尝试过使用layout_weight属性来设置ImageView布局和第二个LinearLayout之间空间的固定比例吗?效果如何? - pogo2065
那个有点奏效,但不是我预期的方式。我尝试了很多参数,当我给图像设置layout_weight=".8",线性布局和layout_weight=".2"时,底部栏出现了。奇怪的是,我给线性布局分配的权重越多,它出现的越少。这对我来说毫无意义。你有什么想法为什么会发生这种情况吗? - amc6
嗯,有没有办法上传整个项目?我试着将那个主要的 XML 代码放到一个新的 Eclipse Android 项目中运行,但是我一直得到无法解决的错误(我也是新手...)。如果我能看到你的源代码并打开它,那么我可能就能修复它。 - pogo2065
是的,主要的问题是我在项目的其他地方创建的。不过我刚刚编辑了我的帖子,删除了任何依赖项。你应该能够使用它并看到问题所在。感谢你的帮助。 - amc6
我认为不幸的是你必须将其四倍间隔。 - amc6
显示剩余2条评论
4个回答

12

如果在 ImageView 中添加 android:layout_weight="1.0",似乎可以正常工作。

(我不确定布局算法中确切发生了什么以及顺序,因此无法解释为什么。)

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:scaleType="fitStart"
        android:src="@drawable/quote_sample_img" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="This is text" />
    </LinearLayout>

</LinearLayout>

1
是的,这样修复了,但我仍然不明白。layout_weight为1不应该使ImageView尽可能大吗?为什么会变小呢? - amc6
1
将layout_weight设置为1.0并不总是意味着视图会尽可能地变大。这取决于我们在设计中分配给其他视图的权重。查看此帖子以获取更多详细信息https://dev59.com/xG865IYBdhLWcg3wFqdV - HenrikS
这就是我浪费的一小时,因为这没有任何意义! - Recycled Steel

2

这是图片中的wrap_content - 源文件可能太高了。

在您的ImageView中使用此选项可以使图像区域在缩放后重置为图像的新大小:

android:adjustViewBounds="true"

我还会改变ImageView的高度,让它充满整个屏幕并强制您的工具栏始终位于底部:

android:layout_height="0dip" // faster than match_parent for weight
android:layout_weight="1.0"

0

我认为问题可能是你使用的图片比屏幕要大,所以ImageView的高度被设置为适应图片的最佳空间高度,不留任何其他空间。

此外,如果你想始终保持底部栏可见,最好使用RelativeLayout,像这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/Main" 
    android:orientation="vertical" >
    <LinearLayout   
        android:id="@+id/bottom_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:layout_alignParentBottom="true"
        android:orientation="vertical" >
        <include layout="@layout/browse_bottom_bar" />
    </LinearLayout>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/bottom_bar"
        android:src="@drawable/quote_sample_img" />
</RelativeLayout>

我之前一直在使用RelativeLayout,但是一开始我认为我使用不当导致了我发布的问题。我换成了LinearLayout,但问题仍然存在,所以我发表了那篇帖子。我稍后会切换回去。你说得对,源图像最初比屏幕大,直到被调整大小。但是在调整大小后,ImageView 仍占据整个屏幕 http://i.imgur.com/m3msH.png。 - amc6

0

Kai的帖子中包含了似乎缺失的链接...先声明固定页脚,然后是以ImageView结尾的顶部部分。如果你的布局比这更复杂,那么天哪,祝你好运... - Ian

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