Android布局:创建板,居中和自动大小

时间:2015-04-19 16:28:22

标签: android

我花了一个星期试图做到这一点,但没有结果。

我想做一些像图像中的内容。

  1. 我想制作按钮表(用户选择5x5,10x10或者 别的东西)
  2. 表格应水平和垂直居中
  3. 表格单元格中的每个按钮的大小都应相对于 设备屏幕
  4. 每个按钮都应该是一个矩形。

    enter image description here

  5. 编辑:
    这是我的代码:

    public class MainActivity extends ActionBarActivity {
    
            private int game_counter=1;
            private int nof_columns = 10;
    
    
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
    
                GridView table_layout = new GridView(this);
                table_layout.setNumColumns(this.nof_columns);
    
    
                int button_size=5;
    
                for(int rows=1; rows<=this.nof_columns; rows++) {
                    TableRow rowT = new TableRow(this);
    
                    for(int columns=1; columns<=this.nof_columns; columns++){
                        Button bn = new Button(this);
                        bn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                onclick_function(v);
                            }
                        });
                        bn.setId(100 + rows * this.nof_columns + columns);
                        bn.setWidth(button_size);
                        bn.setHeight(button_size);
    
    
                        bn.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
                        MyTags tag = new MyTags();
                        bn.setTag(tag);
                        rowT.addView(bn);
                    }
                    //rowT.setPadding(-5,-5,-5,-5);
    
                    rowT.setMinimumHeight(button_size);
                    rowT.setMinimumWidth(button_size);
    
                    table_layout.addView(rowT);
                    //table_layout.setPadding(-70,-70,-70,-70);
                }
                setContentView(table_layout);
            }
    
    
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.menu_main, menu);
                return true;
            }
    
            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                // Handle action bar item clicks here. The action bar will
                // automatically handle clicks on the Home/Up button, so long
                // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();
    
                //noinspection SimplifiableIfStatement
                if (id == R.id.action_settings) {
                    return true;
                }
    
                return super.onOptionsItemSelected(item);
            }
        }
    

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 它在Defining a percentage width for a LinearLayout?

中提到过

稍后我会在电路板上发布我的代码。

谢谢!

相关问题