创建5 x 5网格的方形按钮以编程方式填充布局

时间:2018-03-24 00:06:27

标签: java android button grid

目标是创建5x5网格按钮并填充屏幕

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = this;
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);  //Can also be done in xml by android:orientation="vertical"
    int widthOfCell = (int) squareSize(screenWidth().widthPixels, screenWidth().heightPixels, 9);
    for (int i = 0; i < 5; i++) {
        LinearLayout row = new LinearLayout(this);
        row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        for (int j = 0; j < 5; j++) {
            LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(0, 0, 1.0f);
            DynamicSquareLayout btnTag = new DynamicSquareLayout(this);
            btnTag.setLayoutParams(param);
            btnTag.setText("Button " + (j + 1 + (i * 4 )));
            btnTag.setId(j + 1 + (i * 4));
            row.addView(btnTag);
        }
        layout.addView(row);
    }
    setContentView(layout);
}

class DynamicSquareLayout  extends Button {
    public DynamicSquareLayout(Context context) {
        super(context);
    }
    public DynamicSquareLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public DynamicSquareLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(size, size);
    }
}

然而,它没有填满所有屏幕,如果按钮是正方形,如何修改它以填充屏幕

1 个答案:

答案 0 :(得分:0)

你需要设置 row.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));作为行布局参数,以便所有5行均匀分布在布局中。这是完整的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = this;
    LinearLayout layout = new LinearLayout(this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

    layout.setOrientation(LinearLayout.VERTICAL);  //Can also be done in xml by android:orientation="vertical"

    int widthOfCell = 20; //(int) squareSize(screenWidth().widthPixels, screenWidth().heightPixels, 9);
    for (int i = 0; i < 5; i++) {
        LinearLayout row = new LinearLayout(this);
        row.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
        for (int j = 0; j < 5; j++) {
            LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(0, 0, 1.0f);
            DynamicSquareLayout btnTag = new DynamicSquareLayout(this);
            btnTag.setLayoutParams(param);
            btnTag.setText("Button " + (j + 1 + (i * 4 )));
            btnTag.setId(j + 1 + (i * 4));
            row.addView(btnTag);
        }
        layout.addView(row);
    }
    layout.setLayoutParams(params);
    setContentView(layout);
}

class DynamicSquareLayout  extends android.support.v7.widget.AppCompatButton {
    public DynamicSquareLayout(Context context) {
        super(context);
    }
    public DynamicSquareLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public DynamicSquareLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(size, size);
    }
}