图库仅显示所选图片

时间:2012-08-03 07:17:52

标签: android gallery

我正在使用Android 2.3.6和2.3.1。

Gallery我有不受欢迎的行为:图库项目中的图片不会显示。

我的图库占用了所有可用空间,包含ImageViewTextView的布局设置为WRAP_CONTENT但仍然占据了所有屏幕,因此边框项目超出范围。 ..

这是画廊的xml

<RelativeLayout xmlns:a="http://schemas.android.com/apk/res/android"
    a:layout_width="match_parent"
    a:layout_height="match_parent"
    a:layout_margin="0dp"
    a:padding="0dp">

    <ImageView
        a:id="@+imagePopup/selected"
        a:layout_width="60dp"
        a:layout_height="35dp"
        a:layout_alignParentLeft="true"
        a:layout_alignParentTop="true"
        a:src="@drawable/ic_cancel"
        a:contentDescription="@string/lng" />
    <TextView
        a:id="@+imagePopup/title"
        a:layout_alignParentTop="true"
        a:layout_toRightOf="@+imagePopup/selected"
        a:layout_alignWithParentIfMissing="true"
        a:layout_width="match_parent"
        a:layout_height="35dp"
        a:background="@drawable/rounded"
        a:text="dynamic text"
        a:textSize="23dp"
        a:textColor="#ffffffff"
        a:gravity="center"
        style="@style/trad"/>
    <Gallery
        a:id="@+imagePopup/gallery"
        a:layout_width="match_parent"
        a:layout_height="match_parent"
        a:layout_below="@+imagePopup/title"
        a:layout_alignWithParentIfMissing="true"
        a:background="#ff000000"/>
</RelativeLayout>

及其内容xml

<RelativeLayout xmlns:a="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    a:layout_width="wrap_content"
    a:layout_height="wrap_content"
    a:background="#ffff55ee">

    <ImageView
        a:id="@+imageContent/imageView"
        a:layout_width="wrap_content"
        a:layout_height="wrap_content"
        a:layout_alignParentTop="true"
        a:padding="0dp"
        a:layout_margin="0dp"
        a:src="@drawable/ic_cancel" />

    <TextView
        a:id="@+imageContent/textOver"
        a:layout_width="wrap_content"
        a:layout_height="wrap_content"
        a:layout_alignParentTop="true"
        a:layout_centerHorizontal="true"
        a:layout_marginTop="4dp"
        a:shadowColor="#FF000000"
        a:shadowRadius="4"
        a:textColor="#ffffffff"
        a:textSize="16dp"
        a:textStyle="bold" />

</RelativeLayout>

我在图片容器的背景(RelativeLayout)中放置了一个丑陋的背景颜色,以显示它的行为:

gallery display

有人能告诉我这里我做错了什么......我无法摆脱它。

提前致谢^^

2 个答案:

答案 0 :(得分:1)

我认为问题与你的形象有关。您正试图显示一个更有可能覆盖整个屏幕的巨大图像。

您必须为Image提供一些预定义的宽度和高度属性。

而不是wrap_content你提供的内容如下所示,

  <ImageView
        a:id="@+imageContent/imageView"
        a:layout_width="wrap_content"
        a:layout_height="wrap_content"
        a:layout_alignParentTop="true"
        a:padding="0dp"
        a:layout_margin="0dp"
        a:src="@drawable/ic_cancel" />

尝试提供类似这样的内容,

  <ImageView
    a:id="@+imageContent/imageView"
    a:layout_width="150dip"
    a:layout_height="150dip"
    a:layout_alignParentTop="true"
    a:padding="0dp"
    a:layout_margin="0dp"
    a:src="@drawable/ic_cancel" />

注意:我在这里指定的布局宽度和高度只是虚构的。你必须提供自己的。

你必须知道wrap_content,fill_parent和实际硬编码宽度和高度之间的区别。

  1. Wrap_cotent - 这意味着它会使用实际图片的宽度和高度。

  2. fill_parent - 无论图片大小如何,都需要整个屏幕。

  3. 通过我们自己的方式提供高度和宽度,通过提供特定的宽度和高度属性,可以使图像的大小均匀显示。

答案 1 :(得分:1)

最后,我找到了一个更好的方法来做到这一点,因为我需要更“自然”: 这是我的代码的修改部分:

<RelativeLayout xmlns:a="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    a:layout_width="wrap_content"
    a:layout_height="match_parent"
    tools:ignore="ContentDescription">

    <ImageView
        a:id="@+imageContent/imageView"
        a:layout_width="wrap_content"
        a:layout_height="match_parent"
        a:layout_alignParentTop="true"
        a:scaleType="fitXY"
        a:adjustViewBounds="true"
        a:src="@drawable/ic_cancel" />

scaleType="fitXY"adjustViewBounds="true"很快就成功了,感谢question

相关问题