2014-01-13 90 views
9

在我的Android應用程序中,我有一個顯示包含特殊字符的文本的文本視圖。 TextView以某種方式自動在字符'/'和' - '處斷開字符串。在TextView中防止換行

例如,字符串「AAAAAAA/BBB-CCCCC/DDD」被顯示作爲

aaaaaaa/ 
bbb- 
ccccc/ 
ddd 

然而,我想沒有任何換行符來顯示它,除了一個在視圖,即,邊界,像這樣:

aaaaaaa/bb 
bb-ccccc/d 
dd 

是否有任何方法來停用自動換行符或轉義這些字符?我已經嘗試用\ uFEFF逃脫,但沒有成功。

+0

嘗試使用\之前每個轉義字符即BBB \ -ccccc – Saqib

+0

也許這篇文章將幫助你:http://stackoverflow.com/questions/6134457/how-to-prevent-edittext-from-breaking-a-line-after-punctuation –

+0

'\'被稱爲轉義字符..因此,無論您想要轉義字符(例如aaaaaaa \/bb),都要使用此字符。在這種情況下,您正在轉義/放置\之前。將相同的規則應用到其他地方 –

回答

1

你大概可以使用Lines屬性或它的反部分方法setLines(int)

+0

設置的線似乎工作,但我如何計算動態文本所需的線數量?我試過getLineCount(),但是這會返回0,直到渲染視圖。我試圖用textSize計算所需行的數量,但直到我知道TextView的寬度並且寬度爲0之前我無法完全計算它,直到呈現爲止......似乎我正在進行循環。 – user3190568

+0

你不能得到任何視圖的寬度,直到它被創建。如果您正在進行一項活動,請在OnPostCreate中進行寬度計算。你可以在那裏得到它的寬度。 @ user3190568 –

0

我已經測試了下面的代碼。你甚至可以將它轉換成一個函數:

String specialString = "a/b/-c/d-d"; 
    String[] specialArray = specialString.split("/"); 
    String str = ""; 
    for(int i = 0; i < specialArray.length - 1; i++){ 
     str = str + specialArray[i] + Character.toString((char) 47); 
    } 
    str = str + specialArray[specialArray.length - 1]; 
    specialArray = str.split("-"); 
    str = ""; 
    for(int i = 0; i < specialArray.length - 1; i++){ 
     str = str + specialArray[i] + Character.toString((char) 45); 
    } 
    str = str + specialArray[specialArray.length - 1]; 
    textView.setText(str); 

現在的文本沒有逃脫

-1

你可以試試這個:

"aaaaaaa"+"/"+"bbb-ccccc"+"/"+"ddd" 
+0

絕對不行。編譯器連接字符串。 – hgoebl

5

保持你的TextView屬性

android:layout_width="wrap_content" 
android:layout_height="wrap_content" 

定義您在string.xml中的字符串

<string name="Username"> aaaaaaa\/bb\nbb\-ccccc\/d\ndd</string> 
+1

我認爲OP正在尋找一個textView寬度不變的解決方案。你的回答沒有解釋這一點 – suku

2

也許這是一個解決方案:https://stackoverflow.com/a/22337074/3472905

提到我加斜槓:

public class WordBreakTransformationMethod extends ReplacementTransformationMethod { 
    private static WordBreakTransformationMethod instance; 

    private WordBreakTransformationMethod() {} 

    public static WordBreakTransformationMethod getInstance() { 
     if (instance == null) { 
      instance = new WordBreakTransformationMethod(); 
     } 

     return instance; 
    } 

    private static char[] dash = new char[]{'-', '\u2011'}; 
    private static char[] space = new char[]{' ', '\u00A0'}; 
    private static char[] slash = new char[]{'/', '\u2215'}; 

    private static char[] original = new char[]{dash[0], space[0], slash[0]}; 
    private static char[] replacement = new char[]{dash[1], space[1], slash[1]}; 

    @Override 
    protected char[] getOriginal() { 
     return original; 
    } 

    @Override 
    protected char[] getReplacement() { 
     return replacement; 
    } 
} 
1

有沒有現成的解決方案,並沒有所謂的「包裝用字母文字的TextView」唯一的方法就是擴展TextView並修改Paint的breakText(String text,boolean measureForward,float maxWidth,float [] measuredWidth)函數。

此外,你可以算出像素TextView的大小,計算出一個字母的寬度以像素爲單位,然後找字母(X),將適合在一個行,然後經過每個X字母插入換行符號

0

你可以計算文本的大小是這樣的:

String text = "This is my text"; 
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
textPaint.setTextSize(14.0f); 
Rect bounds = new Rect(); 
textPaint.getTextBounds(text, 0, text.length(), bounds); 

bounds.width() // width in pixels 
bounds.height() // height in pixels 

基於這些值,你可以分割成幾部分的文本,插入換行符。

0

它是Android的一個新生事物6.

嘗試添加以下內容到TextView的XML佈局

android:hyphenationFrequency="none"