如何仅使用ellipsize设置多个textviews

时间:2013-06-18 15:54:36

标签: android layout

我想在ListView adpater中制作一些复杂的布局。

列表中的每个项目合并为4 TextView

前三个TextView基本上只有一个句子,但是我希望第二个TextView不被椭圆化,第一个和第三个我想要进行椭圆化。

最后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);

1 个答案:

答案 0 :(得分: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);
}