Android:AddView to LinearLayout不可见

时间:2014-08-19 13:27:26

标签: android android-layout

我为翻​​译程序创建了一个包含 LinearLayout 的简单 Scrollview 。使用按钮,用户应该能够查看给定单词的一系列翻译 - 这一切都按照我的希望工作(可能不是最有效的编码,但是嘿?)但是当使用一系列填充LinearLayout时添加视图我无法让它们显示在屏幕上 - 我进行了一些研究并经历了无效,postinvalidate,updateviewlayout和refreshdrawablestate的各种尝试,而没有在布局中出现任何新视图。我唯一的成功来自于setcontentview。

问题是 setcontentview基本上重绘了所有布局,因此擦除了我正在使用的autocompletetextview的所有按钮侦听器和下拉适配器。我的程序变成了一个带有可滚动翻译单词列表的shell

我想要做的是将addview合并,然后变得可见,而不会丢失应用程序的其他程序设计工作。它基本上应该保持功能,但我却失去了如何做到这一点。我上面尝试过的所有解决方案都没有显示添加视图。

我的OnClickListener代码,瑕疵和所有:

   private OnClickListener TranslatorListener = new OnClickListener() 
       {
        @Override
          public void onClick(View v) 
          {
              int wordpos;
              View view = getLayoutInflater().inflate(R.layout.activity_scubatranslator, null, false);
              ScrollView sb = (ScrollView) view.findViewById(R.id.Scroll_Box);
              sb.setBackgroundColor(getResources().getColor(R.color.black_transparent_70));
              LinearLayout scroll_list = (LinearLayout) view.findViewById(R.id.Scroller);
              scroll_list.removeAllViews();

              if (current_word == ""){
                  AutoCompleteTextView source = (AutoCompleteTextView) findViewById(R.id.Translator_Search_Box);
                  current_word = source.getText().toString();
              }

              if (Arrays.asList(current_list).contains(current_word)){

                  wordpos = Arrays.asList(current_list).indexOf(current_word);

                  TextView CheckText = (TextView)findViewById(R.id.CheckBox);
                  CheckText.setText(current_lang + " / " + wordpos + " / " + tracker);

                  LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
                  llp.setMargins(0, 2, 0, 0);

                  //for (int lc = 1; lc <=lang_count; ++lc){
                  for (int lc = 1; lc <= 19; ++lc){
                      if (tracker == lc){
                      }  
                      else {
                          switch (lc) {
                            case 1: TextView ChineseView = new TextView(getApplicationContext());
                                    ChineseView.setText("  " + ChineseList[wordpos]);
                                    ChineseView.setGravity(Gravity.CENTER_VERTICAL);
                                    ChineseView.setBackgroundColor(getResources().getColor(R.color.black));
                                    ChineseView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_1, 0, 0 , 0 );
                                    ChineseView.setTypeface(null, Typeface.BOLD);
                                    ChineseView.setLayoutParams(llp);
                                    scroll_list.addView(ChineseView);
                                    break;
                            case 2: TextView DanishView = new TextView(getApplicationContext());
                                    DanishView.setText("  " + DanishList[wordpos]);
                                    DanishView.setGravity(Gravity.CENTER_VERTICAL);
                                    DanishView.setBackgroundColor(getResources().getColor(R.color.black));
                                    DanishView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_2, 0, 0 , 0 );
                                    DanishView.setTypeface(null, Typeface.BOLD);
                                    DanishView.setLayoutParams(llp);
                                    scroll_list.addView(DanishView);
                                    break;
                            case 3: TextView DutchView = new TextView(getApplicationContext());
                                    DutchView.setText("  " + DutchList[wordpos]);
                                    DutchView.setGravity(Gravity.CENTER_VERTICAL);
                                    DutchView.setBackgroundColor(getResources().getColor(R.color.black));
                                    DutchView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_3, 0, 0 , 0 );
                                    DutchView.setTypeface(null, Typeface.BOLD);
                                    DutchView.setLayoutParams(llp);
                                    scroll_list.addView(DutchView);
                                    break;
                            case 4: TextView EnglishView = new TextView(getApplicationContext());
                                    EnglishView.setText("  " + EnglishList[wordpos]);
                                    EnglishView.setGravity(Gravity.CENTER_VERTICAL);
                                    EnglishView.setBackgroundColor(getResources().getColor(R.color.black));
                                    EnglishView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_4, 0, 0 , 0 );
                                    EnglishView.setTypeface(null, Typeface.BOLD);
                                    EnglishView.setLayoutParams(llp);
                                    scroll_list.addView(EnglishView);
                                    break;
                            case 5: TextView FilipinoView = new TextView(getApplicationContext());
                                    FilipinoView.setText("  " + FilipinoList[wordpos]);
                                    FilipinoView.setGravity(Gravity.CENTER_VERTICAL);
                                    FilipinoView.setBackgroundColor(getResources().getColor(R.color.black));
                                    FilipinoView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_5, 0, 0 , 0 );
                                    FilipinoView.setTypeface(null, Typeface.BOLD);
                                    FilipinoView.setLayoutParams(llp);
                                    scroll_list.addView(FilipinoView);
                                    break;
                            case 6: TextView FinnishView = new TextView(getApplicationContext());
                                    FinnishView.setText("  " + DanishList[wordpos]);
                                    FinnishView.setGravity(Gravity.CENTER_VERTICAL);
                                    FinnishView.setBackgroundColor(getResources().getColor(R.color.black));
                                    FinnishView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_6, 0, 0 , 0 );
                                    FinnishView.setTypeface(null, Typeface.BOLD);
                                    FinnishView.setLayoutParams(llp);
                                    scroll_list.addView(FinnishView);
                                    break;
                            case 7: TextView FrenchView = new TextView(getApplicationContext());
                                    FrenchView.setText("  " + DanishList[wordpos]);
                                    FrenchView.setGravity(Gravity.CENTER_VERTICAL);
                                    FrenchView.setBackgroundColor(getResources().getColor(R.color.black));
                                    FrenchView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_7, 0, 0 , 0 );
                                    FrenchView.setTypeface(null, Typeface.BOLD);
                                    FrenchView.setLayoutParams(llp);
                                    scroll_list.addView(FrenchView);
                                    break;
                            case 8: TextView GermanView = new TextView(getApplicationContext());
                                    GermanView.setText("  " + DanishList[wordpos]);
                                    GermanView.setGravity(Gravity.CENTER_VERTICAL);
                                    GermanView.setBackgroundColor(getResources().getColor(R.color.black));
                                    GermanView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_8, 0, 0 , 0 );
                                    GermanView.setTypeface(null, Typeface.BOLD);
                                    GermanView.setLayoutParams(llp);
                                    scroll_list.addView(GermanView);
                                    break;
                            case 9: TextView ItalianView = new TextView(getApplicationContext());
                                    ItalianView.setText("  " + DanishList[wordpos]);
                                    ItalianView.setGravity(Gravity.CENTER_VERTICAL);
                                    ItalianView.setBackgroundColor(getResources().getColor(R.color.black));
                                    ItalianView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_9, 0, 0 , 0 );
                                    ItalianView.setTypeface(null, Typeface.BOLD);
                                    ItalianView.setLayoutParams(llp);
                                    scroll_list.addView(ItalianView);
                                    break;
                            case 10: TextView JapaneseView = new TextView(getApplicationContext());
                                    JapaneseView.setText("  " + DanishList[wordpos]);
                                    JapaneseView.setGravity(Gravity.CENTER_VERTICAL);
                                    JapaneseView.setBackgroundColor(getResources().getColor(R.color.black));
                                    JapaneseView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_10, 0, 0 , 0 );
                                    JapaneseView.setTypeface(null, Typeface.BOLD);
                                    JapaneseView.setLayoutParams(llp);
                                    scroll_list.addView(JapaneseView);
                                    break;
                            case 11: TextView KoreanView = new TextView(getApplicationContext());
                                    KoreanView.setText("  " + DanishList[wordpos]);
                                    KoreanView.setGravity(Gravity.CENTER_VERTICAL);
                                    KoreanView.setBackgroundColor(getResources().getColor(R.color.black));
                                    KoreanView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_11, 0, 0 , 0 );
                                    KoreanView.setTypeface(null, Typeface.BOLD);
                                    KoreanView.setLayoutParams(llp);
                                    scroll_list.addView(KoreanView);
                                    break;
                            case 12: TextView NorwegianView = new TextView(getApplicationContext());
                                    NorwegianView.setText("  " + DanishList[wordpos]);
                                    NorwegianView.setGravity(Gravity.CENTER_VERTICAL);
                                    NorwegianView.setBackgroundColor(getResources().getColor(R.color.black));
                                    NorwegianView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_12, 0, 0 , 0 );
                                    NorwegianView.setTypeface(null, Typeface.BOLD);
                                    NorwegianView.setLayoutParams(llp);
                                    scroll_list.addView(NorwegianView);
                                    break;
                            case 13: TextView PolishView = new TextView(getApplicationContext());
                                    PolishView.setText("  " + DanishList[wordpos]);
                                    PolishView.setGravity(Gravity.CENTER_VERTICAL);
                                    PolishView.setBackgroundColor(getResources().getColor(R.color.black));
                                    PolishView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_13, 0, 0 , 0 );
                                    PolishView.setTypeface(null, Typeface.BOLD);
                                    PolishView.setLayoutParams(llp);
                                    scroll_list.addView(PolishView);
                                    break;
                            case 14: TextView PortugeseView = new TextView(getApplicationContext());
                                    PortugeseView.setText("  " + DanishList[wordpos]);
                                    PortugeseView.setGravity(Gravity.CENTER_VERTICAL);
                                    PortugeseView.setBackgroundColor(getResources().getColor(R.color.black));
                                    PortugeseView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_14, 0, 0 , 0 );
                                    PortugeseView.setTypeface(null, Typeface.BOLD);
                                    PortugeseView.setLayoutParams(llp);
                                    scroll_list.addView(PortugeseView);
                                    break;
                            case 15: TextView RussianView = new TextView(getApplicationContext());
                                    RussianView.setText("  " + DanishList[wordpos]);
                                    RussianView.setGravity(Gravity.CENTER_VERTICAL);
                                    RussianView.setBackgroundColor(getResources().getColor(R.color.black));
                                    RussianView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_15, 0, 0 , 0 );
                                    RussianView.setTypeface(null, Typeface.BOLD);
                                    RussianView.setLayoutParams(llp);
                                    scroll_list.addView(RussianView);
                                    break;
                            case 16: TextView SpanishView = new TextView(getApplicationContext());
                                    SpanishView.setText("  " + DanishList[wordpos]);
                                    SpanishView.setGravity(Gravity.CENTER_VERTICAL);
                                    SpanishView.setBackgroundColor(getResources().getColor(R.color.black));
                                    SpanishView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_16, 0, 0 , 0 );
                                    SpanishView.setTypeface(null, Typeface.BOLD);
                                    SpanishView.setLayoutParams(llp);
                                    scroll_list.addView(SpanishView);
                                    break;
                            case 17: TextView SwedishView = new TextView(getApplicationContext());
                                    SwedishView.setText("  " + DanishList[wordpos]);
                                    SwedishView.setGravity(Gravity.CENTER_VERTICAL);
                                    SwedishView.setBackgroundColor(getResources().getColor(R.color.black));
                                    SwedishView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_17, 0, 0 , 0 );
                                    SwedishView.setTypeface(null, Typeface.BOLD);
                                    SwedishView.setLayoutParams(llp);
                                    scroll_list.addView(SwedishView);
                                    break;
                            case 18: TextView ThaiView = new TextView(getApplicationContext());
                                    ThaiView.setText("  " + DanishList[wordpos]);
                                    ThaiView.setGravity(Gravity.CENTER_VERTICAL);
                                    ThaiView.setBackgroundColor(getResources().getColor(R.color.black));
                                    ThaiView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_18, 0, 0 , 0 );
                                    ThaiView.setTypeface(null, Typeface.BOLD);
                                    ThaiView.setLayoutParams(llp);
                                    scroll_list.addView(ThaiView);
                                    break;
                            case 19: TextView TurkishView = new TextView(getApplicationContext());
                                    TurkishView.setText("  " + DanishList[wordpos]);
                                    TurkishView.setGravity(Gravity.CENTER_VERTICAL);
                                    TurkishView.setBackgroundColor(getResources().getColor(R.color.black));
                                    TurkishView.setCompoundDrawablesWithIntrinsicBounds( R.drawable.lang_icon_19, 0, 0 , 0 );
                                    TurkishView.setTypeface(null, Typeface.BOLD);
                                    TurkishView.setLayoutParams(llp);
                                    scroll_list.addView(TurkishView);
                                    break;
                       default: throw new IllegalArgumentException("oops?");
                       }
                     }
                  }     

                  // ScrollView sb = (ScrollView)findViewById(R.id.Scroll_Box);
                  // sb.updateViewLayout(scroll_list); these did not show the addviews

              }  
              else {

                  int duration = Toast.LENGTH_LONG;
                  String message = getResources().getString(R.string.Trans_Warning);
                  Toast toast = Toast.makeText(context, message, duration);
                  toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                  View toastview = toast.getView();
                  toastview.setBackgroundColor(getResources().getColor(R.color.black_transparent_70));
                  toast.show();
              }

              current_word = "";
              // ViewGroup vg = (ViewGroup) findViewById (R.id.Scroller);
              // vg.invalidate(); 
              // vg.refreshDrawableState ();  these did not show the addviews
              setContentView(view);
          }
       };

感谢您提出任何建议 - 但请轻松我是一名新手程序员 - 随时学习......

1 个答案:

答案 0 :(得分:0)

尝试使用类似的内容初始化TextView

TextView text = (TextView) getLayoutInflater().inflate(
                R.layout.include_textview, container, false);

container是您要填充的LinearLayout