[英]How to create linear layout and center buttons in it in android?
In android how can you insert a linear layout using java, and have three buttons in it in a horizontal layout, and have the buttons centered. 在android中如何使用java插入线性布局,并在水平布局中有三个按钮,并使按钮居中。
So basically I want a horizontal linear layout with three columns and 1 row, each column has the same width, and to insert an image button that's vertically and horizontally centered in each layout cell. 所以基本上我想要一个具有三列和一行的水平线性布局,每列具有相同的宽度,并插入在每个布局单元格中垂直和水平居中的图像按钮。
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(params);
Does anyone know how to do this? 有谁知道如何做到这一点?
Thanks. 谢谢。
You can center all child views in a layout by setting the gravity for the layout to center horizontal like: 您可以通过将布局的重力设置为水平居中来将所有子视图居中放置在布局中:
LayoutParams layout_params = new LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT, Gravity.CENTER_HORIZONTAL);
followed by addView with newly constructed LayoutParams 然后使用新构造的LayoutParams进行addView
You can use weightsum = 3 for linearlayout, and layout_weight = 1 for each button. 您可以将weightsum = 3用于linearlayout,并将layout_weight = 1用于每个按钮。 See the detail code.
查看详细代码。 I hope this will help.
我希望这将有所帮助。 Good luck :)
祝好运 :)
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(params);
layout.setWeightSum(3);
for (int i = 0; i < 3; i++) {
LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
btnParams.weight = 1.0f;
Button btn = new Button(this);
btn.setText("Btn");
btn.setLayoutParams(btnParams);
layout.addView(btn);
}
setContentView(layout);
}
}
Or this code for better but more complicated ^^ 或者这个代码更好但更复杂^^
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
mainLayout.setLayoutParams(params);
mainLayout.setWeightSum(3);
for (int i = 0; i < 3; i++) {
LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setText("Btn");
btn.setLayoutParams(btnParams);
LinearLayout childLayout = new LinearLayout(this);
LinearLayout.LayoutParams childParam = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
childParam.weight = 1.0f;
childLayout.setLayoutParams(childParam);
childLayout.setGravity(Gravity.CENTER);
childLayout.addView(btn);
mainLayout.addView(childLayout);
}
setContentView(mainLayout);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.