我正在制作一个简单的音板应用程序 - 按钮应该出现在两列中,按下按钮会播放声音。我做了它并使用 XML 定义了所有按钮,它工作正常,但是由于应用程序已经附加了一个侦听器以使用循环播放声音,我决定只使用代码来定义按钮。
所以代码没有错误,应用程序启动但没有按钮出现。这里有一个类似的问题:Content not show in dynamic created android TableLayout尽管如果我理解正确,建议的解决方案(确保我使用TableLayout.LayoutParams
)已经在这里应用。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main_table2);
int[] sounds = { ... };
String[] labels = { ... };
declareButtons(sounds, labels);
}
private void declareButtons(int[] sounds, String[] labels)
{
TableLayout tbl = (TableLayout) this.findViewById(R.id.tblMain);
TableRow row = null;
Button cell;
for (int i=0; i<sounds.length; i++)
{
cell = new Button(this);
cell.setText(labels[i]);
cell.setOnClickListener(this.getStartListener(sounds[i]));
cell.setBackgroundDrawable(getResources().getDrawable(R.drawable.soundbutton));
cell.setTextColor(color.white);
TableLayout.LayoutParams cellParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, (float) 1);
cellParams.leftMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());
cellParams.topMargin = cellParams.leftMargin;
if (i%2==0) // left column
{
row = new TableRow(this);
cell.setLayoutParams(cellParams);
row.addView(cell);
}
else // right column
{
cellParams.rightMargin = cellParams.leftMargin;
cell.setLayoutParams(cellParams);
row.addView(cell);
row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
tbl.addView(row);
}
}
if (sounds.length%2 > 0) // handle uneven amount of buttons
tbl.addView(row);
}
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/leonbg2"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="@+id/tblMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
</LinearLayout>
这是以前定义表行的方式,效果很好:
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/buttonaw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:background="@drawable/soundbutton"
android:text="A what?"
android:textColor="@android:color/white" />
<Button
android:id="@+id/buttona"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:background="@drawable/soundbutton"
android:text="Argh!"
android:textColor="@android:color/white" />
</TableRow>