1
我這是怎麼創造出大小的按鈕的5×5格:編程設置按鈕的背景顏色去除保證金和圓角
buttons_in_row = 5;
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
layout.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < buttons_in_row; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < buttons_in_row; j++) {
Button btn = new Button(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
btn.setLayoutParams(params);
btn.setText("B " + (j + 1 + (i * buttons_in_row)));
btn.setId(j + 1 + (i * buttons_in_row));
btn.setWidth(width/buttons_in_row);
btn.setHeight(width/buttons_in_row);
btn.setOnClickListener(this);
row.addView(btn);
}
layout.addView(row);
}
當我設置背景顏色比別的東西默認情況下,這是發生了什麼:
btn.setBackgroundColor(Color.RED);