2012-08-13 121 views
2

我想用一個圖像(1280x800)爲兩個設備與不同的屏幕尺寸和密度相同。如何適合佈局背景圖片

屏幕尺寸爲1280x800320x480

圖像是九個補丁(內容區)。我想使用圖像作爲行的背景,有一個textview。不要使用手動填充文本(我想使用內容區域)。

如果我使用此xml:在仿真器

<RelativeLayout 
xmlns:android = "http://schemas.android.com/apk/res/android" 
xmlns:foo="http://schemas.android.com/apk/res/pak.name" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@layout/menu_selector" 
android:gravity="right|center_vertical"> 

<pack.name.TextViewPlus 
    android:id="@+id/menutext" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#3D1E00" 
    android:textSize="@dimen/TitleNormalM" 
    foo:customFont="font.ttf" 
    android:text="blah blah"/> 

輸出是:

http://ivm.neru9.com/pic/2ccf95a7f2e1.png

和如果我使用此xml:

<RelativeLayout 
xmlns:android = "http://schemas.android.com/apk/res/android" 
xmlns:foo="http://schemas.android.com/apk/res/pack.name." 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:paddingRight="15dp" 
android:paddingLeft="15dp"> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@layout/menu_selector" 
    android:scaleType="fitCenter" 
    /> 

<pack.name..TextViewPlus 
    android:id="@+id/menutext" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#3D1E00" 
    android:textSize="@dimen/TitleNormalM" 
    foo:customFont="font.ttf" 
    android:gravity="right|center_vertical" 
    android:text="blah blah blah"/> 

輸出爲:

http://ivm.neru9.com/pic/f20f7f5d2e7a.png

,這是列表視圖XML(我認爲並不重要,對於這個問題):

<ListView 
    android:id = "@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:divider="@null" 
    android:dividerHeight="3dp" 
    android:layout_marginTop="5dp" 
    android:layout_marginBottom="5dp" 
    android:cacheColorHint="@android:color/transparent" 
    android:listSelector="@android:color/transparent" 
    /> 

及其menu_selector XML代碼,佈局文件夾:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<!-- pressed --> 
<item android:state_pressed="true" android:drawable="@drawable/bg_menu_pressed" /> 
<!-- selected --> 
<item android:state_selected="true" android:drawable="@drawable/bg_menu_focused" /> 
<!-- selected & pressed --> 
<item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/bg_menu_pressed" /> 
<!-- default --> 
<item android:drawable="@drawable/bg_menu" /> 

回答

2

check out this project

主要問題是你的9補丁圖像,我只是修改您的圖像,爲您的問題的願望的表現。

我建議您使用bg_menu277x77來處理1280x800和bg_menu144x40,以便從上方鏈接的項目獲得320x480分辨率。僅用於測試,您可以將兩個圖像都轉換爲分辨率並嘗試。

婁我盡我所能來解釋9補丁圖像,其他偉大的教程&信息,您可以google一下:)

enter image description here

+0

tnx您的關注。關於第1點的 :由於圖像的陰影底部,我已經拉伸了我的圖像。 /畢竟,它沒有解決我的問題,你改變我的問題。我說要使用一個有9patch的大圖像,並且可以伸縮。我認爲它是不可能的(兩種技術:佈局背景中的A-九塊補丁。並對某些屏幕尺寸使用ONE圖像)。 – 2012-08-14 05:07:07

+1

'兩種技術:在佈局背景中的九個補丁。 B可縮放的圖像和一些屏幕尺寸使用一個圖像',是的同意克服這一點,你必須分別爲更小和更大的屏幕創建9patch圖像。如果您想要一張圖像適合所有人,則可以保留一個像素。 +1 :) – 2012-08-14 10:31:32

+0

@hotveryspicy感謝您的更新。 – rajpara 2012-08-14 10:36:03

0

如果我正確理解您的要求,那麼您想要一個列表視圖,並且列表視圖的每一行都包含背景和文本視圖以顯示一些文本。
您不必爲此使用圖像視圖。 您只需爲佈局容器和文本視圖放置背景。

這裏是你如何能做到這一點:

<RelativeLayout 
xmlns:android = "http://schemas.android.com/apk/res/android" 
xmlns:foo="http://schemas.android.com/apk/res/pack.name." 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:paddingRight="15dp" 
android:paddingLeft="15dp" 
android:background="@drawable/some_drawable or any selector"> 

<pack.name..TextViewPlus 
    android:id="@+id/menutext" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#3D1E00" 
    android:textSize="@dimen/TitleNormalM" 
    foo:customFont="font.ttf" 
    android:gravity="right|center_vertical" 
    android:text="blah blah blah"/> 
</RelativeLayout> 

與您的代碼的問題是,你正在使用FILL_PARENT作爲單獨的行項目的高度和寬度。相反,您應該使用fill_parent作爲寬度,並將wrap_content或某個dp值用作上述代碼中所示的高度。

我希望這將有助於:)

+0

不起作用:(http://ivm.neru9.com/pic/ 102a326d5f7c.png – 2012-08-13 12:38:16

+0

什麼是這個白色的盒子在圖像..... – karn 2012-08-13 12:45:11

+0

不重要:))。它的一個標誌:P – 2012-08-13 12:49:34