Android Холст занимает все пространство, кнопки не отображаются внизу - PullRequest
0 голосов
/ 03 августа 2020

У меня есть этот код, который представляет aws сетку с четырьмя кнопками, которые управляют количеством вертикальных и горизонтальных линий, появляющихся в сетке.

Однако я пытаюсь разместить кнопки в низ. Я думал, что смогу сделать это, раскомментировав приведенный ниже код в MainActivity. java, но когда я это делаю, холст, кажется, занимает все пространство. Как это исправить?

Спасибо.

MainActivity. java

package com.neilzanella.grid;

import android.app.*;

import android.os.*;

import android.view.*;

import android.widget.*;

import com.neilzanella.grid.*;

public class MainActivity extends Activity

{

    private PixelGridView pixelGrid;

    

@Override

public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

    

        LinearLayout parent = new LinearLayout(this);

        parent.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        parent.setOrientation(LinearLayout.VERTICAL);

        pixelGrid = new PixelGridView(this);

pixelGrid.setNumColumns(4);

pixelGrid.setNumRows(6);

        LinearLayout buttonsLayout = new LinearLayout(this);

        buttonsLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        buttonsLayout.setOrientation(LinearLayout.HORIZONTAL);

        Button lessHButton = new Button(this);

        Button moreHButton = new Button(this);

        Button lessVButton = new Button(this);

        Button moreVButton = new Button(this);

        lessHButton.setText("less H");

        moreHButton.setText("more H");

        lessVButton.setText("less V");

        moreVButton.setText("more V");

        lessHButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                pixelGrid.setNumColumns(pixelGrid.getNumColumns() - 1);

                pixelGrid.invalidate();

            }

        });

        moreHButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                pixelGrid.setNumColumns(pixelGrid.getNumColumns() + 1);

                pixelGrid.invalidate();

            }

        });

        lessVButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                pixelGrid.setNumRows(pixelGrid.getNumRows() - 1);

                pixelGrid.invalidate();

            }

        });

        moreVButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                pixelGrid.setNumRows(pixelGrid.getNumRows() + 1);

                pixelGrid.invalidate();

            }

        });

        

        buttonsLayout.addView(lessHButton);

        buttonsLayout.addView(moreHButton);

        buttonsLayout.addView(lessVButton);

        buttonsLayout.addView(moreVButton);

        

        parent.addView(buttonsLayout);

        parent.addView(pixelGrid);

        

        //parent.addView(pixelGrid);

        //parent.addView(buttonsLayout);

        

setContentView(parent);

}

}

GridCanvasView. java

package com.neilzanella.grid;

import android.app.*;

import android.content.*;

import android.graphics.*;

import android.os.*;

import android.util.*;

import android.view.*;

import java.util.*;

class PixelGridView extends View {

private int numColumns, numRows;

private int cellWidth, cellHeight;

private Paint blackPaint = new Paint();

private boolean[][] cellChecked;

public PixelGridView(Context context) {

this(context, null);

}

public PixelGridView(Context context, AttributeSet attrs) {

super(context, attrs);

blackPaint.setStyle(Paint.Style.FILL_AND_STROKE);

}

public void setNumColumns(int numColumns) {

        if (numColumns > 0) {

this.numColumns = numColumns;

calculateDimensions();

     }

}

public int getNumColumns() {

return numColumns;

}

public void setNumRows(int numRows) {

        if (numRows > 0) {

this.numRows = numRows;

calculateDimensions();

     }

}

public int getNumRows() {

return numRows;

}

@Override

protected void onSizeChanged(int w, int h, int oldw, int oldh) {

super.onSizeChanged(w, h, oldw, oldh);

calculateDimensions();

}

private void calculateDimensions() {

if (numColumns < 1 || numRows < 1) {

return;

}

cellWidth = getWidth() / numColumns;

cellHeight = getHeight() / numRows;

cellChecked = new boolean[numColumns][numRows];

invalidate();

}

@Override

protected void onDraw(Canvas canvas) {

canvas.drawColor(Color.WHITE);

if (numColumns == 0 || numRows == 0) {

return;

}

int width = getWidth();

int height = getHeight();

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

for (int j = 0; j < numRows; j++) {

if (cellChecked[i][j]) {

canvas.drawRect(i * cellWidth, j * cellHeight,

(i + 1) * cellWidth, (j + 1) * cellHeight,

blackPaint);

}

}

}

for (int i = 1; i < numColumns; i++) {

canvas.drawLine(i * cellWidth, 0, i * cellWidth, height, blackPaint);

}

for (int i = 1; i < numRows; i++) {

canvas.drawLine(0, i * cellHeight, width, i * cellHeight, blackPaint);

}

}

/*

@Override

public boolean onTouchEvent(MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {

int column = (int)(event.getX() / cellWidth);

int row = (int)(event.getY() / cellHeight);

cellChecked[column][row] = !cellChecked[column][row];

invalidate();

}

return true;

}

*/

//

}

У меня есть попытался исправить проблему с макетом с помощью следующего кода, но из-за этого мое приложение выдает ошибку sh. Не уверен, почему я не смог добавить layout_weight = "1" в параметры LinearLayout.

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
      buttonsLayout.getLayoutParams();
    params.weight = 1.0f;
    buttonsLayout.setLayoutParams(params);
...