今晚我在互联网上进行了大量搜索,这就是我想出的。由于某种原因,它不起作用。我没有收到任何错误,我可以验证我的循环实际上正在运行。这是我的代码。
TableLayout table = (TableLayout) findViewById(R.id.tablelayout1);
while (numsounds>0){
Log.d("MYTAG", ""+numsounds);
if(numsounds>=3){
Log.d("MYTAG", ""+numsounds);
TableRow row = new TableRow(this);
for (int j = 0; j < 3; j++) {
int button_num = j + 1 + ( row_num * 3);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
buttonparams.height = LayoutParams.MATCH_PARENT;
int btnwidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
buttonparams.width = btnwidth;
buttonparams.weight = .31f;
Button btn = new Button(this);
btn.setLongClickable(true);
btn.setLayoutParams(buttonparams);
btn.setText(soundtitleArray[button_num]);
btn.setId(j + 1 + ( button_num));
btn.getBackground().setAlpha(150);
btn.setOnClickListener(buttonClickListener);
btn.setOnLongClickListener(buttonLongClickListener);
row.addView(btn);
}
numsounds=numsounds-3;
Log.d("MYTAG", ""+numsounds);
table.addView(row);
row_num = row_num+1;
}
if(numsounds<3){
Log.d("MYTAG", ""+numsounds);
TableRow row = new TableRow(this);
for (int j = 0; j < numsounds; j++) {
int button_num = j + 1 + ( row_num * numsounds);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
buttonparams.height = LayoutParams.MATCH_PARENT;
int btnwidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
buttonparams.width = btnwidth;
float btnweight = 1/numsounds;
buttonparams.weight = btnweight;
Button btn = new Button(this);
btn.setLongClickable(true);
btn.setLayoutParams(buttonparams);
btn.setText(soundtitleArray[button_num]);
btn.setId(j + 1 + ( button_num));
btn.getBackground().setAlpha(150);
btn.setOnClickListener(buttonClickListener);
btn.setOnLongClickListener(buttonLongClickListener);
row.addView(btn);
}
table.addView(row);
row_num = row_num+1;
numsounds=numsounds-1;
Log.d("MYTAG", ""+numsounds);
}
}
我知道代码正在运行,因为它将数字的变化输出到 logcat。我能弄清楚的是它一直给我一个空白屏幕,只显示我的背景。按钮不显示。
我的进口也如下。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.<MYAPP>.SoundBoard;
import com.<MYAPP>.R;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.AudioColumns;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.widget.TableRow.LayoutParams;
我在课程开始时将按钮参数声明为:
private TableLayout.LayoutParams buttonparams = new TableLayout.LayoutParams();