2013-04-12 1 views
0

Структура «список» из программно:Android (Programmatic) раскладка не показывая 2-ой TextView, когда упаковка 1rst TextView

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

Когда текст название TextView правильно завернуты, цена (бесплатно) является не показано.

enter image description here

Код:

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

Примечание: если price.setSingleLine (истина); удаляется я получаю этот результат:

enter image description here

P.D .: Я попытался с RelativeLayout, и имеют те же проблемы.

+0

Почему бы вам не попробовать с Listview путем реализации пользовательского макета для ListView. Как это http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html – GrIsHu

ответ

1

TextView слишком широк и нажимает второй справа.

Простым способом добиться этого может быть использование одного TextView и некоторого HTML для цветного изображения «бесплатно».

textView.setText(Html.fromHtml(String.format("%s <font color="#00FF00">(%s)</font>", name, gratis))); 
+0

Использование этого не работает: strTitle = Html.fromHtml (" ("+ NumberFormat.getCurrencyInstance(). Format (event.getPrice()) +") "); – surfealokesea

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); 
Смежные вопросы