2010-05-25 58 views
10

我有我的顯示器的頂部,看起來像下面的ImageView的:如何爲ImageView指定動態高度?

╔══════════════════════════════════════════════╗ 
║ ImageView ╔══════════════╗    ║ 
║    ║    ║    ║ 
║    ║ Actual image ║    ║ 
║    ║    ║    ║ 
║    ║    ║    ║ 
║    ║    ║    ║ 
║    ╚══════════════╝    ║ 
╚══════════════════════════════════════════════╝ 

下面這一點,我有一系列的按鈕和TextViews的..

我想的ImageView高度動態根據屏幕的最大高度而增加。我沿着屏幕邊緣的底部對齊包含按鈕的佈局,並且我希望上述ImageView佔用屏幕的其餘部分。另外,由於ImageView包含一個居中圖像,我還想設置最小高度,如果屏幕太小而不能顯示下面的圖像視圖和按鈕,則需要一個滾動條。

謝謝!

這可能嗎?

+13

+1 ascii visual awesomeness。 – womp 2010-05-25 19:20:07

回答

5

如果您使用android:layout_width,第一部分應該很容易。如果僅爲ImageView(或其容器)設置layout_width,它將展開以佔用儘可能多的父佈局。例如,如果你想要一個佈局,其中一個ImageView的佔去了整個屏幕的休息,你可以這樣做:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ImageView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:src="@drawable/icon" 
     android:scaleType="fitCenter" android:layout_weight="1" /> 
    <LinearLayout android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     <!-- Put whatever you want in here --> 
    </LinearLayout> 
</LinearLayout> 

在這個例子中,我拉伸圖像以佔滿整個屏幕,但您還會提出「如果圖像對於屏幕來說太大而我需要滾動會怎麼樣?」的問題。在這種情況下,您只需將ScrollView添加到佈局。如果圖像太高垂直時,屏幕會自動滾動:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:fillViewport="true"> 
    <LinearLayout android:orientation="vertical" 
     android:layout_width="fill_parent" android:layout_height="fill_parent"> 
     <ImageView android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:src="@drawable/huge" 
      android:scaleType="fitCenter" android:layout_weight="1" /> 
     <LinearLayout android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
      <!-- Insert your content here --> 
     </LinearLayout> 
    </LinearLayout> 
</ScrollView> 

一個重要的一點要注意:如果你沒有android:fillViewport設置爲true的滾動型,太小會被填滿ImageViews整個屏幕。

0

完全不是你正在尋找的答案,但它絕對可以通過你的課堂上的相關佈局滾動視圖和動態編碼的混合。目前不在編程計算機附近,但模糊的答案是「可能的,是的!」 :)