0

プログラムで作成された「リスト」の構造:

ScrollView
->LinearLayout (itemRow)
-->LinearLayout (itemTitle)
--->TextView (title)
--->TextView (price)
--> (other layouts)...
... and start again with itemRow

タイトル TextView のテキストが正しく折り返されている場合、価格(無料)は表示されません。

ここに画像の説明を入力

コード:

    LayoutParams p=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT); 

    TextView title=new TextView(ctx);
    title.setText(e.getTitle());
    title.setTextAppearance(ctx, R.style.eList_Title);
    TextView price=new TextView(ctx);
    price.setPadding(2, 1, 1, 1);
    price.setSingleLine(true);
    if (e.getPrice().doubleValue()==0) {
        price.setText('('+getString(R.string.e_free)+')');
        price.setTextColor(colorGreenFree);
    } else {
        price.setText('('+NumberFormat.getCurrencyInstance().format(e.getPrice())+')');
        price.setTextColor(Color.RED);
    }
    LinearLayout llTitle=new LinearLayout(ctx);
    llTitle.setBackgroundResource(R.drawable.gradient_silver_red);
    llTitle.setLayoutParams(llpMatchParentWrapContent);
    llTitle.addView(title);
    llTitle.addView(price);

注: if price.setSingleLine(true); が削除され、次の結果が得られます。

ここに画像の説明を入力

PD: RelativeLayout を試してみましたが、同じ問題があります。

4

2 に答える 2

1

TextView が広すぎて、右から 2 番目を押し出しています。

これを実現する簡単な方法は、単一の TextView といくつかの HTML を使用して「無料」で色付けすることです。

textView.setText(Html.fromHtml(String.format("%s <font color="#00FF00">(%s)</font>", name, gratis)));
于 2013-04-12T08:57:06.177 に答える
0

答えてくれたアレックスに感謝します。最終的にこの方法で解決しました:

        Spannable titleSpan= null;
        String strPrice=null;
        if (e.getPrice().doubleValue()==0) {
            strPrice='('+getString(R.string.e_free)+')';
            titleSpan=new SpannableString(e.getTitle()+strPrice);
            titleSpan.setSpan(new ForegroundColorSpan(Color.GREEN), e.getTitle().length(), e.getTitle().length()+strPrice.length(), Spannable.SPAN_INTERMEDIATE);
        } else {
    strPrice='('+NumberFormat.getCurrencyInstance().format(e.getPrice())+')';
            titleSpan=new SpannableString(e.getTitle()+strPrice);
            titleSpan.setSpan(new ForegroundColorSpan(Color.RED), e.getTitle().length(), e.getTitle().length()+strPrice.length(), Spannable.SPAN_INTERMEDIATE);
        }
        title.setText(titleSpan);
于 2013-04-12T09:52:24.527 に答える