我正在尝试将一些按钮加载到片段内的表格布局中。这怎么不出现。我看到更改背景有效(在 onCreateView(......) 中从灰色更改为红色)但是当我调用 loadAllButtons 时,它们不会出现。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if(container == null)
return null;
basedView = (TableLayout) inflater.inflate(R.layout.fragment_difficult_sound_syllable, container, false);
currentSyllable = getArguments().getString(StaticValues.DIFFICULT_SOUND_SELECTOR);
basedView.setBackgroundColor(Color.RED);
return basedView;
}
下面是我的 loadAllButtons,在有人问是之前,它说所有内容都已加载(例如添加的按钮和行,在 row.add() 和 basedView.add() 之后通过 logcat 检查)
private void loadAllButtons()
{
DataBaseHelper tmpHelper = new DataBaseHelper(getActivity().getApplicationContext());
ArrayList<ButtonInfoContainer> tmpArray = null;
if(currentSyllable.contentEquals(StaticValues.CONSONANT))
{
tmpArray = tmpHelper.returnAllConsonants();
Log.i(StaticValues.TAG, "returned all consonants");
}
else
{
tmpArray = tmpHelper.returnAllVowels();
Log.i(StaticValues.TAG, "returned all vowels");
}
if(tmpArray != null)
{
TableRow row = null;
InfoButton tmpButton = null;
//LayoutParams rowParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//TableRow.LayoutParams equalParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0);
LinearLayout.LayoutParams buttonLayout = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
if(basedView != null)
{
for(int i = 0; i < tmpArray.size(); i++)
{
//adds new row to table if modulus is 0
if(( i % 2 ) == 0)
{
row = (TableRow) getLayoutInflater(getArguments()).inflate(R.layout.table_row, null);
//row.setLayoutParams(rowParams);
basedView.addView(row);
}
tmpButton = (InfoButton) getLayoutInflater(getArguments()).inflate(R.layout.info_button, null);
tmpButton.setBackgroundResource(R.drawable.button_bg);
tmpButton.setLayoutParams(buttonLayout);
//set button Text
tmpButton.setText(tmpArray.get(i).buttonText);
//Obtain extra held info
tmpButton.setExtraInfo(tmpArray.get(i).infoHeld);
//tmpButton.setWidth(buttonWidth);
//tmpButton.setHeight(buttonHeight);
//tmpButton.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
tmpButton.setTextColor(getResources().getColor(R.color.button_text_color));
tmpButton.setTypeface(Typeface.DEFAULT_BOLD);
row.addView(tmpButton);
tmpButton.setOnClickListener(listenerDynamicButton);
}
}
}
}
出于某种原因,即使我的 logcat 输出显示一切都很好(按钮显示正确名称的按钮数量),但表格布局没有被填充。