在Android中以编程方式创建按钮

时间:2015-12-15 17:36:59

标签: android android-layout android-button

我只想在Android中以编程方式创建按钮,我知道。但事情是创建一个具有一定数量的按钮,它完全取决于屏幕的方向。

例如,如果设备处于纵向模式,我只需要连续两个按钮,三个或四个横向(用于移动)和四个或五个(用于平板电脑)对齐。

被修改
我试过下面的代码并得到了一些东西

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);


    pwidth = size.x;
    lHeight = size.x;

    pheight = size.y;
    lWidth = size.y +16;

    scrollView = new ScrollView(MainActivity.this);
    scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

    gl = new GridLayout(MainActivity.this);

    gl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    gl.setOrientation(GridLayout.HORIZONTAL);

    int orientation = this.getResources().getConfiguration().orientation;

    if (orientation == Configuration.ORIENTATION_PORTRAIT) {
        //code for portrait mode
        Toast.makeText(getApplicationContext(), "PORTRAIT", Toast.LENGTH_SHORT).show();

        if ( pheight > pwidth) {
            columnSize = pheight / pwidth;
            gl.setColumnCount(columnSize + 1);
            gl.setRowCount(columnSize + 1);

            Log.i("PORTRAIT WIDTH", String.valueOf(pwidth));
            Log.i("PORTRAIT HEIGHT", String.valueOf(pheight));
            Log.i("PORTRAIT COLUMN SIZE ", String.valueOf(columnSize));
            Log.i("PORTRAIT ROW SIZE ", String.valueOf(rowSize));
        }/*else if(width>height){

        }*/

    } else {
        //code for landscape mode
        Toast.makeText(getApplicationContext(), "LANDSCAPE", Toast.LENGTH_SHORT).show();

        if (lWidth < lHeight) {
            columnSize = lWidth / 400;
            gl.setColumnCount(columnSize + 1);
            gl.setRowCount(columnSize);

            Log.i("LANDSCAPE WIDTH", String.valueOf(lWidth));
            Log.i("LANDSCAPE HEIGHT", String.valueOf(lHeight));
            Log.i("LANDSCAPE COLUMN SIZE ", String.valueOf(columnSize));
            Log.i("LANDSCAPE ROW SIZE ", String.valueOf(columnSize));
        }
    }

    Log.i("ROW SIZE ", String.valueOf(columnSize));

    button = new Button[9];

    for (int i = 0; i < 9; i++) {

        GridLayout.LayoutParams param = new GridLayout.LayoutParams();
        param.height = 200;
        param.width = GridLayout.LayoutParams.WRAP_CONTENT;
        param.rightMargin = 100;
        param.topMargin = 100;
        param.leftMargin = 100;

        param.setGravity(Gravity.CENTER);

        button[i] = new Button(MainActivity.this);
        button[i].setLayoutParams(param);
        button[i].setText("Button " + String.valueOf(i));
        button[i].setTextSize(20);
        button[i].setPadding(50, 50, 50, 50);
        gl.addView(button[i]);
    }

    scrollView.addView(gl);
    setContentView(scrollView);

    for (item = 0; item < 9; item++) {
        button[item].setOnClickListener(new View.OnClickListener() {

            int pos = item;

            public void onClick(View v) {
                // TODO Auto-generated method stub

                Toast.makeText(getBaseContext(), pos + " Clicked",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

Portrait Landscape

2 个答案:

答案 0 :(得分:0)

在整数文件中声明不同资源限定符的列数。 让我们说,

在res / values / integers.xml中声明

<integer name="num_of_cols">2</integer>

在res / values-land / integers.xml中声明

<integer name="num_of_cols">3</integer>

在res / values-sw600dp / integers.xml中声明

<integer name="num_of_cols">4</integer>

在res / values-sw720dp / integers.xml中声明

<integer name="num_of_cols">6</integer>

等等。使用此整数值programmaticaly来定义添加按钮时的列数。

答案 1 :(得分:0)

首先,你需要获得屏幕尺寸,高度和宽度。假设按钮宽度和高度为80像素。左右边距为10px:

Display display = getWindowManager().getDefaultDisplay();
Log.d(TAG,"Display"+display.toString());

Point size = new Point();
display.getSize(size);

int width = size.x;
int height = size.y;

Log.d(TAG,"WIDTH:"+width);
Log.d(TAG,"HEIGHT:"+height);

int sizeOfButtonHeight = 100;
int sizeOfButtonWidth = 100;

RelativeLayout layout = new RelativeLayout(this);


int numberOfRows = height / sizeOfButtonHeight + 20;
Log.d(TAG,"NUMBER OF ROWS :"+numberOfRows);
int numberOfButtons = width / sizeOfButtonWidth + 20;


TableLayout layoutTable = new TableLayout(this);

for (int i = 1; i < numberOfRows; i++) {
    int count = 1;
    TableRow layoutRow = new TableRow(this);
    layoutRow.setId(2000 + i);
    LinearLayout.LayoutParams paramsRow = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    //paramsRow.topMargin =  i * sizeOfButtonHeight;
    paramsRow.gravity = Gravity.CENTER_VERTICAL;

    layoutRow.setLayoutParams(paramsRow);

    while (width > 100) { //button width plus margins
        Button button = new Button(this);
        button.setId(1000 + count);
        button.setMinWidth(80);
        button.setText("BUTTON");

        TableRow.LayoutParams paramsButton = new TableRow.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, count);
        paramsButton.width = sizeOfButtonWidth;
        paramsButton.height = sizeOfButtonHeight;

        paramsButton.topMargin = i * sizeOfButtonHeight;

        paramsButton.gravity = Gravity.CENTER;
        button.setLayoutParams(paramsButton);
        layoutRow.addView(button);
        width -= 100;
        count++;
    }
    layoutTable.addView(layoutRow, i-1);
}
layout.addView(layoutTable);
setContentView(layout);