
我正在运行时创建一个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);如果有人可以帮助我,那会很棒!谢谢 :)
解决方法:
因为您必须将重力设置为线性布局,所以只需在设置线性布局参数后放置此线
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);希望能帮助到你
总结以上是内存溢出为你收集整理的android – 以编程方式创建LinearLayout中的元素全部内容,希望文章能够帮你解决android – 以编程方式创建LinearLayout中的元素所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)