在我的 android 应用程序中,我想创建一个 1 行和 3 列的线性布局。然后在每个单元格中,添加一个 1 列 2 行的新线性布局。
目前它是这样做的,但是三个单元格中的每一个的宽度都包裹了内容,因此主线性布局的整体宽度小于屏幕宽度。我希望它与屏幕宽度匹配,然后将三个单元格中每个单元格的内容水平居中。
有谁知道如何解决这一问题?
谢谢
这是我的代码
LinearLayout layout = new LinearLayout(this);
layout.setWeightSum(1);
layout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(params);
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// ---------
LinearLayout Celllayout = new LinearLayout(this);
Celllayout.setOrientation(LinearLayout.VERTICAL);
Celllayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
TextView titleView = new TextView(this);
titleView.setLayoutParams(lparams);
titleView.setText("Use This");
RadioButton useMapRadio = new RadioButton(this);
Celllayout.addView(titleView);
Celllayout.addView(useMapRadio);
layout.addView(Celllayout);
// ---------
Celllayout = new LinearLayout(this);
Celllayout.setOrientation(LinearLayout.VERTICAL);
Celllayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
titleView = new TextView(this);
titleView.setLayoutParams(lparams);
titleView.setText(date);
ImageButton imagebutton = new ImageButton(this);
imagebutton.setImageResource(R.drawable.calendar);
imagebutton.setBackgroundResource(0);
Celllayout.addView(titleView);
Celllayout.addView(imagebutton);
layout.addView(Celllayout);
// ---------
Celllayout = new LinearLayout(this);
Celllayout.setOrientation(LinearLayout.VERTICAL);
Celllayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
titleView = new TextView(this);
titleView.setLayoutParams(lparams);
titleView.setText("Delete");
imagebutton = new ImageButton(this);
imagebutton.setImageResource(R.drawable.x);
imagebutton.setBackgroundResource(0);
Celllayout.addView(titleView);
Celllayout.addView(imagebutton);
layout.addView(Celllayout);
// ---------
linearlayout.addView(layout);