阻止ImageView在屏幕外推送视图

时间:2012-03-13 02:37:48

标签: android android-layout imageview android-imageview

我有一个线性布局,其中包含一个imageview和另一个线性布局,它是屏幕上的底栏。我在项目的其他地方使用底栏,我不认为这是问题所在。

问题是imageview会占用整个屏幕。图像本身没有,但视图确实如此。我似乎无法弄清楚这一点。正如你在我的xml中看到的那样,我没有在图像视图中使用fill_parent,我已经为图像尝试了所有可能的scaleType。

有什么建议吗?此外,如果它有助于我构建到Android API 8

编辑:我已经更改了下面的xml,以便您可以使用我所写的内容。 (这是我在图像视图中使用的图像: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 (抱歉没有足够的声望发布实际图片)

4 个答案:

答案 0 :(得分:12)

如果将android:layout_weight =“1.0”添加到ImageView,事情似乎有效。

(我不确定布局算法究竟发生了什么以及布局算法的顺序,因此无法解释原因。)

<?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 :(得分:1)

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

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

android:adjustViewBounds="true"

我还会更改ImageView的高度,使其填满屏幕并强制您的栏始终位于底部:

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

答案 2 :(得分: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>

答案 3 :(得分:0)

我有一个非常类似的问题,发布在https://stackoverflow.com/questions/9765401/imageview-with-adjustviewbounds-seems-to-ignore-following-elements

在缩放图像后,ImageView似乎没有收缩,除非你指定adjustviewbounds =&#34; true&#34;。

但是,正如你从我自己的帖子中看到的那样,并不是完整的答案:如果图像太大,图像视图仍然可能会将项目推到显示屏下方,或者进入他们的空间,我和#39 ;我正在尝试修复。