是否可以将自动添加的按钮居中放置在“网格”视图中,而无论重力如何设置,它们始终显示在左侧
button = new Button(context);
button.getRootView().findViewById(R.id.buttonzagrid);
button.setLayoutParams(new GridView.LayoutParams(85,85));
button.setPadding(8,8,8,8);
button.setTextColor(Color.parseColor("#332FA2"));
button.setGravity(Gravity.CENTER);
button.setTextSize(18);
button.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/FredokaOneRegular.ttf"));
button.setBackgroundResource(bgpink);
答案 0 :(得分:0)
实际上,您在此代码行中所做的工作
button.setGravity(Gravity.CENTER);
是您将按钮的重力设置为CENTER
,而不是包含它的布局。
为此,您需要指定您的layoutParams ..诸如此类..
GridLayout.LayoutParams lp = new GridLayout.LayoutParams;
lp.setGravity(Gravity.CENTER);
button.setLayoutParams(lp); // now you can add your button and it will be viewed correctly