我正在尝试将一些按钮加载到片段内的表格布局中。为什么这不会出现。我看到更改背景的工作原理(在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输出显示一切都很好(按钮显示正确名称的正确数量的按钮),它们的表布局也不会被填充。
答案 0 :(得分:0)
找出关于我自己问题的答案。
后的一切tmpButton = (InfoButton) getLayoutInflter()...
到
row.addView(tmpButton);
除外,必须删除
tmpButton.setText();
和
tmpButton.setExtraInfo(..);
虽然我不知道为什么那些杀了我的布局。因此,如果有人认为这部分我很乐意接受这个答案。