2012-04-04 182 views
0

我有一個相當簡單的佈局,圖像和一些textviews包裝在一個垂直的LinearLayout。每當我將此佈局的背景設置爲9patch時,佈局的高度看起來會設置爲9patch的高度(即使9patch似乎成功拉伸爲佈局的高度)。9patch造成奇怪的layout_height問題

如果我使用純色背景色,佈局的高度按預期工作。

這裏的到位與9patch截圖:

enter image description here

這裏有一個堅實的背景,而不是9patch截圖(注意文本是如何佈局的中心現在):

enter image description here

最後,這裏的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/show_gray" 
    android:gravity="center_vertical" 
    android:orientation="horizontal" android:weightSum="5"> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="4" 
     android:scaleType="centerCrop" 
     android:src="@drawable/temp_featured_banner" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#FF0000" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:paddingLeft="30dp" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Large Text" 
      android:textColor="@color/white" /> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Small Text" 
      android:textColor="@color/white" /> 
    </LinearLayout> 

</LinearLayout> 

...以防萬一,這裏的draw9patch的顯示我9patch繪製截圖:

enter image description here

我懷疑的LinearLayout不超過9patch的自然大小高(所以有沒有什麼可以拉伸的..),所以我把9patch的高度減少了50%。問題依然存在。

任何想法?

+0

您是否嘗試過設置九個補丁的垂直填充區域? – JRaymond 2012-04-04 16:56:12

+0

@JRaymond在圖像的左上角和左下角有兩個黑點。你是這個意思嗎? – NPike 2012-04-04 17:17:45

+0

否......九個補丁的頂部和左側指的是圖像的「可拉伸」區域。 9-patch的右側和底部定義了**內容**應該到達的位置 - 目前,您只是告訴它猜測(並且通過將所有內容放在由左上角點指定的行中來猜測) 。從我的圖片中可以看出,您可能需要從九個補丁右側的頂部到底部(當然沒有留下角落)的黑色實線,以及與您的圖像右上角黑點相匹配的點底部 – JRaymond 2012-04-04 17:31:51

回答

1

top和left九個補丁參考圖像的「可拉伸」的區域。 9-patch的右側和底部定義了內容應該去的地方 - 目前,你只是告訴它猜測(並通過將所有內容放在由左上角指定的行中來猜測)。從我的圖片中可以看出,您可能需要從九個補丁右側的頂部到底部(當然沒有留下角落)的黑色實線,以及與您的圖像右上角黑點相匹配的點底部。這告訴操作系統在哪裏放置內容與圖像的關係,以便它更好地知道如何拉伸圖像。