在以编程方式创建LinearLayout后居中元素

时间:2015-09-30 14:45:13

标签: android android-layout position android-linearlayout center

我正在运行时创建一个LinearLayout,我在其中添加了两个元素:textview和一个微调器。 我想将它们水平居中在LinearLayout中,但无法弄清楚如何做到这一点。下面是我用来创建视图的代码:

LinearLayout leftSideAttributLayout = new LinearLayout(this);

leftSideAttributLayout.setOrientation(LinearLayout.HORIZONTAL);

LinearLayout.LayoutParams attributLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
leftSideAttributLayout.setLayoutParams(attributLayoutParams);

LinearLayout.LayoutParams leftLabelParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
leftLabelParams.gravity = Gravity.CENTER;
leftAttributLabel.setLayoutParams(leftLabelParams);         
TextView leftAttributLabel = new TextView(this);

leftAttributLabel.setText(attribut.getNom());
leftAttributLabel.setTextColor(Color.WHITE);
Tools.applyFont(getApplicationContext(), leftAttributLabel, "gothic_0.TTF");

LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
spinnerParams.gravity = Gravity.CENTER_HORIZONTAL;
spinnerParams.setMargins(10, 0, 0, 0);

Spinner leftAttributValues = new Spinner(this);
leftAttributValues.setLayoutParams(spinnerParams);
leftAttributValues.setAdapter(adapter);
leftAttributValues.setBackgroundResource(R.drawable.lenti_attributspinner);
leftAttributValues.setTag(attribut);

如果有人可以帮助我,那会很棒!谢谢:))

2 个答案:

答案 0 :(得分:13)

因为您必须将重力设置为线性布局,所以只需在设置线性布局参数

后放置此线
leftSideAttributLayout.setGravity(Gravity.CENTER);

这是您的代码中的示例

 LinearLayout leftSideAttributLayout = new LinearLayout(this);

            leftSideAttributLayout.setOrientation(LinearLayout.HORIZONTAL);

            LinearLayout.LayoutParams attributLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            attributLayoutParams.gravity = Gravity.CENTER;


            leftSideAttributLayout.setLayoutParams(attributLayoutParams);
            leftSideAttributLayout.setGravity(Gravity.CENTER);

            LinearLayout.LayoutParams leftLabelParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            //leftLabelParams.gravity = Gravity.CENTER;

            TextView leftAttributLabel = new TextView(this);

            leftAttributLabel.setText("sample");
            leftAttributLabel.setTextSize(25);
            leftAttributLabel.setLayoutParams(leftLabelParams);


            LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
           // spinnerParams.gravity = Gravity.CENTER;
            spinnerParams.setMargins(10, 0, 0, 0);

            Spinner leftAttributValues = new Spinner(this);
            leftAttributValues.setLayoutParams(spinnerParams);

            leftSideAttributLayout.addView(leftAttributLabel);
            leftSideAttributLayout.addView(leftAttributValues);

            mainview.addView(leftSideAttributLayout);

希望有所帮助

答案 1 :(得分:1)

问题是你从Layout而不是Spinner得到了参数,所以你的属性大小是全屏的,这会使重力修改无效。

使用此:

Spinner leftAttributValues = new Spinner(this);
leftAttributValues.setAdapter(adapter);
leftAttributValues.setBackgroundResource(R.drawable.lenti_attributspinner);
leftAttributValues.setTag(attribut);

// get actual spinner params
LayoutParams params = (LayoutParams) leftAttributValues.getLayoutParams();
// modify desired params 
params.gravity = Gravity.CENTER_HORIZONTAL;
params.setMargins(10, 0, 0, 0);
// set modified params to spinner again
leftAttributValues.setLayoutParams(params);