gridLayout动态添加button按钮

学习笔记根据网上资料整理,在此对原作者表示感谢!


代码如下:

public class CityActivity extends Activity {
  private GridLayout gridLayout;
  String[][] count = { { "北京", "上海", "天津", "哈尔滨" }, { "沈阳", "深圳", "广州", "重庆" }, { "广州", "香港", "厦门", "南京" } };
  gridLayout = (GridLayout) findViewById(R.id.hot_city_gridlayout);
  private void setHotCityGridLayout() {
    GridLayout.LayoutParams params = null;
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 4; j++) {
        final Button btn = new Button(this);
        btn.setTag(count[i][j]);
        btn.setWidth(40);
        btn.setBackgroundResource(R.drawable.btn_shape_login01);
        btn.setText(count[i][j]);
        btn.setPadding(20, 5, 5, 5);// 设置的是内边距
        // 设置点击按钮实现数据的回传
        btn.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
            Intent intent = new Intent(CityActivity.this, QueryActivity.class);
            intent.putExtra("cityname", btn.getTag().toString());
            setResult(100, intent);
            Log.i("TAG", "btnID:" + btn.getTag().toString());
            finish();
          }
        });
        GridLayout.Spec rowSpec = GridLayout.spec(i); // 设置btn的行
        GridLayout.Spec columnSpec = GridLayout.spec(j);// 设置btn的列
        params = new GridLayout.LayoutParams(rowSpec, columnSpec);
        params.width = 100; //设置btn的宽
        params.height = 60;//设置btn的高
        params.setMargins(5, 0, 5, 5);//设置btn的间距
        params.setGravity(Gravity.LEFT);//设置btn位置(靠左)
        gridLayout.addView(btn, params);
      }
  }
}
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final LinearLayout layout = new LinearLayout(this);// 生成一个LinearLayout
        layout.setOrientation(LinearLayout.VERTICAL);
        setContentView(layout);
        final Button btn = new Button(this);
        btn.setText("Button1");
        layout.addView(btn);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                layout.removeView(btn);
                layout.addView(btn);
            }
        });
        setContentView(layout);
    }
}

动态添加子控件

for (int i = 0, j = list.size(); i < j; i++) {
    ......
    View functionView = new View(getContext());
    functionView.setBackgroundResource(iconResId);
    ......
    //使用Spec定义子控件的位置和比重
    GridLayout.Spec rowSpec = GridLayout.spec(i / 3, 1f);
    GridLayout.Spec columnSpec = GridLayout.spec(i % 3, 1f);
    //将Spec传入GridLayout.LayoutParams并设置宽高为0,必须设置宽高,否则视图异常
    GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams(rowSpec, columnSpec);
    layoutParams.height = 0;
    layoutParams.width = 0;
    //还可以根据位置动态定义子控件直接的边距,下面的意思是
    //第一行的子控件都有2dp的bottomMargin,中间位置的子控件都有2dp的leftMargin和rightMargin
    if (i / 3 == 0)
        layoutParams.bottomMargin = getResources().getDimensionPixelSize(R.dimen.dp_2);
    if (i % 3 == 1) {
        layoutParams.leftMargin = getResources().getDimensionPixelSize(R.dimen.dp_2);
        layoutParams.rightMargin = getResources().getDimensionPixelSize(R.dimen.dp_2);
    }
    functionGrid.addView(functionView, layoutParams);
}

你可能感兴趣的:(Andriod)