2014-02-26 51 views
2

我正在創建一個顯示通知號碼的Imagebutton。這裏是我的示例圖像:Android:如何將文本寫入圖像中的特定區域

Icon Image

我的目標是寫一個圖片中的數字(1,2 ......)的具體黑圈。我嘗試了RelativeLayout和FrameLayout,但不認爲這是實現結果的可靠方法。原因是邊緣,填充將根據不同的設備大小而有所不同。

任何建議開始?

+2

分離黑色圓圈&鉛筆背景圖像。使用FrameLayout將鉛筆背景上的黑色圓圈定位。此外,使用該圓圈作爲TextView的可繪製背景。 – JoelFernandes

+0

@nitesh不確定你的意思是什麼意思?這是在我的問題。 – Signcodeindie

+0

@Signcodeindie你的圖像在哪裏? :) – nitesh

回答

2

在這裏你去:

<FrameLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/pencil"> 

     <TextView 
      android:id="@+id/circle" 
      android:layout_width="22dp" 
      android:layout_height="22dp" 
      android:background="@drawable/circle" 
      android:gravity="center" 
      android:text="2" 
      android:layout_gravity="bottom|right" 
      android:layout_marginRight="8dp" 
      android:layout_marginBottom="8dp" 
      android:textColor="#FFFFFF" 
      android:textSize="12sp" /> 

    </FrameLayout> 

圖片:enter image description here enter image description here

輸出:

enter image description here

+0

謝謝!我仍然懷疑的一件事是你使用了8dp的邊際值來使它正確,在我的日食中,我使用了10的值來達到期望的結果。 android:layout_marginRight =「10dp」 android:layout_marginBottom =「10dp」 – Signcodeindie

+0

這可能是因爲圓形圖像的寬度和高度。但是,您可以在各種屏幕上測試它,看看它是否工作正常。 – JoelFernandes

+0

嘿喬爾,謝謝你真的很容易解決問題。你應該得到一個標記作爲答案。乾杯! – Signcodeindie

0

您可以製作一個自定義的View類,它從ImageView擴展而來。你必須重寫的onDraw方法做這樣的事情:

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    // Calculate x and y positions and set up a Paint instance with correct color/size 
    canvas.drawText(String.valueOf(myNumber), x, y, myPaint); 
} 
0

嘗試的TextView與bacground設定爲圖像

<TextView android:id="@+id/img" android:layout_width="45sp" android:layout_height="45sp" android:background="@drawable/circle" android:gravity="center" android:text="r" /> 使用重力對齊圖像中的文字。

0
<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="bottom|right" 
    android:paddingRight="20dp" 
    android:paddingBottom="15dp" 
    android:textColor="@android:color/white" 
    android:background="@drawable/text" 
    android:text="1" /> 
0
try this code  
Bitmap bitmap = ... // Load your bitmap here 
Canvas canvas = new Canvas(bitmap); 
Paint paint = new Paint(); 
paint.setColor(Color.BLACK); 
paint.setTextSize(10); 
canvas.drawText("Some Text here", x, y, paint); 
+0

如何在我的情況找到x,y? – Signcodeindie

+0

@Signcodeindie:請試試這個座標'11,16' –

相關問題