2011-08-02 20 views
2

我正在嘗試增加文本中兩個單詞之間的間距。爲此,我遇到了很多網站,但是我發現了下面一個有我需要的確切信息。在android中爲文本提供字距/跟蹤

http://developer.android.com/reference/java/awt/font/TextAttribute.html

比如我想使用如下constatnt

public static final TextAttribute TRACKING 

爲我的文字。我應該怎麼做?

在這裏,他們列出了我可以用於文本的所有textAttributes(常量)。但我不知道如何將這個用於我的文本。誰可以幫我這個事。

回答

0

我寫了這個功能:

INPUT

  • 的LinearLayout LL:

    佈局,這將是你的 「TexView」(確保它的方向是垂直的)

  • 字符串文本:

    文本

  • 語境mContext

    上下文

應該做什麼

我評論的部分,你必須編輯


/* 
* Copyright 2011 Sherif 
*/ 

private void populateText(LinearLayout ll, String text , Context mContext) { 
    String [] textArray = text.split(" "); 
    Display display = getWindowManager().getDefaultDisplay(); 
    ll.removeAllViews(); 
    int maxWidth = display.getWidth() - 20; 

    LinearLayout.LayoutParams params; // to be used over and over 
    LinearLayout newLL = new LinearLayout(mContext); 
    newLL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
      LayoutParams.WRAP_CONTENT)); 
    newLL.setGravity(Gravity.LEFT); 
    newLL.setOrientation(LinearLayout.HORIZONTAL); 

    int widthSoFar = 0; 

    for (int i = 0 ; i < textArray.length ; i++){ 
     LL = new LinearLayout(mContext); 
     LL.setOrientation(LinearLayout.HORIZONTAL); 
     LL.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM); 
     LL.setLayoutParams(new ListView.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

     TV = new TextView(mContext); 
     TV.setText(textArray[i]); 
     //TV.setTextSize(size); <<<< SET TEXT SIZE 
     TV.measure(0, 0); 
     params = new LinearLayout.LayoutParams(TV.getMeasuredWidth(), 
       LayoutParams.WRAP_CONTENT); 
     //params.setMargins(5, 0, 5, 0); // YOU CAN USE THIS 
     LL.addView(TV, params); 
     LL.measure(0, 0); 
     widthSoFar += TV.getMeasuredWidth();// YOU MAY NEED TO ADD THE MARGINS 
     if (widthSoFar >= maxWidth) { 
      ll.addView(newLL); 

      newLL = new LinearLayout(mContext); 
      newLL.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT)); 
      newLL.setOrientation(LinearLayout.HORIZONTAL); 
      newLL.setGravity(Gravity.LEFT); 
      params = new LinearLayout.LayoutParams(LL 
        .getMeasuredWidth(), LL.getMeasuredHeight()); 
      newLL.addView(LL, params); 
      widthSoFar = LL.getMeasuredWidth(); 
     } else { 
      newLL.addView(LL); 
     } 
    } 
    ll.addView(newLL); 
} 

注意

我沒有測試它...我用了更復雜的事情,它正在工作

+0

但這是我可以利用的唯一方法。 –

+0

我會看看你是否可以使用TRACKING ...但是你確定TRACKING負責間距嗎? –

+0

是的。我已經看到一些圖像重調字距和跟蹤。而且我很確定 –