我有一个程序可以在水平容器中生成一行 4 个按钮,该容器被添加到垂直容器中。
我不知道如何让按钮居中,现在它们与右侧对齐。代码:
for(int i=0; i<cGlobals.mNames.length; i+=2) {
iSoundIdList[i]=soundPool.load(this, cGlobals.mSounds[i], 1);
iSoundIdList[i+1]=soundPool.load(this, cGlobals.mSounds[i+1], 1);
// would like these views to all be centered
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(llp);
favBut[i]=new ImageView(this);
favBut[i].setImageResource(R.drawable.heartunselected);
favBut[i].setId(defStartFavId+i);
favBut[i].setOnClickListener(this);
layout.addView(favBut[i]);
Button but1=new Button(this);
but1.setText( cGlobals.mNames[i]);
but1.setWidth(120);
layout.addView(but1);
but1.setOnClickListener(this);
but1.setId(defStartButId+i);
TextView t=new TextView(this);
t.setText(" ");
layout.addView(t);
favBut[i+1]=new ImageView(this);
favBut[i+1].setImageResource(R.drawable.heartunselected);
favBut[i+1].setId(defStartFavId+i+1);
favBut[i+1].setOnClickListener(this);
layout.addView(favBut[i+1]);
t=new TextView(this);
t.setText(" ");
layout.addView(t);
Button but2=new Button(this);
but2.setText( cGlobals.mNames[i+1]);
but2.setWidth(120);
but2.setId(defStartButId+i+1);
but2.setOnClickListener(this);
layout.addView(but2);
Container.addView(layout);
}