简体   繁体   English

Android-动态设置按钮的布局宽度

[英]Android - set layout width dynamically for buttons

I automated the process of creating the layout and the buttons it contains. 我使创建布局及其包含的按钮的过程自动化。 Working with the GUI that Eclipse provide for editing layout, it let the use set the button's layout width property. 通过使用Eclipse提供的用于编辑布局的GUI,它可以让用户设置按钮的布局宽度属性。 I wish to do the same through code (dynamically). 我希望通过代码(动态地)执行相同的操作。 Basically, I wish to set the button's layout width property to MATCH_PARENT. 基本上,我希望将按钮的布局宽度属性设置为MATCH_PARENT。

A snipped code or a good reference will be great. 简短的代码或良好的参考将非常有用。 Thanks. 谢谢。

If your container is a LinearLayout its going to be like this: 如果您的容器是LinearLayout,则它将如下所示:

    LinearLayout container = (LinearLayout)findViewById(R.id.container);

    Button btn = new Button(this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.MATCH_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT);
    btn.setLayoutParams(lp);

    container.addView(btn);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM