2011-03-29 160 views
8

我想設置像背景圖像的圖像中的佈局(通過XML或代碼)問題與佈局的背景圖像

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/main_layout" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/my_image"> 

View myLayout= findViewById(R.id.main_layout); 
myLayout.setBackgroundResource(R.drawable.my_image); 

我的問題是比Android修復全背景屏幕中的圖像,調整大小和修改圖像比例。

如何設置圖像,並且圖像僅佔用了必要的空間? (沒有圖像大小調整)

+0

如果你設置了android:layout_width =「fill_parent」,android:layout_height =「fill_parent」也是你的背景「fill_parent」 – carlovv 2011-03-29 10:33:22

回答

0

Jst在LinearLayout中使用另一個佈局。

將它作爲相對值,以便您可以隨時隨地保持該佈局。 和相對佈局你保持imageView。

雅就是這樣我覺得你想要什麼....

0

「的setBackground」調整圖像來填充視圖(LinearLayout中)。
請在LinearLayout或RelativeLayout中添加ImageView,然後使用setImageResource來實現。

7

無需添加ImageView佈局,如果圖像是比空間更小,你可以創建一個xml bitmap resource像以下內容,以代替原來的背景圖像的使用:

<?xml version="1.0" encoding="utf-8"?> 
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@drawable/background" 
    android:gravity="center" 
/> 
+0

這個圖像真的像全屏幕(480x800),在midle有一個標誌。但是當我像背景設置圖像時,圖像被佔用在可見屏幕上(全屏減去狀態欄...) 我想避免爲不同屏幕分辨率的圖像。 :( – 2011-03-29 14:29:28

+0

@Jose也許你可以嘗試擺弄引力設置,如果你不能得到你想要的東西,那麼'ImageView'是唯一的解決方案,但請記住w/h ratio隨設備而變化,並且圖像尺寸合適對於其中一個可能無法在任何地方工作。 – bigstones 2011-03-29 14:43:50

6

把你的LinearLayout放置在RelativeLayout中,並將ImageView添加到該RelativeLayout。

<RelativeLayout 
    android:id="@+id/relativeLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" > 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     android:scaleType="fitXY" 
     android:src="@drawable/my_image" /> 

    <LinearLayout 
     android:id="@+id/main_layout" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

請注意,ImageView是RelativeLayout中的第一個元素,導致它在後臺繪製。通過在ImageView中聲明fitXY和adjustViewBounds,確保圖像保持其比例。您也可以關閉adjustViewBounds並選擇另一個scaleType,如fitStart或centerCrop。他們也保持寬高比。

+0

更容易實施 – 2013-06-14 15:11:08