2012-02-02 120 views
4

我想將背景設置爲TextView,我希望TextView的大小爲背景和文本的大小。TextView背景拉伸

這裏是我的代碼:

<TextView 
     android:id="@+id/attackeNameTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="13dp" 
     android:paddingTop="8dp" 
     android:paddingLeft="35dp" 
     android:paddingRight="35dp" 
     android:singleLine="true" 
     android:ellipsize="end" 
     android:textSize="12sp" 
     android:text="thej hhekj hejk hjkesd ks dkl jsalkj dkl" 
     android:background="@drawable/attackee_name_background"/> 

的結果是,該文本的「填充盒子」就好內配合,但背景被拉伸。

我是否必須爲TextView分配一個固定的layout_width

+0

只是分配給背景屬性配合中心;) – JackTurky 2012-02-02 09:49:02

+2

您可以使用bacground圖像爲[9補丁(http://developer.android.com/guide/developing/tools/draw9patch.html) – 2012-02-02 09:49:45

+0

如何分配我財產的背景? – piojo 2012-02-02 09:50:05

回答

3

如果你想要的背景完全適合您的TextView大小,而不拉伸和文本適合裏面,你有要使用9-patch圖像,或者必須設置與圖像尺寸相同的固定尺寸。沒有其他解決方案。

也考慮到padding僅縮小文本框區域(背景不縮小),而margin s留下TextView框外的空間。

所以,如果背景圖像是(image_width, image_height),你要離開TextView內部填充不streching的背景和不使用9-patch圖像,你必須設定一個固定的大小TextView等於(image_width+paddingLeft+paddingRight, image_height+paddingTop+paddingBottom)

+0

+1如果提到邊距將調整背景的大小,而填充不會。 – ArtOfWarfare 2012-11-13 22:47:16

+0

我在drawable文件夾中使用* .9.png圖像。但不會摧毀9貼圖的製作時間。請建議...如何在Android項目中使用9-patch imge? – 2016-04-21 10:48:45

3

編輯

這裏是做你想要什麼樣的哈克的方式...

<FrameLayout 
     android:id="@+id/frameLayout1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="fitCenter" 
      android:src="@drawable/something" /> 
     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="TextView" /> 
    </FrameLayout> 
+0

'android:scaleType =「fitCenter」'不起作用。如果我製作9個補丁,是否需要爲它設置一個內容區域,對不對? – piojo 2012-02-02 09:56:07

+0

雅所以填充是正確和底部,然後比例是頂部和左側。 – Maurycy 2012-02-02 09:59:30

+0

我不希望它縮放,多數民衆贊成我試圖阻止。它根本不起作用 – piojo 2012-02-02 10:06:16