在Android中添加的EditText来的LinearLayout编程 [英] Adding EditText to LinearLayout programatically in android

查看:319
本文介绍了在Android中添加的EditText来的LinearLayout编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能一边让我的 EditTexts 来适应的LinearLayout 端共享相同的空间大写金额。

I can't make my EditTexts to fit in LinearLayout side by side sharing the same amout of space.

下面是code,它做的:

Here is the code that does it:

LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textFieldsLayout = (LinearLayout) findViewById(R.id.LinearLayout2);

        for(int i=1; i <= 8; i++){

            final EditText ed = new EditText(this);

            ed.setText("" + i);

            ed.setInputType(2);

            ed.setLayoutParams(lparams);

            textFieldsLayout.addView(ed);
        }   
    }

这code设法增加的EditText 来我的布局,但他们并排出现,且有空白空间的结束的LinearLayout ,当我改变PARAMS为 WRAP_CONTENT ,只有第一个的EditText 添加到布局填充布局和别人不出现在那里,任何想法,我究竟做错了什么?

this code manages to add EditText to my layout but they appear side by side, and there is empty space at the end of LinearLayout, when I change params to WRAP_CONTENT, only first EditText added to layout fills the layout and others don't appear there, any idea what am I doing wrong here?

推荐答案

添加layout_weight到布局PARAMS

add layout_weight to the layout params

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
                       (LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);

这篇关于在Android中添加的EditText来的LinearLayout编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆