0

我叫帕维尔。

我想要这样的布局:

布局

我想在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);
    }

日食中的错误:

09-21 12:41:09.062: E/AndroidRuntime(758): java.lang.ArithmeticException: 除以零

我认为它与拉伸柱有关。

我的问题是:如何在 android 中纠正这个问题,使布局如图所示?

4

1 回答 1

2

更改此行

LinearLayout.LayoutParams buttonLayout = 
             new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f);

TableRow.LayoutParams buttonLayout = 
             new TableRow.LayoutParams(LayoutParams.FILL_PARENT, 
                                       LayoutParams.WRAP_CONTENT);

当您扩展所有字段时,我觉得不需要 weight 参数。与 FILL_PARENT 相同

于 2012-09-21T16:05:58.430 回答