2017-03-03 88 views
0

我一直在試圖建立一個可擴展的TextView與5個系開始了(如果最初文本有超過5行)。TextView.SetMaxLines填充文本n行

我到目前爲止的代碼如下:

TextView 
    textView; 
Button 
    button; 

textView.LayoutChange += delegate { 
    if(textView.LineCount > 5) { 
     button.Visibility = ViewStates.Visible; 
     textView.SetMaxLines(5); 

     button.Click += delegate { 
      button.Visibility = ViewStates.Gone; 
      textView.SetMaxLines(Int32.MaxValue); 
     }; 
    } 
}; 

textView.Text = "Text that may occupy more than 5 lines due to size."; 

基本上,我有一個TextView其中I添加delegateLayoutChange事件。之後,我將初始文本設置爲textView。到現在爲止還挺好。如果文本跨越行數限制,則在設置文本後調用delegate,並將textViewMaxLines設置爲5

的問題是,當用戶點擊button,所述textViewMaxLines被(到目前爲止,那麼好)設定爲Int32.MaxValue但文本是填充 5行從頂部,切割的文本。

我試圖將MaxLines設置爲0和Int32.MaxValue和/或將文本設置爲"",然後再次設置爲所需文本,但沒有結果。

任何有關發生了什麼事情的想法,或者我在這裏做錯了什麼?


編輯添加圖片說明發生了什麼。文本似乎填充了文本最初佔用的空間的相同數量,並設置了MaxLines

左邊圖像是textViewMaxLines與設置爲5,右側一個是textViewMaxLines與設置爲Int32.MaxValue

回答

0

好吧......我可能已經發現一個的解決方案 - 不解決 - 針對此問題。它甚至可能是Xamarin上的錯誤 - 或者Android本身。

無論哪種方式,我發現TextView.SetMaxLines(Int32)不喜歡突然的變化。所以,爲了使得這個工作沒有TextView從無處獲得填充,我必須設置一個while,在將值傳遞給方法之前,將值遞增1。

最終的結果看起來是這樣的:

TextView 
    textView = ...; 
Button 
    buttonView = ...; 
Int32 
    lineCount = 0, 
    maxLines = 5; 

textView.LayoutChange += delegate { 
    if(textView.LineCount <= maxLines) { 
     return; 
    } 

    lineCount = textView.LineCount; 

    buttonView.Visibility = ViewStates.Visible; 
    textView.SetMaxLines(maxLines); 

    buttonView.Click += delegate { 
     buttonView.Visibility = ViewStates.Gone; 

     while(maxLines++ <= lineCount) { 
      textView.SetMaxLines(maxLines); 
     } 
    }; 
};