2013-05-21 17 views
0

我在缩放的相对布局上设置图像。但是,在设置图像时,布局对齐规则会被破坏。该图像应该与左侧对齐,但会发生移位。图像越大,图像放置的距离越远。其他布局项目(例如依赖图像位置的文本)也会移动。缩放后的图像看起来很好,除了它的位置。ImageView在设置图像时不符合布局规则

图像是200px,酒吧的高度的实际大小是70dp。我可以调整图像大小,但是我想知道为什么发生这种情况。

编辑 - 我自己也尝试设置图像动态image.setImageResource(R.drawable.placeholder);

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:background="@drawable/footer" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     > 
    <ImageView 
      android:id="@id/icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/placeholder" 
      android:layout_alignParentLeft="true" 
      /> 
    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="test" 
      android:layout_marginLeft="15dp" 
      android:layout_toRightOf="@id/icon" 
      android:layout_above="@id/username"/> 

    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:id="@id/username" 
      android:text="user name test" 
      android:layout_marginBottom="15dp" 
      android:layout_marginLeft="15dp" 
      android:layout_toRightOf="@id/icon" 
      android:layout_alignParentBottom="true"/> 
    <ToggleButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/play_media_btn_toggle" 
      android:layout_marginTop="15dp" 
      android:id="@id/play" 
      android:layout_alignParentRight="true" 
      android:textOn="" 
      android:textOff=""/> 
</RelativeLayout> 
+0

你能分享一下这个问题的截图吗? –

+1

你在imageview上设置了“adjustViewBounds”吗?这可能是问题所在。 –

+0

@ Sandervan'tVeer修复它谢谢!如果您回复,我会将其设置为正确的答案。我看到了这个文档,但并没有真正理解属性的作用。 – serenskye

回答

0

设置你的ImageViewtrueadjustViewBounds可能会解决你的问题。默认情况下,ImageViews在缩放其内容时不会缩放边界,从而创建混乱的布局。

0

如果动态设置你的形象,当你的图像大小调整你的持有人不设置所以你有一些奇怪的不成比例的差距,以适应图像一边(宽度或高度)。你需要动态地做到这一点,所以你会避免任何差距。编辑:这里是一个例子,我如何缩放图像(以适应宽度),然后将其设置为持有人,并相应地调整大小持有人,这种方式我控制持有人和图像和ImageView(持有人)规模类型的宽度是中央。

float scaleFactor = (float)rssLayout.getWidth()/(float)bitmap.getWidth();  
    bitmap = Bitmap.createScaledBitmap(bitmap, (int)(bitmap.getWidth() * scaleFactor), (int)(bitmap.getHeight() * scaleFactor), true); 
    bannerImage.setImageBitmap(bitmap); 
    LayoutParams params = (LayoutParams) bannerImage.getLayoutParams();    
    params.width = bitmap.getWidth(); 
    params.height = bitmap.getHeight(); 
    bannerImage.setLayoutParams(params); 

希望这有助于和喜欢你的工作

+0

Hey Marko,我最初动态地设置图像,这导致我调查问题'imageView.setImageResource(placeholder_id);',这会产生相同的问题 – serenskye