【问题标题】:Create Buttons programmatically in Android在 Android 中以编程方式创建按钮
【发布时间】:2015-12-15 17:36:59
【问题描述】:

我只想在我知道的 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();
            }
        });
    }

【问题讨论】:

  • 保持方向不变。这是第一个想到的想法。
  • 实际上,我会说它最终取决于父 ViewGroup 的宽度,所以你可以检查一下,看看有多少按钮适合。或者,如果您不想进行数学运算,请使用 FlowLayout 之类的名称。
  • 为纵向和横向模式创建单独的布局。
  • @Sharath 如果他们以编程方式创建按钮会有什么帮助?
  • @KishorPawar 我想要纵向和横向。所以不需要固定方向

标签: android android-layout android-button


【解决方案1】:

为不同的资源限定符声明整数文件中的列数。 可以说,

在 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>

等等。以编程方式使用此整数值来定义添加按钮时的列数。

【讨论】:

  • 我如何使用 integer.xml?
【解决方案2】:

首先您需要获取屏幕尺寸以及高度和宽度。假设 Button 的宽度和高度为 80 px。左右边距为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);

【讨论】:

  • 您的代码有效吗?为什么,因为即使按照您上面的代码修复了错误,我也会遇到应用程序崩溃
  • 发布您的堆栈跟踪。
  • tableRow 属于哪个小部件??并且 paramsButton 没有列属性
  • 我得到了这个'原因:java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()。'
猜你喜欢
  • 2014-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多