,可以通过以下步骤实现:
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
int buttonWidth = screenWidth / m;
int buttonHeight = screenHeight / n;
GridLayout gridLayout = findViewById(R.id.gridLayout);
for (int i = 0; i < gridLayout.getChildCount(); i++) {
Button button = (Button) gridLayout.getChildAt(i);
GridLayout.LayoutParams params = (GridLayout.LayoutParams) button.getLayoutParams();
params.width = buttonWidth;
params.height = buttonHeight;
button.setLayoutParams(params);
}
通过以上步骤,可以根据屏幕设置GridLayout中按钮的宽度和高度。这样可以保证在不同屏幕尺寸下,按钮的大小能够适配并均匀分布在GridLayout中。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云