我叫帕維爾。如何在java中以編程方式製作表格佈局?
我想有佈局是這樣的:
我想在Java中的佈局,因爲按鈕的數量會發生變化。正方形是按鈕。 佈局包括:
<LinearLayout>
<Button/>
<TableLayout>
<TableRow>
<Button1/>
<Button2/>
</TableRow>
<TableRow>
<Button3/>
<Button4/>
</TableRow>
<TableLayout>
<Button/>
</LinearLayout>
我的代碼:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
int title = getIntent().getIntExtra(KEY, MainScreen.CHOICE_COMPUTER);
switch(title)
{
case MainScreen.CHOICE_COMPUTER:
setTitle("Computer");
break;
case MainScreen.CHOICE_LAPTOP:
setTitle("Laptop");
break;
case MainScreen.CHOICE_TV:
setTitle("Tv");
break;
case MainScreen.CHOICE_FRIDGE:
setTitle("Fridge");
break;
}
LinearLayout choiceLayout = new LinearLayout(this);
choiceLayout.setOrientation(LinearLayout.VERTICAL);
choiceLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
choiceLayout.setPadding(15, 15, 15, 15);
choiceLayout.setGravity(Gravity.CENTER);
choiceLayout.setBackgroundResource(R.drawable.background);
button1 = new Button(this);
LinearLayout.LayoutParams buttonParameters = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonParameters.setMargins(50, 20, 50, 70);
button1.setLayoutParams(buttonParameters);
button1.setGravity(Gravity.CENTER);
button1.setPadding(10, 10, 10, 10);
button1.setBackgroundResource(R.drawable.background_button);
switch(title)
{
case MainScreen.CHOICE_COMPUTER:
button1.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_button_computer, 0, 0);
button1.setText("Computer");
break;
case MainScreen.CHOICE_LAPTOP:
button1.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_button_laptop, 0, 0);
button1.setText("Laptop");
break;
case MainScreen.CHOICE_TV:
button1.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_button_tv, 0, 0);
button1.setText("Tv");
break;
case MainScreen.CHOICE_FRIDGE:
button1.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon_button_fridge, 0, 0);
button1.setText("Fridge");
break;
}
TableLayout choiceTableLayout = new TableLayout(this);
TableLayout.LayoutParams param1 = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
param1.setMargins(20, 20, 20, 20);
choiceTableLayout.setLayoutParams(param1);
choiceTableLayout.setGravity(Gravity.CENTER);
choiceTableLayout.setStretchAllColumns(true);
TableLayout.LayoutParams rowLayout = new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TableRow row1 = new TableRow(this);
TableRow row2 = new TableRow(this);
row1.setLayoutParams(rowLayout);
row2.setLayoutParams(rowLayout);
LinearLayout.LayoutParams buttonLayout = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f);
buttonLayout.setMargins(10, 10, 10, 10);
buttonOption1 = new Button(this);
buttonOption1.setLayoutParams(param1);
buttonOption1.setId(objectCounter++);
Log.d("Counter: ", Integer.toString(objectCounter));
Log.d("ID", Integer.toString(buttonOption1.getId()));
buttonOption1.setGravity(Gravity.CENTER);
buttonOption1.setBackgroundResource(R.drawable.option_off_background_button);
buttonOption1.setTextSize(19);
buttonOption1.setTextColor(getResources().getColor(R.color.color_text_button));
buttonOption1.setText("Off");
buttonOption1.setBackgroundResource(R.drawable.option_off_background_button);
buttonOption1.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.option1, 0, 0);
buttonOption2 = new Button(this);
buttonOption2.setLayoutParams(buttonLayout);
buttonOption2.setId(objectCounter++);
buttonOption2.setGravity(Gravity.CENTER);
buttonOption2.setBackgroundResource(R.drawable.option_background_button);
buttonOption2.setTextSize(19);
buttonOption2.setTextColor(getResources().getColor(R.color.color_text_button));
buttonOption2.setText(R.string.button_text_option2);
buttonOption2.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.option2, 0, 0);
buttonOption3 = new Button(this);
buttonOption3.setLayoutParams(buttonLayout);
buttonOption3.setId(objectCounter++);
buttonOption3.setGravity(Gravity.CENTER);
buttonOption3.setBackgroundResource(R.drawable.option_background_button);
buttonOption3.setTextSize(19);
buttonOption3.setTextColor(getResources().getColor(R.color.color_text_button));
buttonOption3.setText(R.string.button_text_option3);
buttonOption3.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.option3, 0, 0);
buttonOption4 = new Button(this);
buttonOption4.setLayoutParams(buttonLayout);
buttonOption4.setId(objectCounter++);
buttonOption4.setGravity(Gravity.CENTER);
buttonOption4.setBackgroundResource(R.drawable.option_background_button);
buttonOption4.setTextSize(19);
buttonOption4.setTextColor(getResources().getColor(R.color.color_text_button));
buttonOption4.setText(R.string.button_text_option4);
buttonOption4.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.option4, 0, 0);
buttonSend = new Button(this);
LinearLayout.LayoutParams buttonSendParameter = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonSendParameter.gravity = Gravity.RIGHT|Gravity.BOTTOM;
buttonSend.setLayoutParams(buttonSendParameter);
buttonSend.setId(objectCounter++);
buttonSend.setGravity(Gravity.RIGHT|Gravity.BOTTOM);
buttonSend.setBackgroundResource(R.drawable.background_button);
buttonSend.setBackgroundResource(R.drawable.button_send);
row1.addView(buttonOption1);
row1.addView(buttonOption2);
row2.addView(buttonOption3);
row2.addView(buttonOption4);
choiceTableLayout.addView(row1);
choiceTableLayout.addView(row2);
choiceLayout.addView(button1);
choiceLayout.addView(choiceTableLayout);
choiceLayout.addView(buttonSend);
setContentView(choiceLayout);
buttonOption1.setOnClickListener(this);
buttonOption2.setOnClickListener(this);
buttonOption3.setOnClickListener(this);
buttonOption4.setOnClickListener(this);
buttonSend.setOnClickListener(this);
}
錯誤蝕:
12月9日至21日:41:09.062:E/AndroidRuntime(758):java.lang中。 ArithmeticException:除以零
我認爲它與拉伸列連接。
我的問題是:如何糾正這在Android中使佈局如圖片?
如何在TableLayout中爲按鈕創建佈局?如果不將佈局設置爲TableLayout內的按鈕,則不會出現錯誤。 – Pawel
在哪一行發生錯誤異常 – Ronnie
09-21 15:15:13.285:E/AndroidRuntime(779):\t at android.widget.TableLayout.mutateColumnsWidth(TableLayout.java:583) – Pawel