2013-12-17 87 views
0

我有一個textview,我已經設置了它的圖像背景。該圖像沒有調整文本的大小。TextView背景圖像不能調整文本大小

例如: - 如果我的文本是「A」,背景仍然是相同的,不能調整大小以適應文本。這裏是TextView中的XML: -

<TextView 
    android:id="@+id/navmenusection_right_label_action" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="10dp" 
    android:background="@drawable/textview_round_background" 
    android:ellipsize="end" 
    android:gravity="center" 
    android:singleLine="true" 
    android:text="A" 
    android:textColor="#fff" 
    android:textSize="13sp" 
    android:textStyle="bold"/> 

enter image description here

+1

爲什麼不使用9Patch圖形?這意味着可調整大小視圖的背景,請記住,9個補丁不會越來越小,因此只能繪製最小大小http://developer.android.com/tools/help/draw9patch.html –

+0

您正在使用9Patch?如果是的話,你還設置了圖像的填充線嗎?因爲這可能是你的問題.. – Cata

+0

背景將會伸展但不縮水。使用最小尺寸的9patch。或者如果你不想去9patch去尋找更小版本的背景。 – dmSherazi

回答

1

現在我真的能理解你的問題。 Android不縮小圖像背景。在你的例子中,背景是寬度賦予者。

爲了您的目的,只需繪製一個9補丁圖像,您可以在其中聲明拉伸的區域(嘗試僅標記未舍入的區域以獲得更好的結果)以及應顯示內容的位置。

谷歌有一個相當不錯的DOC在那裏: Documentation

記住,9個圖像只拉伸並沒有得到縮小的,所以我將只使用一個最小寬度圖像。只要做到這一點像這樣,使之成爲9Patch

PIC EXAMPLE

+0

你是如何創建這個圖像的? 。我用你的形象,並得到了這個結果。它沒有正確調整大小。它呈橢圓形。 http://postimg.org/image/rkdwlqq8x/。其實我不知道如何創建一個九個補丁圖像 –

+0

所以請看看文檔,上面的鏈接,android sdk附帶一個9Patch繪圖軟件 –

+0

@Rahul Gupta,你解決了你的問題嗎? –

0

使用9修補圖像或創建此類型

創建文件夾繪製的bg.xml自己的XML佈局ANS設置爲文本背景

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 

    <item android:left="1dp" android:right="1dp" android:top="1dp" android:bottom="1dp"> 
     <shape> 
      <solid android:color="#000000" /> 
      <stroke 
       android:width="1.2dp" 
       android:color="#000000" /> 
      <corners android:radius="15dp" /> 
     </shape> 
    </item> 

</layer-list> 
+0

這贏得了幫助。我努力了。看到上面的圖片。看到圓角的效果,它不是一個平面圖像,我不知道如何創建一個9補丁圖像。你可以創建它嗎?請張貼它。謝謝 –