2014-03-12 185 views
3

我想顯示這樣的文字:避免音節

"For more infomration please visit www.my-site.com" 

對於一些分辨率/屏幕上的文字顯示爲:

|For more information please visit www.my- | 
|site.com         | 

我能避免對這種影響網址部分?

+1

您可以發佈您的代碼/佈局? – ElectronicGeek

+0

如果您可以接受兩行文字,請在網址前加上一個換行符('\ n')。 –

+1

容器的類型是什麼?即TextView,Button或其他東西? –

回答

1

使用non-breaking hyphen代替常規-的。

在XML:‑

在Java:"\u2011"

當然,如果你有可點擊的鏈接,不要在URL只是鏈接文本替換它。

例子:

<TextView android:layout_width="60dp" android:layout_height="wrap_content" 
    android:text="foo foo-foo foo&#x2011;foo"/> 

圖形佈局:

enter image description here

+0

簡單,工作。謝謝。 –

1

如果此文本出現在標準的Activity或Fragment中,可以將其分成兩個TextView,第一個包含信息文本,第二個包含網站URL並設置屬性android:singleLine="true"以防止文本被包裝。

接下來,你可以把這兩TextViews並排側定製「的FlowLayout」裏面,它會顯示他們在一個行,如果可能的:

|For more information please visit www.my-site.com | 
|             | 

或包裹在TextView的界限:

|For more information please visit  | 
|www.my-site.com       | 

不幸的是,在Android中沒有原生的FlowLayout。您可以編寫自己的代碼,調整像RelativeLayout這樣的東西,並編寫自定義方法來測量屏幕寬度和子視圖(有關如何完成此操作的示例,請參閱How to write Android Autowrap Layout using RelativeLayoutLinearLayout Horizontal with wrapping children)。

或者,您也可以使用幾個這樣的佈局已經適用的:

+0

我不想使用android:singleLine =「true」或換行符。 –

+0

@ Seraphim'shost你的意思是沒有換行符?你想把它全部放在一條線上嗎?在這種情況下,您可以將其放置在ScrollView中,並允許文本從屏幕上消失,直到用戶滾動屏幕。 – savanto

+0

我不想使用\ n在url之前打破這一行 –

1

如果你的項目需要小部件不允許使用換行符或自定義「FlowLayout」類型的佈局,您可以通過使用自定義方法擴展TextView來應用自定義換行包裝方案來創建自定義視圖。

// String text must contain a portion between 
// <unwrappable></unwrappable> tags. 
public void setCustomWrappedText(String text) { 
    final String OPEN_TAG = "<unwrappable>"; 
    final String CLOSE_TAG = "</unwrappable>"; 

    // Unwrappable string not yet set, find it, between <unwrappable></unwrappable> 
    // tags, strip out the tags, and set prefix and suffix. 
    int index = text.indexOf(OPEN_TAG); 
    int index2 = text.indexOf(CLOSE_TAG); 
    String prefix = text.substring(0, index); 
    String unwrappable = text.substring(index + OPEN_TAG.length(), index2); 
    String suffix = text.substring(index2 + CLOSE_TAG.length()); 

    // Contents already fit on one line, do nothing. 
    this.setText(prefix + unwrappable + suffix); 
    int lines = this.getLineCount(); 
    if (lines < 2) 
     return; 

    // Set content prefix, ie. text _before_ unwrappable appears, and count lines. 
    this.setText(prefix); 
    lines = this.getLineCount(); 

    // Set content to prefix _with_ unwrappable (no suffix), and count lines. 
    this.setText(prefix + unwrappable); 

    if (this.getLineCount() > lines) // Text has wrapped inside unwrappable, insert a line break to prevent. 
     this.setText(prefix + "\n" + this.unwrappable + suffix); 
    else // Text may or may not wrap _after_ unwrappable, we don't care. 
     this.setText(prefix + this.unwrappable + suffix); 
} 
+0

一個解決方案,使用標記來指定一個子文本是unwrappable將是完美的。如下所示:.setText(「這是可打包的文本和這不是一個可包裝的文本」) –

+0

@ Seraphim'shost請看看,我增加了在'標籤之間使用文本的功能。 – savanto

+0

@ Seraphim'shost我明白了。您只想以編程方式設置文本。我爲你做了一個自定義的方法,它會完成'onLayout'用來做的事情,看一看。 – savanto