我通过以下代码动态添加表格行上的按钮,按钮。
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());
此日志显示正确的输出。我已经使用示例文本(测试)进行了追踪,然后添加了按钮(通过以下代码)
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 :(得分:1)
似乎你的布局问题发生了变化
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);
并且工作正常。