GridLayout上動態新增控制元件
阿新 • • 發佈:2019-01-24
Android上GridLayout和java上的感覺差不多,但是在添加布局內部新增控制元件就不是那麼容易了。
grid = (GridLayout)findViewById(R.id.grid); //獲取GridLayout控制元件 int count=1; for(int i=0;i<6;i++) for(int j=0;j<5;j++) { System.out.println("count:"+count); Button btn = new Button(this); btn.setWidth(40); btn.setText(String.valueOf(count)); count++; GridLayout.Spec rowSpec = GridLayout.spec(i); //設定它的行和列 GridLayout.Spec columnSpec=GridLayout.spec(j); GridLayout.LayoutParams params=new GridLayout.LayoutParams(rowSpec,columnSpec); params.setGravity(Gravity.LEFT); grid.addView(btn,params); }