2017-10-25 83 views
-1

ボタンを追加しています。動的に作成されたボタンが表示されない

tableLayout = (TableLayout) view.findViewById(R.id.tl); 
       Toast.makeText(getActivity(), "option size " + optionList.size(), Toast.LENGTH_SHORT).show(); 
       while (i < optionList.size()) { 

        tableRow = new TableRow(getActivity()); 
        tableLayout.addView(tableRow); 

        option = optionList.get(i); 
        Log.d("TAG", option.getPaymentReason()); 
        button = new Button(getActivity()); 
        button.setId(option.getId());//set id 
        button.setText(option.getPaymentReason());//set button title 
        button.setBackgroundColor(Color.parseColor(option.getBgColor()));//set btn background 
        button.setTextColor(Color.parseColor(option.getTextColor()));//set text color 
        button.setLayoutParams(params); 


        tableRow.addView(button); 

        i++; 
       } 

but buttons are not creating. 


Log.d("TAG", option.getPaymentReason()); 

このログは正しいoutput.Iを示しているが、サンプルテキスト(テスト)でtraiedてきた、それは(コードに従うことによって)

tableLayout = (TableLayout) view.findViewById(R.id.tl); 
     Toast.makeText(getActivity(), "option size " + optionList.size(), Toast.LENGTH_SHORT).show(); 
     while (i < optionList.size()) { 
tableRow = new TableRow(getActivity()); 
      tableLayout.addView(tableRow); 

      option = optionList.get(i); 
      Log.d("TAG", option.getPaymentReason()); 
      button = new Button(getActivity()); 
      button.setId(option.getId());//set id 
      button.setText("test");//set button title 
      button.setBackgroundColor(Color.parseColor(option.getBgColor()));//set btn background 
      button.setTextColor(Color.parseColor(option.getTextColor()));//set text color 
      button.setLayoutParams(params); 


      tableRow.addView(button); 

      i++; 
     } 

をボタンを追加することがあります

+0

あなたのparamsを投稿してください。どのLayoutParamsを使ってmake paramsオブジェクトに使用するか – Munir

+0

params = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT、ViewGroup.LayoutParams.WRAP_CONTENT); – ki123

答えて

1

を助けてください、あなたのlayoutparamsです問題は

0に

params = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 

を変更します

TableRow.LayoutParams params = new TableRow.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT); 

とうまく動作します。

関連する問題