2013-09-28 21 views
1

我不知道爲什麼這不能正常工作。我在佈局容器中添加圖像。我希望圖像爲768x1024,容器爲500x500。我這樣做是爲了讓我可以在遊戲中滑動圖像,但是我無法讓圖像比容器更大。圖像大小調整爲容器的大小。Android調整ImageView內部佈局

  containerView = new RelativeLayout(this); 
    RelativeLayout.LayoutParams layoutForContainer = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    layoutForContainer.height = 500; 
    layoutForContainer.width = 500; 
    layoutForContainer.addRule(RelativeLayout.CENTER_IN_PARENT); 
    containerView.setLayoutParams(layoutForContainer); 
    layout.addView(containerView); 

    myImageView = new ImageView(this); 
    myImageView.setImageResource(R.drawable.myImage); 
    RelativeLayout.LayoutParams layoutForLargeImage = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    layoutForLargeImage.height = 768; 
    layoutForLargeImage.width = 1024; 
    myImageView.setLayoutParams(layoutForLargeImage); 
    containerView.addView(myImageView); 
+0

'ImageView.setScaleType()'http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType – Simon

+0

這確實不改變ImageView的大小隻會改變ImageView內部的圖像。 – Bobby

回答

1

使用下面的代碼:

myImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
myImageView.setAdjustViewBounds(true); 
+0

它不會改變任何東西,myImageView仍然是容器相同的大小。 – Bobby