我有一个小问题。我动态创建了我的TableLayout,但它不会显示出来。
这是我的片段XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TableLayout
android:id="@+id/resourcesAvailableTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/back" >
</TableLayout>
当微调器被更改时,它会调用
onItemSelected(AdapterView<?> parent, View view, int position, long id)
这就是我相应填写TableLayout的地方。这是我填写的方式。
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
quickVesselRef = optimizer.Reference.vesselAlloys;
//I destroy the buttons and TableRows that were displayed previously
if (oreButtons != null){
availableRes.removeAllViews();
}
int[] imageRef = quickVesselRef[position].getCombinedOreResVessel();
oreButtons = new ImageButton[imageRef.length];
//create rows
oreRows = new TableRow[(int) Math.ceil(oreButtons.length /((screenWidth / (double)(buttonSize + padding))))];
for(int i = 0; i < oreRows.length; i++){
oreRows[i] = new TableRow(getActivity());
availableRes.addView(oreRows[i]);
}
//create j buttons per i rows
int buttonsPerRow = screenWidth / (buttonSize + padding);
for (int i = 0; i < oreRows.length; i++){
for (int j = 0; j < buttonsPerRow; j++){
if (i * buttonsPerRow + j >= imageRef.length)
break;
ImageButton button = new ImageButton(getActivity());
oreButtons[i * buttonsPerRow + j] = button;
button.setScaleType(ScaleType.FIT_CENTER);
button.setLayoutParams(new LayoutParams(buttonSize, buttonSize));
button.setImageResource(imageRef[i * buttonsPerRow + j]);
oreRows[i].addView(button);
}
}
}
我的TableLayout被调用:
availableRes
此代码不会崩溃,但它不显示我创建的TableLayout,我不知道为什么。