【问题标题】:ImageView changes position after loading the pictureImageView 加载图片后改变位置
【发布时间】:2013-11-06 14:45:15
【问题描述】:

加载图片后出现 ImageView 位置问题。开始时,我在 ImageView 中有示例图片的应用程序,然后在从图库中选择另一张图片后,旧的被新的替换,不幸的是,imageView 在同一时刻改变了位置。如何阻止它?如何让 ImageView 一直在同一个位置?

这是我的 xaml 代码:

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

<EditText
    android:id="@+id/add_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="66dp"
    android:ems="10"
    android:hint="add name of product"
    android:inputType="textPersonName" />

<TextView
    android:id="@+id/prod_show_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Name of Product"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/add_description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/prod_show_description"
    android:layout_marginTop="31dp"
    android:ems="10"
    android:hint="here put the description of product "
    android:inputType="textMultiLine" />

<TextView
    android:id="@+id/prod_show_description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/add_name"
    android:layout_marginTop="31dp"
    android:text="Description Of Product "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<ImageView
    android:id="@+id/add_picture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/prod_show_description"
    android:layout_below="@+id/add_description"
    android:layout_marginRight="22dp"
    android:layout_marginTop="45dp"
    android:maxHeight="120dp"
    android:maxWidth="120dp"
    android:minHeight="120dp"
    android:minWidth="120dp"
    android:scaleType="centerInside"
    android:src="@drawable/sample" >
</ImageView>

<Button
    android:id="@+id/add_addpicture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/add_picture"
    android:layout_marginRight="141dp"
    android:minHeight="80dp"
    android:onClick="AddPicture"
    android:text="Add Picture " />

<Button
    android:id="@+id/add_addingbut"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/add_picture"
    android:layout_marginTop="119dp"
    android:onClick="AddTheProductMethod"
    android:text="Add the product" />

【问题讨论】:

  • 嗨,这是因为它们都在RelativeLayout 中,这让我相信这两个图像的大小不同。请告诉我图片sample 的大小以及您稍后要放入ImageView 的图片的大小?
  • 您有layout_width wrap_content 以及minWidth 120dp 和maxWidth 120dp。高度相同。这是非常不寻常的做法。只需省略 minWidthmaxWidth 并让您的 layout_width 120 dp
  • 示例图片是drawable xhdpi,复制到这个文件夹的文件是336x299,后面放的图片是平板的相机(2560x1920),但是有什么意义,如果我的 imageView 缩放它 ("android:scaleType="centerInside"") 并具有恒定大小?

标签: android imageview android-relativelayout


【解决方案1】:

imageviewandroid:layout_heightandroid:layout_width更改为120dip最好使用dip而不是dp

<ImageView
    android:id="@+id/add_picture"
    android:layout_width="120dip"
    android:layout_height="120dip"
    android:layout_alignRight="@+id/prod_show_description"
    android:layout_below="@+id/add_description"
    android:layout_marginRight="22dp"
    android:layout_marginTop="45dp"
    android:scaleType="centerInside"
    android:src="@drawable/sample" >
</ImageView>

【讨论】:

  • dipdp 是一样的。使用dp 完全没问题。但我同意省略 minHeight maxHeight 的内容并将该值放入 layout_height。另见stackoverflow.com/questions/7608251/is-dp-the-same-as-dip
  • 当然,我很久以前就为 android 研究过 dpdip,所有谷歌开发者都建议使用 dip 而不是 dp for imageView 这只是一个建议:)跨度>
  • 是的,它的工作原理,谢谢,但我的问题是为什么? wrap_content 表示只需要一个区域来显示里面的图片。所以当我不设置任何限制时它会像图片一样大,但我设置了maxheight和maxwidth。 imageView 没有改变仅尺寸位置,所以我可以说我的限制有效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多