代码生成GridLayout

(1)获取屏幕宽度,高度
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
displayWidth = displayMetrics.widthPixels;
displayHeight = displayMetrics.heightPixels;
(2)动态布局方法
private void updateView(final ArrayList<BmServiceCategory> data) {
if (data.size() > 1) {
View view = LayoutInflater.from(context).inflate(
R.layout.layout_grid, null);
GridLayout grid_layout = (GridLayout) view.findViewById(R.id.grid);


grid_layout.setColumnCount(4);
for (int i = 0; i < data.size(); i++) {


if (i == 0) {
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.columnSpec = GridLayout.spec(0);
params.rowSpec = GridLayout.spec(0, 2);
int temp = (int) (displayWidth / 4.5);
params.width = temp;
params.height = temp;



ImageView img = new ImageView(context);
img.setScaleType(ScaleType.FIT_XY);
img.setLayoutParams(params);
FileUtil.getInstance(context).loadNetImage(
data.get(i).categoryImageUrl, img);
grid_layout.addView(img);
} else if (i != 4) {
TextView txt_title1 = null;
txt_title1 = new TextView(context);


GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.columnSpec = GridLayout.spec(i % 4);
params.rowSpec = GridLayout.spec(i / 4);
txt_title1.setGravity(Gravity.CENTER);
txt_title1.setTextColor(getResources().getColor(
R.color.mine_info_myservice));
txt_title1.setTextSize(15);
int temp1 = (int) (displayWidth / 4.5);
params.width = temp1;
int temp_heght = temp1 / 2;
params.height = temp_heght;
txt_title1.setSingleLine(true);


txt_title1.setEllipsize(TextUtils.TruncateAt.END);
txt_title1.setLayoutParams(params);
final int temp = i;
final BmServiceCategory category = data.get(i);
// txt_title1.setSelected(true);
txt_title1.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// String cat_id = data.get(temp).categoryId;
Intent intent = new Intent(BmServicesActivity.this,
IBmServiceActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("categroy", category);
intent.putExtra("bundle", bundle);
startActivity(intent);
}
});
txt_title1.setFocusable(true);
txt_title1.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.select_btn));
txt_title1.setText(data.get(i).categoryName);
grid_layout.addView(txt_title1);


}


}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);


params.leftMargin = displayWidth / 18;
params.topMargin = displayWidth / 20;
// params.
liner_layout.addView(grid_layout, params);
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值