2014-06-16 39 views
0

我有這個ImageView,它填充整個頁面。圖像iteself大約是它的1/3,並且集中在imageview的中間。如何檢測圖像的寬度和高度?

如何檢測圖像的寬度和高度,以便ImageView可以環繞它而不是填充整個頁面?

代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<ImageView 
    android:id="@+id/ivimage" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:src="@drawable/picture" /> 

+4

你並不需要,只要設置' android:adjustViewBounds =「true」' – jyoon

+0

@jyoon就是這樣,謝謝 – user154989

回答

0

開關機器人:layout_height = 「FILL_PARENT」 到機器人:layout_height = 「WRAP_CONTENT」

FILL_PARENT(棄用,在API級別更名MATCH_PARENT 8和更高的)

將小部件的佈局設置爲fill_parent將強制它擴展以佔用儘可能多的佈局元素內的空間被放置在。

你也可以以編程方式設置ImageView的大小,如果那是你知道圖像的大小。

image_view.getLayoutParams().height = 20; 

(注)如果你設置後的佈局已經「佈局」的高度,確保你調用image_view.requestLayout()