2012-02-10 94 views
14

我不知道如何獲得圖像來填充屏幕,嘗試了一切,也許有一些特定的佈局,我應該使用..?Android:如何獲取ImageView來填充屏幕

我使用的是ImageView的,然而,這只是填滿屏幕的約70%,在底部留下空白......

目前我只是使用的LinearLayout和背景設置爲我的形象。雖然這填滿了屏幕,但圖像略微拉伸,我不想要。

任何幫助將不勝感激! :)

我的佈局文件的ImageView的標籤中的代碼是:

android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:scaleType="centerCrop" 
android:adjustViewBounds="true" 
android:src="@drawable/image" 
+0

android:layout_height =「match_parent」會做 – njzk2 2012-02-10 15:56:08

回答

42

://在你的XML圖像屬性作爲

android:scaleType="fitXY" 

或 //使用直通編程

imgview.setScaleType(ScaleType.FIT_XY); 

注:這適用於當圖像設置使用Android :src =「...」而不是android:background =「...」,因爲背景默認設置爲伸展並適合視圖。

+0

當你說通過你的編碼也使用這個,你的意思是在你的活動? – Shaw 2012-02-10 15:35:08

+0

@Shaw no我的意思是你也可以通過程序 – 2012-02-10 15:38:32

+2

這個答案會拉伸圖像 - 我知道這不是OP的要求,但是如果你想要一個填充屏幕而不損壞縱橫比的答案,請檢查下面的@Revs回答。 – ntgCleaner 2016-01-19 21:33:11

4

使用下面的線在圖像View XML代碼。

android:scaleType="fitXY" 
+0

這將不會保持比例 – njzk2 2012-02-10 15:55:37

+0

可能會,但它會填滿屏幕,因爲OP想要的,如果圖像分辨率足夠好,那麼它不應該給出問題。 – Sunny 2012-02-10 16:21:13

-1

您幾乎可以使用任何佈局,只需將背景圖像設置爲圖像即可。然後,只需使寬度和高度填充父項即可。如果你想

或實例只是一個形象設置你的主線性佈局

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:orientation="horizontal" 
     android:layout_height="fill_parent" 
     android:background="@drawable/yourimage"> 
</LinearLayout>  
6
android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scaleType="centerCrop" 
    android:adjustViewBounds="true" 
    android:src="@drawable/image" 

只是改變佈局的寬度和高度將與這兩個「中心」和「centercrop」

+1

不確定爲什麼這不被接受 - 這個答案沒有拉伸圖像和失去正確的寬高比。 – ntgCleaner 2016-01-19 21:32:34

0
工作

不要忘記將取下的LayoutParams

android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 

,在活動的XML的頂部。 否則圖像填充整個屏幕時會出現問題。