0

我是一名初学者 android 开发人员,但我有一些 c# 技能。我用c#做了一个滑动拼图,我想在android上做。

这是我的代码:

  TableLayout layout = new TableLayout(this);
  setContentView(layout);
  layout.setBackgroundColor(Color.GRAY);
  final TextView text = new TextView(this);
  layout.addView(text);

  layout.setStretchAllColumns(true);  
    layout.setShrinkAllColumns(true); 

  int rows = 3;
final int colomuns = 3;


  for ( int i = 0 ; i < rows ; i ++)
  {

      TableRow tr = new TableRow(this);

      for ( int k = 0 ; k < colomuns ; k++)
      {
          final int indexk = k+1;
              final Button btn = new Button(this);
              final int indexi = i;
              final int index = indexi*colomuns+indexk ;
                btn.setId(index);
                btn.setTextSize(36);
                btn.setPadding(20, 20, 20, 20);
                btn.setTag(new Point(indexi, indexk));
                btn.setText(""+ index);

                //layout params
                TableRow.LayoutParams params = new TableRow.LayoutParams();
                params.setMargins(5, 5, 5, 5);
                btn.setLayoutParams(params);
                //----------------
                btn.setBackgroundColor(Color.WHITE);
                btn.setOnClickListener(new View.OnClickListener() 
                {
                    public void onClick(View v) {


                        TableRow.LayoutParams leftViewParams = new TableRow.LayoutParams();
                        leftViewParams.column = 1;


                        btn.setLayoutParams(leftViewParams);
                        Point p = (Point) btn.getTag();



                    }
                });
                tr.addView(btn);

      }

      layout.addView(tr);
  }

但是,我有一个问题,因为我还没有找到“位置”属性,我想如何使用 layout 来做到这一点?而我对 TableLayout 的选择是对的吗?

谢谢,

4

2 回答 2

0
TableRow.LayoutParams aa = new TableRow.LayoutParams();
aa = (android.widget.TableRow.LayoutParams) v.getLayoutParams();

TableRow.LayoutParams leftViewParams = new TableRow.LayoutParams();
leftViewParams.setMargins(aa.leftMargin + 50, 5, 5, 5);
leftViewParams.column = aa.column +1;
 v.setLayoutParams(leftViewParams);

这是结果:

图片

于 2013-07-16T07:47:56.460 回答
0

You should add the button to your layout and then offset its position using margins/padding, you shouldn't be positioning based on X,Y

use the left margin and top margins to try and align as you require.

于 2013-07-15T12:16:35.827 回答