0

我想在我的ListView适配器中制作一个有点复杂的布局。

列表中的每个项目由 4 个组合而成TextView

前三个TextView基本上是一个句子,但我希望第二个TextView不要被省略,第一个和第三个我确实想要被省略。

最后一个TextView需要定位在右侧。

所以如果前三个TextView不能完全显示,布局应该是这样的:

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

我的代码在这种情况下工作,但在简单的情况下,当不需要椭圆大小时,它不需要,它看起来像这样:

`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);
4

1 回答 1

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);
}
于 2013-06-19T16:14:28.067 回答