0

今晚我在互联网上进行了大量搜索,这就是我想出的。由于某种原因,它不起作用。我没有收到任何错误,我可以验证我的循环实际上正在运行。这是我的代码。

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();
4

3 回答 3

1

您用于 TableRow 的布局参数应该是 TableRow.LayoutParams,试试

TableRow.LayoutParams buttonparams = new TableRow.LayoutParams(); 
于 2013-03-08T10:30:08.760 回答
0

您可以将此示例用作参考。

LinearLayout imageslayout = (LinearLayout)findViewById(R.id.imageslayout);

for (int i = 0; i < 10; i++) {
                Button im = new Button(getApplicationContext());

                TableRow.LayoutParams blp = new TableRow.LayoutParams();
                blp.rightMargin = 20;
                blp.width=170;
                blp.height=140;  
                im.setLayoutParams(blp); 


                imageslayout.addView(im); 
                im.setOnClickListener(this);

            }
于 2013-03-08T09:49:02.317 回答
0

通过 buttonParams = new LayoutParams(height,width) 等构造函数设置按钮参数,而不是通过 buttonParams.height = height 等设置。不同之处在于分配不会调用无效,因此维度将为零。

于 2013-03-08T09:52:15.157 回答