2013-06-18 18 views
0

我想在我的ListView adpater中設置一些複雜的佈局。如何在橢圓大小上只設置多個文字瀏覽

列表中的每個項目相結合的4 TextView

前三TextView基本上都是一句話,但我想第二TextView不被ellipsized,而第一和第三我不想被人ellipsized 。

最後的TextView需要放置在右側。

所以,如果所有的前三TextView不能完全顯示,佈局應該是這樣的:

`TextView1...` `TextView2` `TextView3...` `TextView4` 

我的代碼工作在這種情況下,但在簡單的情況下,在沒有ellipsize是必要的,它不,它看起來像這樣:

`TextView1`  `TextView2` `TextView3` `TextView4` 

這裏是我的代碼:

LinearLayout layout = new LinearLayout(m_context); 
layout.setGravity(Gravity.CENTER_VERTICAL); 
layout.setOrientation(LinearLayout.HORIZONTAL); 
layout.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 


LinearLayout textLayout = new LinearLayout(m_context); 
textLayout.setOrientation(LinearLayout.HORIZONTAL); 
textLayout.setGravity(Gravity.CENTER_VERTICAL); 

view.m_text1 = new TQTextView(m_context); 
view.m_text1.setSingleLine(); 
view.m_text1.setEllipsize(TruncateAt.END); 

view.m_text2 = new TQTextView(m_context); 

view.m_text3 = new TQTextView(m_context); 
view.m_text3.setSingleLine(); 
view.m_text3.setEllipsize(TruncateAt.END); 

view.m_text1 = new TQTextView(m_context); 

textLayout.setWeightSum(2f); 
textLayout.addView(view.m_text1, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f)); 
textLayout.addView(view.m_text2, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0f)); 
textLayout.addView(view.m_text3, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f)); 

layout.addView(textLayout, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f)); 


view.m_text4 = new TQTextView(m_context); 

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0f); 
int margin = 10; 
params.leftMargin = margin; 
params.gravity = Gravity.CENTER_VERTICAL|Gravity.RIGHT; 

layout.addView(view.m_text4, params); 
+0

呃,給句子加'...'? –

+0

在您的代碼中,'m_text4'不會添加到視圖中,而且'm_text2'會添加不同的佈局參數,原因不明顯 –

+0

爲什麼不在xml中創建此佈局並在代碼中使其膨脹? –

回答

0

我發現了一個解決方案:

我添加三個TextView無特殊LayoutParams這樣的:

textLayout.addView(view.m_attackeeName); 
textLayout.addView(view.m_text); 
textLayout.addView(view.m_attackerName); 

和我稱之爲getView函數結束下面的函數

private void updateTexts(View parent, ViewHolder view, Data data){ 
    view.m_text1.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    view.m_text3.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    view.m_text2.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    view.m_text4.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    int totalWidth = parent.getWidth() - parent.getPaddingLeft() - parent.getPaddingRight(); 
    totalWidth = totalWidth - view.m_text4.getMeasuredWidth() - view.m_text2.getMeasuredWidth() - m_margin; 

    int attackerTextWidth = view.m_text1.getMeasuredWidth(); 
    int attackeeTextWidth = view.m_text3.getMeasuredWidth(); 
    if(attackerTextWidth + attackeeTextWidth > totalWidth) 
    { 
     int spareWidth = totalWidth/2 - attackerTextWidth; 
     if(spareWidth > 0) 
     { 
      attackeeTextWidth = totalWidth/2 + spareWidth; 
     } 
     else 
     { 
      spareWidth = totalWidth/2 - attackeeTextWidth; 
      if(spareWidth > 0) 
      { 
       attackerTextWidth = totalWidth/2 + spareWidth; 
      } 
      else 
      { 
       attackerTextWidth = totalWidth/2; 
       attackeeTextWidth = totalWidth/2; 
      } 
     } 
    } 

    String text = TextUtils.ellipsize(data.GetText1(), view.m_text1.getPaint(), 
      attackerTextWidth, TextUtils.TruncateAt.END).toString(); 
    view.m_text1.setText(text); 
    text = TextUtils.ellipsize(data.GetText3(), view.m_text3.getPaint(), 
      attackeeTextWidth, TextUtils.TruncateAt.END).toString(); 
    view.m_text3.setText(text); 
} 
(設置文本後)
相關問題