2
我想將一些按鈕加載到片段內的表格佈局中。爲什麼不顯示。我看到改變背景的作品(在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,並有人問是之前它說,一切都被加載(如按鈕和行補充說,通過檢查logcat中後row.add()和basedView.add())
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輸出顯示一切都很好(按鈕顯示正確的名稱適當的按鈕數)他們表佈局不會填充。
似乎你的問題是LayoutParams。可能大概是 – wtsang02 2013-02-18 21:25:42
。這是以前活動的複製粘貼代碼。它在那裏工作,但沒有在碎片中工作。很奇怪。 – Raigex 2013-02-18 21:34:26